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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::{
    f32, hash,
    ops::{Add, Div, Mul, Sub},
};

use crate::CanonBits;

#[derive(Clone, Copy, Debug)]
pub struct Point {
    pub x: f32,
    pub y: f32,
}

impl Eq for Point {}

impl PartialEq for Point {
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }
}

impl hash::Hash for Point {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.x.to_canon_bits().hash(state);
        self.y.to_canon_bits().hash(state);
    }
}

impl Point {
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    pub fn to_array(self) -> [f32; 2] {
        [self.x, self.y]
    }
}

#[allow(clippy::many_single_char_names)]
fn approx_atan2(y: f32, x: f32) -> f32 {
    let x_abs = x.abs();
    let y_abs = y.abs();

    let a = x_abs.min(y_abs) / x_abs.max(y_abs);
    let s = a * a;
    let mut r = s.mul_add(-0.046_496_473, 0.159_314_22).mul_add(s, -0.327_622_77).mul_add(s * a, a);

    if y_abs > x_abs {
        r = f32::consts::FRAC_PI_2 - r;
    }

    if x < 0.0 {
        r = f32::consts::PI - r;
    }

    if y < 0.0 {
        r = -r;
    }

    r
}

// Restrict the Point functions visibility as we do not want to run into the business of delivering a linear algebra package.
impl Point {
    pub(crate) fn len(self) -> f32 {
        (self.x * self.x + self.y * self.y).sqrt()
    }

    pub(crate) fn angle(self) -> Option<f32> {
        (self.len() >= f32::EPSILON).then(|| approx_atan2(self.y, self.x))
    }
}

impl Add<Point> for Point {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        Self { x: self.x + other.x, y: self.y + other.y }
    }
}

impl Sub<Point> for Point {
    type Output = Self;

    #[inline]
    fn sub(self, other: Self) -> Self {
        Self { x: self.x - other.x, y: self.y - other.y }
    }
}

impl Mul<f32> for Point {
    type Output = Self;

    #[inline]
    fn mul(self, other: f32) -> Self {
        Self { x: self.x * other, y: self.y * other }
    }
}

impl Div<f32> for Point {
    type Output = Self;

    #[inline]
    fn div(self, other: f32) -> Self {
        Self { x: self.x / other, y: self.y / other }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, PI};

    #[test]
    fn add() {
        assert_eq!(
            Point { x: 32.0, y: 126.0 },
            Point { x: 13.0, y: 27.0 } + Point { x: 19.0, y: 99.0 }
        );
    }

    #[test]
    fn sub() {
        assert_eq!(
            Point { x: -6.0, y: -72.0 },
            Point { x: 13.0, y: 27.0 } - Point { x: 19.0, y: 99.0 }
        );
    }

    #[test]
    fn mul() {
        assert_eq!(Point { x: 3.0, y: 21.0 }, Point { x: 1.0, y: 7.0 } * 3.0);
    }

    #[test]
    fn div() {
        assert_eq!(Point { x: 1.0, y: 7.0 }, Point { x: 3.0, y: 21.0 } / 3.0);
    }

    #[test]
    fn angle() {
        assert_eq!(Some(0.0), Point { x: 1.0, y: 0.0 }.angle());
        assert_eq!(Some(0.0), Point { x: 1e10, y: 0.0 }.angle());
        assert_eq!(Some(PI), Point { x: -1.0, y: 0.0 }.angle());
        assert_eq!(Some(FRAC_PI_2), Point { x: 0.0, y: 1.0 }.angle());
        assert_eq!(Some(-FRAC_PI_2), Point { x: 0.0, y: -1.0 }.angle());
        assert!((FRAC_PI_4 - Point { x: 1.0, y: 1.0 }.angle().unwrap()).abs() < 1e-3);
        assert!((-FRAC_PI_4 - Point { x: 1.0, y: -1.0 }.angle().unwrap()).abs() < 1e-3);
    }
}