-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.cpp
More file actions
67 lines (58 loc) · 1.72 KB
/
Copy pathVehicle.cpp
File metadata and controls
67 lines (58 loc) · 1.72 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
#include "Vehicle.h"
#include <iostream>
void Vehicle::set_location(const shared_ptr<Point> &location) {
_location = std::make_shared<Point>(*location);
}
const shared_ptr<Point> &Vehicle::get_location() const {
return _location;
}
void Vehicle::stop() {
this->angle = 0.0;
this->speed = 0.0;
this->destination = nullptr;
this->en_route = false;
setState("Stopped");
}
double Vehicle::computeDistance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
return std::sqrt(dx * dx + dy * dy);
}
void Vehicle::setSpeed(double x) {
if (x< 0) {
std::cerr << "Speed cannot be negative. Setting to 0." << std::endl;
x = 0;
}
this->speed = x;
}
void Vehicle::position(double x, double y, double speed) {
pending_position = true;
pending_pos_x = x;
pending_pos_y = y;
pending_pos_speed = speed;
}
void Vehicle::course(double angle, double speed) {
pending_course = true;
pending_course_angle = angle;
pending_course_speed = speed;
}
void Vehicle::beginPositionMode(double worldSpeed) {
destination = std::make_shared<Point>(pending_pos_x, pending_pos_y);
angle = to_degrees(std::atan2(pending_pos_x - _location->x,
pending_pos_y - _location->y));
speed = worldSpeed;
mode = 0;
en_route = true;
}
void Vehicle::beginCourseMode(double worldSpeed) {
angle = pending_course_angle;
speed = worldSpeed;
destination = nullptr;
mode = 1;
en_route = true;
}
void Vehicle::stepAlongHeading(double stepDistance) {
double radians = to_radians(angle);
_location->x += stepDistance * std::sin(radians);
_location->y += stepDistance * std::cos(radians);
}