-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradian.cpp
115 lines (95 loc) · 2.22 KB
/
radian.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
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "radian.h"
#include "math.h"
#include <cstdlib>
#include <cstdio>
Radian::Radian(double val) {
//1 degree is 0.0174532925 radians
radVal = val * 0.0174532925;
normalize();
}
Radian::Radian() {
this->radVal = 0;
}
Radian::Radian(const Radian& other) {
this->radVal = other.radVal;
}
Radian Radian::operator*(double factor) {
Radian temp;
temp.radVal = radVal * factor;
temp.normalize();
return temp;
}
Radian Radian::operator+(double addend) {
Radian temp;
temp.radVal = radVal + addend;
temp.normalize();
return temp;
}
Radian Radian::operator-(double subtrahend) {
Radian temp;
temp.radVal = radVal - subtrahend;
temp.normalize();
return temp;
}
Radian Radian::operator-(Radian subtrahend) {
Radian temp;
temp.radVal = radVal - subtrahend.radVal;
temp.normalize();
return temp;
}
Radian Radian::operator*(Radian factor) {
Radian temp;
temp.radVal = radVal * factor.radVal;
temp.normalize();
return temp;
}
Radian Radian::operator+(Radian addend) {
Radian temp;
temp.radVal = radVal + addend.radVal;
temp.normalize();
return temp;
}
void Radian::add(double amount) {
this->radVal += amount;
normalize();
}
void Radian::add(Radian amount) {
this->radVal += amount.radVal;
normalize();
}
void Radian::subtract(double amount) {
this->radVal -= amount;
normalize();
}
void Radian::subtract(Radian amount) {
this->radVal -= amount.radVal;
normalize();
}
Radian Radian::operator/(double divisor) {
Radian temp;
temp.radVal = radVal / divisor;
temp.normalize();
return temp;
}
Radian& Radian::operator=(const Radian& other) {
this->radVal = other.radVal;
}
double Radian::toRad() {
return this->radVal;
}
double Radian::toDegrees() {
return radVal/0.0174532925;
}
// Normalizes a radian value - if it's more than 2PI, keep subtracting 2PI until it's not
// If it's less than 0, keep addding 2PI until it's not
void Radian::normalize() {
while (this->radVal >= 2 * M_PI) {
this->radVal -= 2 * M_PI;
}
while (this->radVal < 0) {
this->radVal += 2 * M_PI;
}
}
Vector2 Radian::toVector2() {
return Vector2(cos(this->radVal), sin(this->radVal));
}