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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::math::{Point, Rect, UPoint, URect};


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

    pub const fn zero() -> Self { Point::new(0, 0) }
}

impl UPoint {
    pub const fn new(x: usize, y: usize) -> Self {
        UPoint { x, y }
    }

    pub const fn zero() -> Self { UPoint::new(0, 0) }
}

impl URect {
    pub const fn new(x1: usize, y1: usize, x2: usize, y2: usize) -> Self {
        URect { x1, y1, x2, y2 }
    }

    pub const fn from_point(point: UPoint, w: usize, h: usize) -> Self {
        URect {
            x1: point.x,
            y1: point.y,
            x2: point.x + w,
            y2: point.y + h,
        }
    }
}

impl Rect {
    pub const fn new(x1: isize, y1: isize, x2: isize, y2: isize) -> Self {
        Rect { x1, y1, x2, y2 }
    }

    pub const fn from_point(point: Point, w: isize, h: isize) -> Self {
        Rect {
            x1: point.x,
            y1: point.y,
            x2: point.x + w,
            y2: point.y + h,
        }
    }
}

impl Point {
    pub const fn is_positive(&self) -> bool {
        self.x >= 0 && self.y >= 0
    }
}

impl Rect {
    pub const fn width(&self) -> isize {
        self.x2 - self.x1
    }

    pub const fn height(&self) -> isize {
        self.y2 - self.y1
    }

    pub fn clip_to_urect(&self) -> URect {
        URect::new(
            self.x1.max(0) as usize,
            self.y1.max(0) as usize,
            self.x2.max(0) as usize,
            self.y2.max(0) as usize,
        )
    }

    pub fn move_to(&self, x: isize, y: isize) -> Rect {
        let w = self.width();
        let h = self.height();
        Rect::new(x, y, x + w, y + h)
    }

    pub fn translate(&self, x: isize, y: isize) -> Rect {
        Rect::new(self.x1 + x, self.y1 + y, self.x2 + x, self.y2 + y)
    }

    pub fn topleft(&self) -> Point {
        Point::new(self.x1, self.y1)
    }

    pub fn bottomright(&self) -> Point {
        Point::new(self.x2, self.y2)
    }

    /// Union this rect and another, the result will contain both rectangles
    /// Generally, this means the result will be bigger than self
    pub fn union(&self, other: &Rect) -> Rect {
        let x1 = self.x1.min(other.x1);
        let y1 = self.y1.min(other.y1);
        let x2 = self.x2.max(other.x2);
        let y2 = self.y2.max(other.y2);

        Rect::new(x1, y1, x2, y2)
    }

    /// Intersect this rect and another, the result will contain the area covered by both rectangles
    /// Generally, this means the result will be smaller than self
    ///
    /// # Returns
    ///
    /// self if rectangles do not intersect
    pub fn intersect(&self, other: &Rect) -> Rect {
        let x1 = self.x1.max(other.x1);
        let y1 = self.y1.max(other.y1);
        let x2 = self.x2.min(other.x2);
        let y2 = self.y2.min(other.y2);
        if x1 < x2 && y1 < y2 {
            Rect::new(x1, y1, x2, y2)
        } else {
            *self
        }
    }

    pub fn intersects(&self, other: &Rect) -> bool {
        let x1 = self.x1.max(other.x1);
        let y1 = self.y1.max(other.y1);
        let x2 = self.x2.min(other.x2);
        let y2 = self.y2.min(other.y2);

        x1 < x2 && y1 < y2
    }
}

impl URect {
    pub const fn width(&self) -> usize {
        self.x2 - self.x1
    }

    pub const fn height(&self) -> usize {
        self.y2 - self.y1
    }

    pub fn move_to(&self, x: usize, y: usize) -> URect {
        let w = self.width();
        let h = self.height();
        URect::new(x, y, x + w, y + h)
    }

    pub fn translate(&self, x: isize, y: isize) -> Result<URect, String> {
        if self.x1 as isize + x < 0 {
            return Err(format!(
                "Translated Rect {},{}-{},{} by {},{} which would set x below 0",
                self.x1, self.y1, self.x2, self.y2, x, y
            ));
        }
        if self.y1 as isize + y < 0 {
            return Err(format!(
                "Translated Rect {},{}-{},{} by {},{} which would set y below 0",
                self.x1, self.y1, self.x2, self.y2, x, y
            ));
        }
        Ok(URect::new(
            ((self.x1 as isize) + x) as usize,
            ((self.y1 as isize) + y) as usize,
            ((self.x2 as isize) + x) as usize,
            ((self.y2 as isize) + y) as usize,
        ))
    }

