-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangle.rs
186 lines (165 loc) · 4.89 KB
/
angle.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
use std::ops::{Add, Div, Mul, Sub};
pub const TWO_PI_DEG: f64 = 360.;
pub const PI_DEG: f64 = 180.;
pub const RIGHT_ANG_DEG: f64 = 90.;
// A trait to implement on a numerical type that truncates its value to
// its whole number value, e.g., 1.7 is "floored" to 1.
pub trait CanFloor {
fn floor(self) -> Self;
}
// A trait to implement on a numerical type that represents a mathematical
// angle and has default implementations of methods to cap/limit it.
pub trait LimitAngle
where
Self: Sized
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ PartialOrd
+ CanFloor
+ From<f64>
+ Copy,
{
// Cap the angle within `cap` degrees and return a new angle consuming the self.
fn cap_angle(self, cap: Self) -> Self {
let val = self / cap;
let val = val - val.floor();
if val > Self::from(0.) {
cap * val
} else if val < Self::from(0.) {
cap - cap * val
} else {
val
}
}
// Cap the angle within 360 degrees and return a new angle consuming the self.
fn cap_angle_360(self) -> Self {
self.cap_angle(Self::from(TWO_PI_DEG))
}
// Cap the angle within 180 degrees and return a new angle consuming the self.
fn cap_angle_180(self) -> Self {
self.cap_angle(Self::from(PI_DEG))
}
// Cap the angle between 0. ..=1. and return a new angle consuming the self.
fn cap_angle_1(self) -> Self {
let val = self - self.floor();
if val < Self::from(0.) {
val + Self::from(1.)
} else {
val
}
}
// Cap the angle between 180 and -180 degrees return a new angle consuming the self.
fn cap_angle_between_180(self) -> Self {
let val = self / Self::from(TWO_PI_DEG);
let val = (val - val.floor()) * Self::from(TWO_PI_DEG);
if val < Self::from(-PI_DEG) {
val + Self::from(TWO_PI_DEG)
} else if val > Self::from(PI_DEG) {
val - Self::from(TWO_PI_DEG)
} else {
val
}
}
}
impl CanFloor for f64 {
fn floor(self) -> Self {
self.floor()
}
}
impl LimitAngle for f64 {}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::EPSILON_TEST;
use float_cmp::assert_approx_eq;
#[test]
fn should_cap_fractional_positive_angle_at_360() {
// Arrange
// Act
let result = 723.2.cap_angle_360();
// Assert
assert_approx_eq!(f64, 3.2, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_fractional_negative_angle_at_360() {
// Arrange
// Act
let result = (-723.2).cap_angle_360();
// Assert
assert_approx_eq!(f64, 356.8, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_integer_multiple_angle_at_360() {
// Arrange
// Act
let result = 720.0.cap_angle_360();
// Assert
assert_approx_eq!(f64, 0., result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_fractional_positive_angle_at_180() {
// Arrange
// Act
let result = 183.2.cap_angle_180();
// Assert
assert_approx_eq!(f64, 3.2, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_fractional_negative_angle_at_180() {
// Arrange
// Act
let result = (-183.2).cap_angle_180();
// Assert
assert_approx_eq!(f64, 176.8, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_integer_multiple_angle_at_180() {
// Arrange
// Act
let result = 360.0.cap_angle_180();
// Assert
assert_approx_eq!(f64, 0., result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_positive_angle_1() {
// Arrange
// Act
let result = 0.1.cap_angle_1();
// Assert
assert_approx_eq!(f64, 0.1, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_negative_angle_1() {
// Arrange
// Act
let result = (-0.9).cap_angle_1();
// Assert
assert_approx_eq!(f64, 0.1, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_between_180_when_angle_gt_180() {
// Arrange
// Act
let result = 189.3.cap_angle_between_180();
// Assert
assert_approx_eq!(f64, -170.7, result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_between_180_when_angle_lt_180() {
// Arrange
// Act
let result = 165.0.cap_angle_between_180();
// Assert
assert_approx_eq!(f64, 165., result, epsilon = EPSILON_TEST);
}
#[test]
fn should_cap_between_180_when_angle_lt_negative_180() {
// Arrange
// Act
let result = (-189.3).cap_angle_between_180();
// Assert
assert_approx_eq!(f64, 170.7, result, epsilon = EPSILON_TEST);
}
}