-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cpp
64 lines (58 loc) · 1.62 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
//
// Created by Admin on 6/18/2021.
//
#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 << "(" << x << ", " << y << ")";
}
bool Point::operator==(const Point &rhs) {
return x == rhs.x && y == rhs.y;
}