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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Mathematical point on the 2D (x, y) plane.

use point2i::Point2i;
use point2u::Point2u;
use vector2f::Vector2f;

use std::ops::{Add, Sub};

#[cfg(all(windows, feature = "d2d"))]
use winapi::um::dcommon::D2D_POINT_2F;

/// Mathematical point on the 2D (x, y) plane.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[repr(C)]
pub struct Point2f {
    /// Horizontal component
    pub x: f32,
    /// Vertical component
    pub y: f32,
}

/// Mathematical origin point on the real number plane.
pub const ORIGIN: Point2f = Point2f::ORIGIN;

impl Point2f {
    /// Mathematical origin point on the real number plane.
    pub const ORIGIN: Point2f = Point2f { x: 0.0, y: 0.0 };

    /// Construct a point from the components
    #[inline]
    pub fn new(x: f32, y: f32) -> Self {
        Point2f { x, y }
    }

    /// Casts the point to an integer point. Will truncate integers; if
    /// another behavior is desired it should be manually performed on
    /// the values before calling this function.
    #[inline]
    pub fn to_i32(self) -> Point2i {
        Point2i {
            x: self.x as i32,
            y: self.y as i32,
        }
    }

    /// Casts the point to an unsigned integer point. Will truncate integers;
    /// if another behavior is desired it should be manually performed on
    /// the values before calling this function. Beware this function will
    /// not produce reasonable values if the current value is negative.
    #[inline]
    pub fn to_u32(self) -> Point2u {
        Point2u {
            x: self.x as u32,
            y: self.y as u32,
        }
    }

    /// Rounds the values in the point to the nearest integer, rounding away
    /// from zero in the half-way case.
    /// 
    /// See [f32::round][1]
    /// 
    /// [1]: https://doc.rust-lang.org/std/primitive.f32.html#method.round
    #[inline]
    pub fn rounded(self) -> Point2f {
        Point2f {
            x: self.x.round(),
            y: self.y.round(),
        }
    }

    /// Determines if the components of two points are less than `epsilon`
    /// distance from each other. Be wary that this does not check the actual
    /// distance, but a component-wise distance check. If you desire a more
    /// precise distance check, consider subtracting one point from the other
    /// and comparing the length(_sq) of the resulting vector.
    #[inline]
    pub fn is_approx_eq(self, other: impl Into<Point2f>, epsilon: f32) -> bool {
        let other = other.into();
        return (self.x - other.x).abs() <= epsilon && (self.y - other.y).abs() <= epsilon;
    }
}

impl<V> Add<V> for Point2f
where
    V: Into<Vector2f>,
{
    type Output = Point2f;

    #[inline]
    fn add(self, rhs: V) -> Point2f {
        let rhs = rhs.into();
        Point2f {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl Sub for Point2f {
    type Output = Vector2f;

    #[inline]
    fn sub(self, rhs: Point2f) -> Vector2f {
        Vector2f {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl Sub<(f32, f32)> for Point2f {
    type Output = Vector2f;

    #[inline]
    fn sub(self, rhs: (f32, f32)) -> Vector2f {
        Vector2f {
            x: self.x - rhs.0,
            y: self.y - rhs.1,
        }
    }
}

impl Sub<Point2f> for (f32, f32) {
    type Output = Vector2f;

    #[inline]
    fn sub(self, rhs: Point2f) -> Vector2f {
        Vector2f {
            x: self.0 - rhs.x,
            y: self.1 - rhs.y,
        }
    }
}

impl<V> Sub<V> for Point2f
where
    V: Into<Vector2f>,
{
    type Output = Point2f;

    #[inline]
    fn sub(self, rhs: V) -> Point2f {
        let rhs = rhs.into();
        Point2f {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl From<(f32, f32)> for Point2f {
    #[inline]
    fn from((x, y): (f32, f32)) -> Point2f {
        Point2f { x, y }
    }
}

impl From<[f32; 2]> for Point2f {
    #[inline]
    fn from(p: [f32; 2]) -> Point2f {
        Point2f { x: p[0], y: p[0] }
    }
}

impl From<Point2f> for [f32; 2] {
    #[inline]
    fn from(p: Point2f) -> [f32; 2] {
        [p.x, p.y]
    }
}

#[cfg(all(windows, feature = "d2d"))]
impl From<Point2f> for D2D_POINT_2F {
    #[inline]
    fn from(point: Point2f) -> D2D_POINT_2F {
        D2D_POINT_2F {
            x: point.x,
            y: point.y,
        }
    }
}

#[cfg(all(windows, feature = "d2d"))]
impl From<D2D_POINT_2F> for Point2f {
    #[inline]
    fn from(point: D2D_POINT_2F) -> Point2f {
        Point2f {
            x: point.x,
            y: point.y,
        }
    }
}

#[cfg(feature = "mint")]
impl From<Point2f> for mint::Point2<f32> {
    #[inline]
    fn from(p: Point2f) -> mint::Point2<f32> {
        mint::Point2 { x: p.x, y: p.y }
    }
}

#[cfg(feature = "mint")]
impl From<mint::Point2<f32>> for Point2f {
    #[inline]
    fn from(p: mint::Point2<f32>) -> Point2f {
        Point2f { x: p.x, y: p.y }
    }
}

#[cfg(all(test, windows, feature = "d2d"))]
#[test]
fn pt2f_d2d_bin_compat() {
    use std::mem::size_of_val;

    fn ptr_eq<T>(a: &T, b: &T) -> bool {
        (a as *const T) == (b as *const T)
    }

    let pt = Point2f::ORIGIN;
    let d2d = unsafe { &*((&pt) as *const _ as *const D2D_POINT_2F) };

    assert!(ptr_eq(&pt.x, &d2d.x));
    assert!(ptr_eq(&pt.y, &d2d.y));
    assert_eq!(size_of_val(&pt), size_of_val(d2d));
}