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
use super::measurement::*;
#[cfg(feature = "from_str")]
use regex::Regex;
#[cfg(feature = "from_str")]
use std::str::FromStr;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug)]
pub struct Angle {
radians: f64,
}
impl Angle {
pub fn from_degrees(degrees: f64) -> Self {
Angle::from_radians(degrees * ::PI / 180.0)
}
pub fn from_radians(radians: f64) -> Self {
Angle { radians }
}
pub fn as_degrees(&self) -> f64 {
self.radians * 180.0 / ::PI
}
pub fn as_radians(&self) -> f64 {
self.radians
}
#[cfg(feature = "std")]
pub fn cos(&self) -> f64 {
self.radians.cos()
}
#[cfg(feature = "std")]
pub fn sin(&self) -> f64 {
self.radians.sin()
}
#[cfg(feature = "std")]
pub fn sin_cos(&self) -> (f64, f64) {
self.radians.sin_cos()
}
#[cfg(feature = "std")]
pub fn tan(&self) -> f64 {
self.radians.tan()
}
#[cfg(feature = "std")]
pub fn asin(num: f64) -> Self {
Angle::from_radians(num.asin())
}
#[cfg(feature = "std")]
pub fn acos(num: f64) -> Self {
Angle::from_radians(num.acos())
}
#[cfg(feature = "std")]
pub fn atan(num: f64) -> Self {
Angle::from_radians(num.atan())
}
}
impl Measurement for Angle {
fn as_base_units(&self) -> f64 {
self.radians
}
fn from_base_units(units: f64) -> Self {
Self::from_radians(units)
}
fn get_base_units_name(&self) -> &'static str {
"rad"
}
}
#[cfg(feature = "from_str")]
impl FromStr for Angle {
type Err = std::num::ParseFloatError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
if val.is_empty() {
return Ok(Angle::from_degrees(0.0));
}
let re = Regex::new(r"(?i)\s*([0-9.]*)\s?(deg|\u{00B0}|rad)\s*$").unwrap();
if let Some(caps) = re.captures(val) {
let float_val = caps.get(1).unwrap().as_str();
return Ok(
match caps.get(2).unwrap().as_str().to_lowercase().as_str() {
"deg" | "\u{00B0}" => Angle::from_degrees(float_val.parse::<f64>()?),
"rad" => Angle::from_radians(float_val.parse::<f64>()?),
_ => Angle::from_degrees(val.parse::<f64>()?),
},
);
}
Ok(Angle::from_degrees(val.parse::<f64>()?))
}
}
implement_measurement! { Angle }
#[cfg(test)]
mod test {
use angle::*;
use std::f64::consts::PI;
use test_utils::assert_almost_eq;
#[test]
fn radians() {
let i1 = Angle::from_degrees(360.0);
let r1 = i1.as_radians();
let i2 = Angle::from_radians(PI);
let r2 = i2.as_degrees();
assert_almost_eq(r1, 2.0 * PI);
assert_almost_eq(r2, 180.0);
}
#[test]
#[cfg(feature = "from_str")]
fn angle_from_str() {
let t = Angle::from_str("100 deg");
assert!(t.is_ok());
let o = t.unwrap().as_degrees();
assert_almost_eq(o, 100.0);
}
#[test]
#[cfg(feature = "from_str")]
fn angle_from_degree_str() {
let t = Angle::from_str("100°");
assert!(t.is_ok());
let o = t.unwrap().as_degrees();
assert_almost_eq(o, 100.0);
}
#[test]
#[cfg(feature = "from_str")]
fn angle_from_radian_str() {
let t = Angle::from_str("100rad");
assert!(t.is_ok());
let o = t.unwrap().as_radians();
assert_almost_eq(o, 100.0);
}
#[test]
#[cfg(feature = "from_str")]
fn default_str() {
let t = Angle::from_str("100");
assert!(t.is_ok());
let o = t.unwrap().as_degrees();
assert_almost_eq(o, 100.0);
}
#[test]
#[cfg(feature = "from_str")]
fn invalid_str() {
let t = Angle::from_str("abcd");
assert!(t.is_err());
}
}