-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.cpp
More file actions
70 lines (65 loc) · 1.7 KB
/
Copy pathGeometry.cpp
File metadata and controls
70 lines (65 loc) · 1.7 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
#include "Geometry.h"
#include <cmath>
#include <iostream>
#include <iomanip>
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;
}