-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.hpp
209 lines (180 loc) · 6.85 KB
/
objects.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/****************************************************************************
** Author: J.-O. Lachaud, University Savoie Mont Blanc
** (adapted from Qt colliding mices example)
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
****************************************************************************/
#ifndef OBJECTS_HPP
#define OBJECTS_HPP
#include <vector>
#include <QGraphicsItem>
#include <QBitmap>
static const int IMAGE_SIZE = 600;
static const int SZ_BD = 100;
/// @brief Abstract class that describes a graphical object with additional
/// methods for testing collisions.
struct GraphicalShape : public QGraphicsItem
{
virtual QPointF randomPoint() const = 0;
virtual bool isInside( const QPointF& p ) const = 0;
// Already in QGraphicsItem
// virtual QRectF boundingRect() const override;
};
/// @brief Polymorphic class that represents the top class of any complex
/// shape.
///
/// It takes care of memorizing collisions and storing the
/// current main coordinates of a shape.
struct MasterShape : public GraphicalShape
{
enum State { Ok, Collision };
MasterShape( QColor cok, QColor cko );
void setGraphicalShape( GraphicalShape* f );
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
// Checks if this shape collides with another shape and forces also
// the shapes to stay in the graphical view.
virtual void advance(int step) override;
State currentState() const;
QColor currentColor() const;
protected:
GraphicalShape* _f;
State _state;
QColor _cok, _cko;
};
/// @brief Merge two shapes to create a more complex shape.
struct Union: public GraphicalShape
{
// The two shapes that are merged.
GraphicalShape & _f1;
GraphicalShape & _f2;
Union( GraphicalShape & f1, GraphicalShape & f2 );
virtual void paint( QPainter *, const QStyleOptionGraphicsItem *,
QWidget *) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
};
struct Transformation: public GraphicalShape
{
// The shape that is transformed.
GraphicalShape & _f;
// A vector used for the translation of the shape.
QPointF _dx;
// A real used for the rotation of the shape.
qreal _angle;
Transformation( GraphicalShape & f, QPointF dx );
Transformation( GraphicalShape & f, QPointF dx, qreal angle );
virtual void paint( QPainter *, const QStyleOptionGraphicsItem *,
QWidget *) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
void setAngle( qreal angle );
};
struct ImageShape: public GraphicalShape
{
const QPixmap _pixmap;
const MasterShape* _master_shape;
QBitmap _mask;
QImage _mask_img;
ImageShape( const QPixmap & pixmap, const MasterShape* master_shape );
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *,
QWidget *) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
};
/// @brief An asteroid is a simple shape that moves linearly in some direction.
struct Asteroid : public MasterShape
{
Asteroid( QColor cok, QColor cko, double speed, double r );
// moves the asteroid forward according to its speed.
virtual void advance(int step) override;
protected:
double _speed;
};
/// @brief A NiceAsteroid is a simple shape that moves linearly in some direction.
struct NiceAsteroid : public MasterShape
{
NiceAsteroid( QColor cok, QColor cko, double speed, double r );
// moves the asteroid forward according to its speed.
virtual void advance(int step) override;
protected:
double _speed;
Transformation* _t1;
Transformation* _t2;
};
/// @brief An asteroid is a simple shape that moves linearly in some direction.
struct SpaceTruck : public MasterShape
{
SpaceTruck( QColor cok, QColor cko, double speed );
// moves the asteroid forward according to its speed.
virtual void advance(int step) override;
protected:
double _speed;
};
/// @brief An enterprise is a simple shape that moves linearly in some direction.
struct Enterprise : public MasterShape
{
Enterprise( QColor cok, QColor cko, double speed );
// moves the asteroid forward according to its speed.
virtual void advance(int step) override;
protected:
double _speed;
};
/// @brief A disk is a simple graphical shape.
///
/// It points to its master shape
/// in order to know in which color it must be painted.
struct Disk : public GraphicalShape
{
Disk( qreal r, const MasterShape* master_shape );
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
const qreal _r;
const MasterShape* _master_shape;
};
/// @brief A rectangle is a simple graphical shape.
///
/// It points to its master shape
/// in order to know in which color it must be painted.
struct Rectangle : public GraphicalShape
{
Rectangle( const QPointF & x, const QPointF & y, const MasterShape* master_shape );
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
virtual QPointF randomPoint() const override;
virtual bool isInside( const QPointF& p ) const override;
virtual QRectF boundingRect() const override;
const QRectF _rect;
const MasterShape* _master_shape;
};
/// @brief A class to store master shapes and to test their possible
/// collisions with a randomized algorithm.
struct LogicalScene {
std::vector< MasterShape*> formes;
int nb_tested;
/// Builds a logical scene where collisions are detected by checking
/// \a n random points within shapes.
///
/// @param n any positive integer.
LogicalScene( int n );
/// Given two shapes \a f1 and \a f2, returns if they collide.
/// @param f1 any master shape.
/// @param f2 any different master shape.
/// @return 'true' iff they collide, i.e. have a common intersection.
bool intersect( MasterShape* f1, MasterShape* f2 );
/// @param f1 any master shape.
/// @return 'true' iff it collides with a different master shape stored in this logical scene.
bool intersect( MasterShape* f1 );
};
extern LogicalScene* logical_scene;
#endif