forked from MChemodanov/maze_solver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.cpp
More file actions
84 lines (69 loc) · 1.46 KB
/
robot.cpp
File metadata and controls
84 lines (69 loc) · 1.46 KB
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
#include "robot.h"
#include <iostream>
Point Robot::turnShift(Point originalPos, float originalDir, Point vector)
{
int length = sqrt(vector.x*vector.x + vector.y*vector.y);
float angle = atan2(vector.x, vector.y) + originalDir;
Point shift (0,0);
shift.x = round(length*sin(angle));
shift.y = round(length*cos(angle));
return originalPos + shift;
}
void Robot::truncDir()
{
while (dir > 2*M_PI)
dir -= 2*M_PI;
while (dir < 0)
dir += 2*M_PI;
}
Robot::Robot(Maze & newMaze, Point initialPosition, float initialDirection):
maze(newMaze),
position(initialPosition),
dir(initialDirection)
{
}
void Robot::moveStraight()
{
position = turnShift(position, dir, Point(0,1));
cout << "moveStraight" << endl;
}
void Robot::turnLeft()
{
dir += M_PI/2;
truncDir();
cout << "turnLeft" << endl;
}
void Robot::turnRight()
{
dir -= M_PI/2;
truncDir();
cout << "turnRight" << endl;
}
int Robot::addSensor(Point pos)
{
sensors.push_back(pos);
return sensors.size() - 1;
}
bool Robot::getSensorValue(unsigned int id)
{
return maze.getValue(turnShift(position, dir, getSensorPosition(id)));
}
int Robot::getSensorsCount()
{
return sensors.size();
}
Point Robot::getSensorPosition(unsigned int id)
{
if (id < sensors.size())
return sensors[id];
else
return Point(0,0);
}
Point Robot::getPosition()
{
return position;
}
float Robot::getDirection()
{
return dir;
}