INFO702 - TPs
Loading...
Searching...
No Matches
TP0
objects.hpp
1
/****************************************************************************
2
** Author: J.-O. Lachaud, University Savoie Mont Blanc
3
** (adapted from Qt colliding mices example)
4
**
5
** Copyright (C) 2015 The Qt Company Ltd.
6
** Contact: http://www.qt.io/licensing/
7
****************************************************************************/
8
9
#ifndef OBJECTS_HPP
10
#define OBJECTS_HPP
11
12
#include <vector>
13
#include <QGraphicsItem>
14
15
static
const
int
IMAGE_SIZE = 600;
16
static
const
int
SZ_BD = 100;
17
18
/// @return a random floating point number between 0 and 1.
19
double
rand01();
20
21
/// @brief Abstract class that describes a graphical object with additional
22
/// methods for testing collisions.
23
struct
GraphicalShape
:
public
QGraphicsItem
24
{
25
virtual
QPointF randomPoint()
const
= 0;
26
virtual
bool
isInside(
const
QPointF& p )
const
= 0;
27
// Already in QGraphicsItem
28
// virtual QRectF boundingRect() const override;
29
};
30
31
32
/// @brief Polymorphic class that represents the top class of any complex
33
/// shape.
34
///
35
/// It takes care of memorizing collisions and storing the
36
/// current main coordinates of a shape.
37
struct
MasterShape
:
public
GraphicalShape
38
{
39
enum
State { Ok, Collision };
40
MasterShape
( QColor cok, QColor cko );
41
void
setGraphicalShape(
GraphicalShape
* f );
42
virtual
void
paint( QPainter *painter,
const
QStyleOptionGraphicsItem *option,
43
QWidget *widget)
override
;
44
virtual
QPointF randomPoint()
const override
;
45
virtual
bool
isInside(
const
QPointF& p )
const override
;
46
virtual
QRectF boundingRect()
const override
;
47
48
// Checks if this shape collides with another shape and forces also
49
// the shapes to stay in the graphical view.
50
virtual
void
advance(
int
step)
override
;
51
State currentState()
const
;
52
QColor currentColor()
const
;
53
54
protected
:
55
GraphicalShape
* _f;
56
State _state;
57
QColor _cok, _cko;
58
};
59
60
/// @brief An asteroid is a simple shape that moves linearly in some direction.
61
struct
Asteroid
:
public
MasterShape
62
{
63
Asteroid
( QColor cok, QColor cko,
double
speed,
double
r );
64
// moves the asteroid forward according to its speed.
65
virtual
void
advance(
int
step)
override
;
66
protected
:
67
double
_speed;
68
};
69
70
/// @brief A disk is a simple graphical shape.
71
///
72
/// It points to its master shape
73
/// in order to know in which color it must be painted.
74
struct
Disk
:
public
GraphicalShape
75
{
76
Disk
( qreal r,
const
MasterShape
* master_shape );
77
virtual
void
paint( QPainter *painter,
const
QStyleOptionGraphicsItem *option,
78
QWidget *widget)
override
;
79
virtual
QPointF randomPoint()
const override
;
80
virtual
bool
isInside(
const
QPointF& p )
const override
;
81
virtual
QRectF boundingRect()
const override
;
82
const
qreal _r;
83
const
MasterShape
* _master_shape;
84
};
85
86
87
/// @brief A class to store master shapes and to test their possible
88
/// collisions with a randomized algorithm.
89
struct
LogicalScene
{
90
std::vector< MasterShape*> formes;
91
int
nb_tested;
92
93
/// Builds a logical scene where collisions are detected by checking
94
/// \a n random points within shapes.
95
///
96
/// @param n any positive integer.
97
LogicalScene
(
int
n );
98
/// Given two shapes \a f1 and \a f2, returns if they collide.
99
/// @param f1 any master shape.
100
/// @param f2 any different master shape.
101
/// @return 'true' iff they collide, i.e. have a common intersection.
102
bool
intersect
(
MasterShape
* f1,
MasterShape
* f2 );
103
/// @param f1 any master shape.
104
/// @return 'true' iff it collides with a different master shape stored in this logical scene.
105
bool
intersect
(
MasterShape
* f1 );
106
};
107
108
extern
LogicalScene
* logical_scene;
109
110
#endif
111
Asteroid
An asteroid is a simple shape that moves linearly in some direction.
Definition
objects.hpp:62
Disk
A disk is a simple graphical shape.
Definition
objects.hpp:75
GraphicalShape
Abstract class that describes a graphical object with additional methods for testing collisions.
Definition
objects.hpp:24
LogicalScene
A class to store master shapes and to test their possible collisions with a randomized algorithm.
Definition
objects.hpp:89
LogicalScene::intersect
bool intersect(MasterShape *f1, MasterShape *f2)
Definition
objects.cpp:190
MasterShape
Polymorphic class that represents the top class of any complex shape.
Definition
objects.hpp:38
Generated on Sun Oct 22 2023 16:22:34 for INFO702 - TPs by
1.9.8