12#include <QGraphicsScene>
13#include <QRandomGenerator>
15#include <QStyleOption>
24double rand01() {
return double( rand() ) / double( RAND_MAX ); }
30Disk::Disk( qreal r,
const MasterShape* master_shape )
31 : _r( r ), _master_shape( master_shape ) {}
34Disk::randomPoint()
const
38 p = QPointF( ( rand01() * 2.0 - 1.0 ),
39 ( rand01() * 2.0 - 1.0 ) );
40 }
while ( ( p.x() * p.x() + p.y() * p.y() ) > 1.0 );
45Disk::isInside(
const QPointF& p )
const
47 return QPointF::dotProduct( p, p ) <= _r * _r;
51Disk::boundingRect()
const
53 return QRectF( -_r, -_r, 2.0 *_r, 2.0 * _r );
57Disk::paint(QPainter *painter,
const QStyleOptionGraphicsItem *, QWidget *)
59 painter->setBrush( _master_shape->currentColor() );
60 painter->drawEllipse( QPointF( 0.0, 0.0 ), _r, _r );
68MasterShape::MasterShape( QColor cok, QColor cko )
69 : _f( 0 ), _state( Ok ), _cok( cok ), _cko( cko )
77 if ( _f != 0 ) _f->setParentItem(
this );
81MasterShape::currentColor()
const
83 if ( _state == Ok )
return _cok;
88MasterShape::currentState()
const
94MasterShape::paint( QPainter *,
const QStyleOptionGraphicsItem *, QWidget *)
100MasterShape::advance(
int step)
107 if ( p.x() < -SZ_BD ) {
108 auto point = parentItem() != 0
109 ? parentItem()->mapFromScene( QPointF( IMAGE_SIZE + SZ_BD - 1, p.y() ) )
110 : QPointF( IMAGE_SIZE + SZ_BD - 1, p.y() );
112 }
else if ( p.x() > IMAGE_SIZE + SZ_BD ) {
113 auto point = parentItem() != 0
114 ? parentItem()->mapFromScene( QPointF( -SZ_BD + 1, p.y() ) )
115 : QPointF( -SZ_BD + 1, p.y() );
118 if ( p.y() < -SZ_BD ) {
119 auto point = parentItem() != 0 ?
120 parentItem()->mapFromScene( QPointF( p.x(), IMAGE_SIZE + SZ_BD - 1 ) )
121 : QPointF( p.x(), IMAGE_SIZE + SZ_BD - 1 );
123 }
else if ( p.y() > IMAGE_SIZE + SZ_BD ) {
124 auto point = parentItem() != 0
125 ? parentItem()->mapFromScene( QPointF( p.x(), -SZ_BD + 1 ) )
126 : QPointF( p.x(), -SZ_BD + 1 );
138MasterShape::randomPoint()
const
141 return mapToParent( _f->randomPoint() );
145MasterShape::isInside(
const QPointF& p )
const
148 return _f->isInside( mapFromParent( p ) );
152MasterShape::boundingRect()
const
155 return mapRectToParent( _f->boundingRect() );
163Asteroid::Asteroid( QColor cok, QColor cko,
double speed,
double r )
169 this->setGraphicalShape( d );
173Asteroid::advance(
int step)
176 setPos( mapToParent( _speed, 0.0 ) );
177 MasterShape::advance( step );
192 for (
int i = 0; i < nb_tested; ++i )
194 if ( f2->isInside( f1->randomPoint() )
195 || f1->isInside( f2->randomPoint() ) )
204 for (
auto f : formes )
A disk is a simple graphical shape.
Abstract class that describes a graphical object with additional methods for testing collisions.
A class to store master shapes and to test their possible collisions with a randomized algorithm.
bool intersect(MasterShape *f1, MasterShape *f2)
Polymorphic class that represents the top class of any complex shape.