forked from buffolu/project_3_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geometry.cpp
102 lines (86 loc) · 2.93 KB
/
Geometry.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
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
#include "Geometry.h"
#include <cmath>
#include <ctgmath>
#include <limits>
const double pi = 2. * atan2(1., 0.);
double to_radians(double theta_d) { return theta_d * pi / 180.0; }
double to_degrees(double theta_r) { return theta_r * 180.0 / pi; }
// construct a Cartesian_vector from a Polar_vector
Cartesian_vector::Cartesian_vector(const Polar_vector &pv) {
delta_x = pv.r * cos(pv.theta);
delta_y = pv.r * sin(pv.theta);
}
Cartesian_vector::Cartesian_vector() {
delta_x = 0.0;
delta_y = 0.0;
}
void Cartesian_vector::operator=(const Polar_vector &pv) {
delta_x = pv.r * cos(pv.theta);
delta_y = pv.r * sin(pv.theta);
}
// construct a Polar_vector from a Cartesian_vector
Polar_vector::Polar_vector(const Cartesian_vector &cv) {
r = sqrt((cv.delta_x * cv.delta_x) + (cv.delta_y * cv.delta_y));
/* atan2 will return a negative angle for Quadrant III, IV, must translate
* to I, II */
theta = atan2(cv.delta_y, cv.delta_x);
if (theta < 0.)
theta = 2. * pi + theta; // normalize theta positive
}
Polar_vector::Polar_vector() {
r = 0.0;
theta = 0.0;
}
void Polar_vector::operator=(const Cartesian_vector &cv) {
r = sqrt((cv.delta_x * cv.delta_x) + (cv.delta_y * cv.delta_y));
/* atan2 will return a negative angle for Quadrant III, IV, must translate
* to I, II */
theta = atan2(cv.delta_y, cv.delta_x);
if (theta < 0.)
theta = 2. * pi + theta; // normalize theta positive
}
Point::Point(double x, double y) : x(x), y(y) {}
Point::Point() {
x = 0.0;
y = 0.0;
}
void Point::print() const {
std::cout << std::setprecision(2) << "(" << x << ", " << y << ")";
}
bool Point::operator==(const Point &rhs) { return x == rhs.x && y == rhs.y; }
bool Point::operator!=(const Point &rhs) { return !operator==(rhs); }
double Point::distance(Point a, Point b) {
double x1 = a.x;
double y1 = a.y;
double x2 = b.x;
double y2 = b.y;
return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}
// return angle as degrees
double Point::getAngle(Point a, Point b) {
double angle = atan2(b.x - a.x,b.y - a.y) * 180 / M_PI;
if (angle < 0) {
angle += 360;
}
return angle;
}
Point Point::advance(Point point, double speed, double angle) {
double radians = to_radians(angle);
double delta_x = speed / 10 * sin(radians);
double delta_y = speed / 10 * cos(radians);
double epsilon = std::numeric_limits<double>::epsilon();
if (std::abs(delta_x) < epsilon) {
delta_x = 0.0;
}
if (std::abs(delta_y) < epsilon) {
delta_y = 0.0;
}
return {point.x + delta_x, point.y + delta_y};
}
Point Point::advance(Point beginning, Point target, double speed) {
if (distance(beginning, target) * 10 <= speed) {
return target;
}
double angle = getAngle(beginning, target);
return advance(beginning, speed, angle);
}