geo/algorithm/
convert_angle_unit.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
use geo_types::Coord;
use geo_types::CoordFloat;

use crate::{MapCoords, MapCoordsInPlace};

pub trait ToRadians<T: CoordFloat>:
    Sized + MapCoords<T, T, Output = Self> + MapCoordsInPlace<T>
{
    fn to_radians(&self) -> Self {
        self.map_coords(|Coord { x, y }| Coord {
            x: x.to_radians(),
            y: y.to_radians(),
        })
    }

    fn to_radians_in_place(&mut self) {
        self.map_coords_in_place(|Coord { x, y }| Coord {
            x: x.to_radians(),
            y: y.to_radians(),
        })
    }
}
impl<T: CoordFloat, G: MapCoords<T, T, Output = Self> + MapCoordsInPlace<T>> ToRadians<T> for G {}

pub trait ToDegrees<T: CoordFloat>:
    Sized + MapCoords<T, T, Output = Self> + MapCoordsInPlace<T>
{
    fn to_degrees(&self) -> Self {
        self.map_coords(|Coord { x, y }| Coord {
            x: x.to_degrees(),
            y: y.to_degrees(),
        })
    }

    fn to_degrees_in_place(&mut self) {
        self.map_coords_in_place(|Coord { x, y }| Coord {
            x: x.to_degrees(),
            y: y.to_degrees(),
        })
    }
}
impl<T: CoordFloat, G: MapCoords<T, T, Output = Self> + MapCoordsInPlace<T>> ToDegrees<T> for G {}

#[cfg(test)]
mod tests {
    use std::f64::consts::PI;

    use approx::assert_relative_eq;
    use geo_types::Line;

    use super::*;

    fn line_degrees_mock() -> Line {
        Line::new((90.0, 180.), (0., -90.))
    }

    fn line_radians_mock() -> Line {
        Line::new((PI / 2., PI), (0., -PI / 2.))
    }

    #[test]
    fn converts_to_radians() {
        assert_relative_eq!(line_radians_mock(), line_degrees_mock().to_radians())
    }

    #[test]
    fn converts_to_radians_in_place() {
        let mut line = line_degrees_mock();
        line.to_radians_in_place();
        assert_relative_eq!(line_radians_mock(), line)
    }

    #[test]
    fn converts_to_degrees() {
        assert_relative_eq!(line_degrees_mock(), line_radians_mock().to_degrees())
    }

    #[test]
    fn converts_to_degrees_in_place() {
        let mut line = line_radians_mock();
        line.to_degrees_in_place();
        assert_relative_eq!(line_degrees_mock(), line)
    }
}