    pub fn topleft(&self) -> UPoint {
        UPoint::new(self.x1, self.y1)
    }

    pub fn bottomright(&self) -> UPoint {
        UPoint::new(self.x2, self.y2)
    }

    /// Union this rect and another, the result will contain both rectangles
    /// Generally, this means the result will be bigger than self
    pub fn union(&self, other: &URect) -> URect {
        let x1 = self.x1.min(other.x1);
        let y1 = self.y1.min(other.y1);
        let x2 = self.x2.max(other.x2);
        let y2 = self.y2.max(other.y2);

        URect::new(x1, y1, x2, y2)
    }

    /// Intersect this rect and another, the result will contain the area covered by both rectangles
    /// Generally, this means the result will be smaller than self
    ///
    /// # Returns
    ///
    /// self if rectangles do not intersect
    pub fn intersect(&self, other: &URect) -> URect {
        let x1 = self.x1.max(other.x1);
        let y1 = self.y1.max(other.y1);
        let x2 = self.x2.min(other.x2);
        let y2 = self.y2.min(other.y2);
        if x1 < x2 && y1 < y2 {
            URect::new(x1, y1, x2, y2)
        } else {
            *self
        }
    }

    pub fn intersects(&self, other: &URect) -> bool {
        let x1 = self.x1.max(other.x1);
        let y1 = self.y1.max(other.y1);
        let x2 = self.x2.min(other.x2);
        let y2 = self.y2.min(other.y2);

        x1 < x2 && y1 < y2
    }
}

#[cfg(test)]
mod test {
    mod rect {
        use crate::math::URect;

        #[test]
        fn width() {
            let rect = URect::new(10, 10, 20, 20);
            assert_eq!(rect.width(), 10);
        }

        #[test]
        fn height() {
            let rect = URect::new(10, 10, 20, 20);
            assert_eq!(rect.height(), 10);
        }

        #[test]
        fn translate() {
            let rect = URect::new(10, 10, 20, 20);
            let translated = rect.translate(5, 5).unwrap();
            assert_eq!(translated, URect::new(15, 15, 25, 25));

            let rect = URect::new(10, 10, 20, 20);
            let translated = rect.translate(-5, -5).unwrap();
            assert_eq!(translated, URect::new(5, 5, 15, 15));

            let rect = URect::new(5, 5, 10, 10);
            let err = rect.translate(-10, -10);
            assert!(err.is_err());
        }

        #[test]
        fn union() {
            let rect = URect::new(10, 10, 20, 20);
            let other = URect::new(15, 15, 25, 25);

            let union = rect.union(&other);

            assert_eq!(union, URect::new(10, 10, 25, 25));

            let rect = URect::new(50, 1, 50, 100);
            let other = URect::new(1, 50, 100, 50);

            let union = rect.union(&other);

            assert_eq!(union, URect::new(1, 1, 100, 100));
        }

        #[test]
        fn intersects() {
            let rect = URect::new(10, 10, 20, 20);
            let does_intersect = URect::new(15, 15, 25, 25);
            let doesnt_intersect = URect::new(30, 30, 40, 40);

            assert!(rect.intersects(&does_intersect));
            assert!(!rect.intersects(&doesnt_intersect));
        }

        #[test]
        fn intersect() {
            let rect = URect::new(10, 10, 20, 20);
            let other = URect::new(15, 15, 25, 25);

            let intersection = rect.intersect(&other);

            assert_eq!(intersection, URect::new(15, 15, 20, 20));

            let rect = URect::new(50, 1, 51, 100);
            let other = URect::new(1, 50, 100, 51);

            let intersection = rect.intersect(&other);

            assert_eq!(intersection, URect::new(50, 50, 51, 51));

            let rect = URect::new(10, 10, 20, 20);
            let doesnt_intersect = URect::new(30, 30, 40, 40);

            assert_eq!(rect.intersect(&doesnt_intersect), rect);
        }
    }
}