-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradians.rs
198 lines (171 loc) · 5.38 KB
/
radians.rs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::ops::{Add, AddAssign, Div, Sub, SubAssign};
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Radians(f64); // Radians range [0, pi*2)
pub const PI_2: f64 = std::f64::consts::PI * 2.0;
fn fix_radians(radians: f64) -> f64 {
if radians < 0.0 {
return radians + PI_2;
} else if radians > PI_2 {
return radians - PI_2;
} else {
return radians;
}
}
impl Radians {
pub fn new(radians: f64) -> Radians {
Radians(fix_radians(radians))
}
pub(crate) fn into_rays_index(&self, number_of_rays: usize) -> f64 {
number_of_rays as f64 / PI_2 * self.0
}
pub(crate) fn tan(&self) -> f64 {
self.0.tan()
}
pub fn to_f64(&self) -> f64 {
self.0
}
pub const ZERO: Radians = Radians(0.0);
pub const PI: Radians = Radians(std::f64::consts::PI);
pub const PI_2: Radians = Radians(std::f64::consts::PI * 2.0);
#[cfg(test)]
pub const OUT_OF_RANGE: Radians = Radians(std::f64::consts::PI * 3.0);
}
impl AddAssign for Radians {
fn add_assign(&mut self, rhs: Self) {
self.0 = fix_radians(self.0 + rhs.0);
}
}
impl SubAssign for Radians {
fn sub_assign(&mut self, rhs: Self) {
self.0 = fix_radians(self.0 - rhs.0);
}
}
impl Add for Radians {
type Output = Self;
fn add(self, rhs: Radians) -> Self {
Radians(fix_radians(self.0 + rhs.0))
}
}
impl Sub for Radians {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Radians(fix_radians(self.0 - rhs.0))
}
}
impl Sub<Radians> for &Radians {
type Output = Radians;
fn sub(self, rhs: Radians) -> Self::Output {
Radians(fix_radians(self.0 - rhs.0))
}
}
impl Div for Radians {
type Output = Radians;
fn div(self, rhs: Self) -> Self::Output {
Radians(self.0 / rhs.0)
}
}
impl Div<f64> for Radians {
type Output = Radians;
fn div(self, rhs: f64) -> Self::Output {
Radians(self.0 / rhs)
}
}
impl PartialOrd for Radians {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self.to_f64() == other.to_f64() {
return Some(std::cmp::Ordering::Equal);
}
if (self.to_f64() - other.to_f64()).abs() > std::f64::consts::PI {
if self.to_f64() > other.to_f64() {
return Some(std::cmp::Ordering::Less);
}
return Some(std::cmp::Ordering::Greater);
}
if self.to_f64() < other.to_f64() {
return Some(std::cmp::Ordering::Less);
}
return Some(std::cmp::Ordering::Greater);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn add_sub_assign() {
let radians_value = 2.5;
let radians_delta = 3.0;
let mut radians = Radians::new(radians_value);
radians += Radians::new(radians_delta);
assert_eq!(radians, Radians(radians_value + radians_delta));
radians += Radians::new(radians_delta);
assert_eq!(
radians,
Radians(radians_value + radians_delta + radians_delta - PI_2)
);
radians -= Radians::new(radians_delta);
assert_eq!(radians, Radians(radians_value + radians_delta));
radians -= Radians::new(radians_delta);
assert_eq!(radians, Radians(radians_value));
}
#[test]
fn add_sub() {
let radians_value = 2.5;
assert_eq!(
Radians(radians_value) + Radians(radians_value),
Radians(radians_value + radians_value)
);
assert_eq!(
Radians(radians_value) + Radians(radians_value) + Radians(radians_value),
Radians(radians_value + radians_value + radians_value - PI_2)
);
assert_eq!(
Radians(radians_value) + Radians(radians_value) + Radians(radians_value)
- Radians(radians_value),
Radians(radians_value + radians_value)
);
assert_eq!(
Radians::new(radians_value) + Radians::new(radians_value) + Radians::new(radians_value)
- Radians::new(radians_value)
- Radians::new(radians_value),
Radians(radians_value)
);
}
#[test]
fn div() {
let radians_1 = 2.5;
let radians_2 = 0.5;
assert_eq!(
Radians::new(radians_1) / Radians::new(radians_2),
Radians(radians_1 / radians_2)
);
assert_eq!(
Radians::new(radians_1) / radians_2,
Radians(radians_1 / radians_2)
);
}
#[test]
fn into_rays_index() {
let radians_value_1 = std::f64::consts::PI;
let radians_value_2 = std::f64::consts::PI * 3.0 / 2.0;
let number_of_rays = 100;
assert_eq!(
Radians::new(radians_value_1).into_rays_index(number_of_rays),
number_of_rays as f64 / 2.0
);
assert_eq!(
Radians::new(radians_value_2).into_rays_index(number_of_rays),
number_of_rays as f64 * 3.0 / 4.0
);
}
#[test]
fn partial_cmp() {
let radians_1 = Radians(std::f64::consts::PI * 7.0 / 4.0);
let radians_2 = Radians(std::f64::consts::PI / 4.0);
let radians_3 = Radians(std::f64::consts::PI * 2.0 / 4.0);
assert!(radians_1 < radians_2);
assert!(radians_2 > radians_1);
assert!(radians_2 < radians_3);
assert!(radians_3 > radians_2);
assert!(radians_1 == radians_1);
}
}