-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geometry.cpp
82 lines (73 loc) · 2.03 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
#include "Geometry.h"
#include <cmath>
#include <ctgmath>
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
{
cout << setprecision(2) << "(" << x << ", " << y << ")";
}
bool Point::operator==(const Point & rhs)
{
return x == rhs.x && y == rhs.y;
}
double Point::getAngle(Point *p1, Point *p2) {
return atan2(p2->y - p1->y, p2->x - p1->x) * 180 / pi;
}
double Point::getDistance(const Point & a, const Point & b) {
return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2)) * 100;
}
//double getDistance(const Point & a, const Point & b) {
// return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2)) * 100;
//}