/
car.cpp
65 lines (53 loc) · 1.23 KB
/
car.cpp
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
#include <car.h>
#include <cmath>
inline double degreeToRads(double degree)
{
return degree*M_PI/180;
}
car::car(map m){
int x0, y0;
bool placed = false;
QRect c;
while (!placed)
{
x0 = rand()%(m.getBorder().width() - this->width);
y0 = rand()%(m.getBorder().height() - this->height);
c = QRect(x0,y0, this->width, this->height);
placed = !(c.intersects(m.getGoal()));
for (int i = 0; i < m.getObstacles().size(); ++i)
{
placed = placed & (!c.intersects(m.getObstacles().at(i)));
}
}
this->x = x0 + this->width/2;
this->y = y0 + this->height/2;
this->angle = 0;
this->rad = 0;
}
int car::getX(){
return static_cast<int>(this->x);
}
int car::getY(){
return static_cast<int>(this->y);
}
qreal car::getAngle(){
return this->angle;
}
QRect car::getCar()
{
return QRect(QPoint(-this->width/2, -this->height/2), QPoint(this->width/2, this->height/2));
}
int car::getRad()
{
return this->rad;
}
void car::moveForward(int step)
{
this->rad += step;
this->x += step*sin(degreeToRads(this->getAngle()));
this->y -= step*cos(degreeToRads(this->getAngle()));
}
void car::turn(qreal angle)
{
this->angle +=angle;
}