|
1 |
| -//! Types and constants for handling angles |
2 |
| -
|
3 |
| -use super::measurement::*; |
4 |
| -use ::std::f64::consts::PI; |
5 |
| - |
6 |
| -/// The 'Angle' struct can be used to deal with angles in a common way. |
7 |
| -/// |
8 |
| -/// # Example |
9 |
| -/// |
10 |
| -/// ``` |
11 |
| -/// use measurements::Angle; |
12 |
| -/// |
13 |
| -/// let whole_cake = Angle::from_degrees(360.0); |
14 |
| -/// let pieces = 6.0; |
15 |
| -/// let slice = whole_cake / pieces; |
16 |
| -/// println!("Each slice will be {} degrees", slice.as_degrees()); |
17 |
| -/// ``` |
18 |
| -#[derive(Copy, Clone, Debug)] |
19 |
| -pub struct Angle { |
20 |
| - radians: f64, |
21 |
| -} |
22 |
| - |
23 |
| -/// Number of degrees in a radian |
24 |
| -pub const RADIAN_DEGREE_FACTOR: f64 = 180.0 / PI; |
25 |
| - |
26 |
| -impl Angle { |
27 |
| - |
28 |
| - /// Create a new Angle from a floating point value in degrees |
29 |
| - pub fn from_degrees(degrees: f64) -> Self { |
30 |
| - Angle::from_radians(degrees / RADIAN_DEGREE_FACTOR) |
31 |
| - } |
32 |
| - |
33 |
| - /// Create a new Angle from a floating point value in radians |
34 |
| - pub fn from_radians(radians: f64) -> Self { |
35 |
| - Angle { radians: radians } |
36 |
| - } |
37 |
| - |
38 |
| - /// Convert this Angle to a floating point value in degrees |
39 |
| - pub fn as_degrees(&self) -> f64 { |
40 |
| - self.radians * RADIAN_DEGREE_FACTOR |
41 |
| - } |
42 |
| - |
43 |
| - /// Convert this Angle to a floating point value in radians |
44 |
| - pub fn as_radians(&self) -> f64 { |
45 |
| - self.radians |
46 |
| - } |
47 |
| -} |
48 |
| - |
49 |
| -impl Measurement for Angle { |
50 |
| - fn as_base_units(&self) -> f64 { |
51 |
| - self.radians |
52 |
| - } |
53 |
| - |
54 |
| - fn from_base_units(units: f64) -> Self { |
55 |
| - Self::from_radians(units) |
56 |
| - } |
57 |
| - |
58 |
| - fn as_base_units_name(&self) -> &'static str { |
59 |
| - "rad" |
60 |
| - } |
61 |
| -} |
62 |
| - |
63 |
| -implement_measurement! { Angle } |
64 |
| - |
65 |
| -#[cfg(test)] |
66 |
| -mod test { |
67 |
| - use angle::*; |
68 |
| - use test_utils::assert_almost_eq; |
69 |
| - |
70 |
| - #[test] |
71 |
| - fn radians() { |
72 |
| - let i1 = Angle::from_degrees(360.0); |
73 |
| - let r1 = i1.as_radians(); |
74 |
| - let i2 = Angle::from_radians(PI); |
75 |
| - let r2 = i2.as_degrees(); |
76 |
| - assert_almost_eq(r1, 2.0 * PI); |
77 |
| - assert_almost_eq(r2, 180.0); |
78 |
| - } |
79 |
| -} |
| 1 | +//! Types and constants for handling angles |
| 2 | +
|
| 3 | +use super::measurement::*; |
| 4 | + |
| 5 | +/// The 'Angle' struct can be used to deal with angles in a common way. |
| 6 | +/// |
| 7 | +/// # Example |
| 8 | +/// |
| 9 | +/// ``` |
| 10 | +/// use measurements::Angle; |
| 11 | +/// |
| 12 | +/// let whole_cake = Angle::from_degrees(360.0); |
| 13 | +/// let pieces = 6.0; |
| 14 | +/// let slice = whole_cake / pieces; |
| 15 | +/// println!("Each slice will be {} degrees", slice.as_degrees()); |
| 16 | +/// ``` |
| 17 | +#[derive(Copy, Clone, Debug)] |
| 18 | +pub struct Angle { |
| 19 | + radians: f64, |
| 20 | +} |
| 21 | + |
| 22 | +impl Angle { |
| 23 | + /// Create a new Angle from a floating point value in degrees |
| 24 | + pub fn from_degrees(degrees: f64) -> Self { |
| 25 | + Angle::from_radians(degrees.to_radians()) |
| 26 | + } |
| 27 | + |
| 28 | + /// Create a new Angle from a floating point value in radians |
| 29 | + pub fn from_radians(radians: f64) -> Self { |
| 30 | + Angle { radians: radians } |
| 31 | + } |
| 32 | + |
| 33 | + /// Convert this Angle to a floating point value in degrees |
| 34 | + pub fn as_degrees(&self) -> f64 { |
| 35 | + self.radians.to_degrees() |
| 36 | + } |
| 37 | + |
| 38 | + /// Convert this Angle to a floating point value in radians |
| 39 | + pub fn as_radians(&self) -> f64 { |
| 40 | + self.radians |
| 41 | + } |
| 42 | + |
| 43 | + /// Calculate the cosine of this angle |
| 44 | + pub fn cos(&self) -> f64 { |
| 45 | + self.radians.cos() |
| 46 | + } |
| 47 | + |
| 48 | + /// Calculate the sine of this angle |
| 49 | + pub fn sin(&self) -> f64 { |
| 50 | + self.radians.sin() |
| 51 | + } |
| 52 | + |
| 53 | + /// Calculate the sine and cosine of this angle |
| 54 | + pub fn sin_cos(&self) -> (f64, f64) { |
| 55 | + self.radians.sin_cos() |
| 56 | + } |
| 57 | + |
| 58 | + /// Calculate the tangent of this angle |
| 59 | + pub fn tan(&self) -> f64 { |
| 60 | + self.radians.tan() |
| 61 | + } |
| 62 | + |
| 63 | + /// Calculate the arcsine of a number |
| 64 | + pub fn asin(num: f64) -> Self { |
| 65 | + Angle::from_radians(num.asin()) |
| 66 | + } |
| 67 | + |
| 68 | + /// Calculate the arccosine of a number |
| 69 | + pub fn acos(num: f64) -> Self { |
| 70 | + Angle::from_radians(num.acos()) |
| 71 | + } |
| 72 | + |
| 73 | + /// Calculate the arctangent of a number |
| 74 | + pub fn atan(num: f64) -> Self { |
| 75 | + Angle::from_radians(num.atan()) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Measurement for Angle { |
| 80 | + fn as_base_units(&self) -> f64 { |
| 81 | + self.radians |
| 82 | + } |
| 83 | + |
| 84 | + fn from_base_units(units: f64) -> Self { |
| 85 | + Self::from_radians(units) |
| 86 | + } |
| 87 | + |
| 88 | + fn as_base_units_name(&self) -> &'static str { |
| 89 | + "rad" |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +implement_measurement! { Angle } |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod test { |
| 97 | + use angle::*; |
| 98 | + use std::f64::consts::PI; |
| 99 | + use test_utils::assert_almost_eq; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn radians() { |
| 103 | + let i1 = Angle::from_degrees(360.0); |
| 104 | + let r1 = i1.as_radians(); |
| 105 | + let i2 = Angle::from_radians(PI); |
| 106 | + let r2 = i2.as_degrees(); |
| 107 | + assert_almost_eq(r1, 2.0 * PI); |
| 108 | + assert_almost_eq(r2, 180.0); |
| 109 | + } |
| 110 | +} |
0 commit comments