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
//! Mathematical point on the 2D (x, y) plane.

use crate::point2f::Point2f;
use crate::point2u::Point2u;
use crate::vector2i::Vector2i;

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

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

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

impl Point2i {
    /// Mathematical origin point
    pub const ORIGIN: Point2i = Point2i { x: 0, y: 0 };

    /// Construct a point from its components
    #[inline]
    pub fn new(x: i32, y: i32) -> Point2i {
        Point2i { x, y }
    }

    /// Convert this value to a floating point
    #[inline]
    pub fn to_f32(self) -> Point2f {
        Point2f {
            x: self.x as f32,
            y: self.y as f32,
        }
    }

    /// Convert this value to an unsigned point. It is the caller's duty
    /// to ensure the values are not negative to avoid casting underflow.
    #[inline]
    pub fn to_u32(self) -> Point2u {
        Point2u {
            x: self.x as u32,
            y: self.y as u32,
        }
    }
}

impl<V> Add<V> for Point2i
where
    V: Into<Vector2i>,
{
    type Output = Point2i;

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

impl Add<Point2i> for Vector2i {
    type Output = Point2i;

    #[inline]
    fn add(self, rhs: Point2i) -> Point2i {
        rhs + self
    }
}

impl Add<(i32, i32)> for Vector2i {
    type Output = Point2i;

    #[inline]
    fn add(self, rhs: (i32, i32)) -> Point2i {
        Point2i::from(rhs) + self
    }
}

impl Sub for Point2i {
    type Output = Vector2i;

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

impl Sub<(i32, i32)> for Point2i {
    type Output = Vector2i;

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

impl<V> Sub<V> for Point2i
where
    V: Into<Vector2i>,
{
    type Output = Point2i;

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

impl<V> AddAssign<V> for Point2i
where
    Point2i: Add<V, Output = Point2i>,
{
    fn add_assign(&mut self, v: V) {
        *self = *self + v;
    }
}

impl<V> SubAssign<V> for Point2i
where
    Point2i: Sub<V, Output = Point2i>,
{
    fn sub_assign(&mut self, v: V) {
        *self = *self - v;
    }
}

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

impl From<Point2i> for (i32, i32) {
    #[inline]
    fn from(p: Point2i) -> (i32, i32) {
        (p.x, p.y)
    }
}

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

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

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

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

#[cfg(all(test, windows, feature = "d2d"))]
#[test]
fn pt2i_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 = Point2i::ORIGIN;
    let d2d = unsafe { &*((&pt) as *const _ as *const D2D_POINT_2L) };

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