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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use crate::math;
use crate::math::{Point2, Vector2};

/// A generic rectangle with bottom left and top right coordinates.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct Rect<T> {
    pub x1: T,
    pub y1: T,
    pub x2: T,
    pub y2: T,
}

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

    pub fn sized(x1: T, y1: T, w: T, h: T) -> Self
    where
        T: std::ops::Add<Output = T> + Copy,
    {
        Self::new(x1, y1, x1 + w, y1 + h)
    }

    pub fn zero() -> Self
    where
        T: math::Zero,
    {
        Self {
            x1: T::zero(),
            x2: T::zero(),
            y1: T::zero(),
            y2: T::zero(),
        }
    }

    pub fn origin(w: T, h: T) -> Self
    where
        T: math::Zero,
    {
        Self::new(T::zero(), T::zero(), w, h)
    }

    pub fn map<F, S>(self, f: F) -> Rect<S>
    where
        F: Fn(T) -> S,
    {
        Rect {
            x1: f(self.x1),
            x2: f(self.x2),
            y1: f(self.y1),
            y2: f(self.y2),
        }
    }

    pub fn scale(&self, x: T, y: T) -> Self
    where
        T: std::ops::Mul<Output = T> + Copy,
    {
        Self {
            x1: self.x1,
            y1: self.y1,
            x2: self.x2 * x,
            y2: self.y2 * y,
        }
    }

    /// Return the rectangle with a different origin.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::new(1, 1, 4, 4);
    /// assert_eq!(r.with_origin(0, 0), Rect::new(0, 0, 3, 3));
    /// ```
    pub fn with_origin(&self, x: T, y: T) -> Self
    where
        T: std::ops::Add<Output = T> + std::ops::Sub<Output = T> + Copy,
    {
        Self {
            x1: x,
            y1: y,
            x2: x + (self.x2 - self.x1),
            y2: y + (self.y2 - self.y1),
        }
    }

    /// Return the rectangle with a different size.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::new(1, 1, 4, 4);
    /// assert_eq!(r.with_size(9, 9), Rect::new(1, 1, 10, 10));
    /// ```
    pub fn with_size(&self, w: T, h: T) -> Self
    where
        T: std::ops::Add<Output = T> + std::ops::Sub<Output = T> + Copy,
    {
        Self {
            x1: self.x1,
            y1: self.y1,
            x2: self.x1 + w,
            y2: self.y1 + h,
        }
    }

    /// Return an expanded rectangle by a constant amount.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::new(0, 0, 3, 3);
    /// assert_eq!(r.expand(1, 1, 1, 1), Rect::new(-1, -1, 4, 4));
    ///
    /// let r = Rect::new(3, 3, 0, 0);
    /// assert_eq!(r.expand(1, 1, 1, 1), Rect::new(4, 4, -1, -1));
    ///
    /// let r = Rect::new(-1, 1, 1, -1);
    /// assert_eq!(r.expand(4, 4, 4, 4), Rect::new(-5, 5, 5, -5));
    /// ```
    pub fn expand(&self, x1: T, y1: T, x2: T, y2: T) -> Self
    where
        T: std::ops::Add<Output = T> + std::ops::Sub<Output = T> + PartialOrd + Copy,
    {
        let (x1, x2) = if self.x2 > self.x1 {
            (self.x1 - x1, self.x2 + x2)
        } else {
            (self.x1 + x1, self.x2 - x2)
        };
        let (y1, y2) = if self.y2 > self.y1 {
            (self.y1 - y1, self.y2 + y2)
        } else {
            (self.y1 + y1, self.y2 - y2)
        };
        Self { x1, x2, y1, y2 }
    }

    /// Return the rectangle flipped in the Y axis.
    pub fn flip_y(&self) -> Self
    where
        T: Copy,
    {
        Rect::new(self.x1, self.y2, self.x2, self.y1)
    }

    /// Return the rectangle flipped in the X axis.
    pub fn flip_x(&self) -> Self
    where
        T: Copy,
    {
        Rect::new(self.x2, self.y1, self.x1, self.y2)
    }

    /// Return the area of a rectangle.
    pub fn area(&self) -> T
    where
        T: Copy + std::ops::Sub<Output = T> + std::cmp::PartialOrd + std::ops::Mul<Output = T>,
    {
        self.width() * self.height()
    }

    pub fn is_empty(&self) -> bool
    where
        T: PartialEq,
    {
        self.x1 == self.x2 && self.y1 == self.y2
    }

    pub fn is_zero(&self) -> bool
    where
        T: math::Zero,
    {
        self.x1.is_zero() && self.x2.is_zero() && self.y1.is_zero() && self.y2.is_zero()
    }

    /// Return the width of the rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::new(0, 0, 3, 3);
    /// assert_eq!(r.width(), 3);
    /// ```
    pub fn width(&self) -> T
    where
        T: Copy + PartialOrd + std::ops::Sub<Output = T>,
    {
        if self.x1 < self.x2 {
            self.x2 - self.x1
        } else {
            self.x1 - self.x2
        }
    }

    /// Return the height of the rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::origin(-6, -6);
    /// assert_eq!(r.height(), 6);
    /// ```
    pub fn height(&self) -> T
    where
        T: Copy + PartialOrd + std::ops::Sub<Output = T>,
    {
        if self.y1 < self.y2 {
            self.y2 - self.y1
        } else {
            self.y1 - self.y2
        }
    }

    /// Return the minimum point of a rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    /// use rgx::math::Point2;
    ///
    /// let r = Rect::new(0, 0, 1, -1);
    /// assert_eq!(r.min(), Point2::new(0, -1));
    /// ```
    pub fn min(&self) -> Point2<T>
    where
        T: PartialOrd + Copy,
    {
        let x = if self.x1 < self.x2 { self.x1 } else { self.x2 };
        let y = if self.y1 < self.y2 { self.y1 } else { self.y2 };

        Point2::new(x, y)
    }

    /// Return the maximum point of a rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    /// use rgx::math::Point2;
    ///
    /// let r = Rect::origin(-1, 1);
    /// assert_eq!(r.max(), Point2::new(0, 1));
    /// ```
    pub fn max(&self) -> Point2<T>
    where
        T: PartialOrd + Copy,
    {
        let x = if self.x1 > self.x2 { self.x1 } else { self.x2 };
        let y = if self.y1 > self.y2 { self.y1 } else { self.y2 };

        Point2::new(x, y)
    }

    /// Return the center of the rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    /// use rgx::math::Point2;
    ///
    /// let r = Rect::origin(8, 8);
    /// assert_eq!(r.center(), Point2::new(4, 4));
    ///
    /// let r = Rect::new(0, 0, -8, -8);
    /// assert_eq!(r.center(), Point2::new(-4, -4));
    /// ```
    pub fn center(&self) -> Point2<T>
    where
        T: std::ops::Div<Output = T>
            + Copy
            + Ord
            + From<i16>
            + PartialOrd
            + math::Zero
            + std::ops::Neg<Output = T>
            + std::ops::Sub<Output = T>,
    {
        let r = self.abs();
        Point2::new(r.x1 + r.width() / 2.into(), r.y1 + r.height() / 2.into())
    }

    pub fn radius(&self) -> T
    where
        T: std::ops::Div<Output = T>
            + Copy
            + From<i16>
            + PartialOrd
            + math::Zero
            + std::ops::Neg<Output = T>
            + std::ops::Sub<Output = T>,
    {
        let w = self.width();
        let h = self.height();

        if w > h {
            w / 2.into()
        } else {
            h / 2.into()
        }
    }

    /// Check whether the given point is contained in the rectangle.
    ///
    /// ```
    /// use rgx::rect::Rect;
    /// use rgx::math::Point2;
    ///
    /// let r = Rect::origin(6, 6);
    /// assert!(r.contains(Point2::new(0, 0)));
    /// assert!(r.contains(Point2::new(3, 3)));
    /// assert!(!r.contains(Point2::new(6, 6)));
    ///
    /// let r = Rect::new(0, 0, -6, -6);
    /// assert!(r.contains(Point2::new(-3, -3)));
    /// ```
    pub fn contains(&self, p: Point2<T>) -> bool
    where
        T: Copy + PartialOrd,
    {
        let min = self.min();
        let max = self.max();
        p.x >= min.x && p.x < max.x && p.y >= min.y && p.y < max.y
    }

    pub fn intersects(&self, other: Rect<T>) -> bool
    where
        T: PartialOrd,
    {
        self.y2 > other.y1 && self.y1 < other.y2 && self.x1 < other.x2 && self.x2 > other.x1
    }

    /// Return the absolute rectangle.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let r = Rect::new(3, 3, 1, 1).abs();
    /// assert_eq!(r, Rect::new(1, 1, 3, 3));
    ///
    /// let r = Rect::new(-1, -1, 1, 1).abs();
    /// assert_eq!(r, Rect::new(-1, -1, 1, 1));
    /// ```
    pub fn abs(&self) -> Rect<T>
    where
        T: Ord + Copy,
    {
        Rect::new(
            T::min(self.x1, self.x2),
            T::min(self.y1, self.y2),
            T::max(self.x1, self.x2),
            T::max(self.y1, self.y2),
        )
    }

    /// Return the intersection between two rectangles.
    ///
    /// # Examples
    ///
    /// ```
    /// use rgx::rect::Rect;
    ///
    /// let other = Rect::new(0, 0, 3, 3);
    ///
    /// let r = Rect::new(1, 1, 6, 6);
    /// assert_eq!(r.intersection(other), Rect::new(1, 1, 3, 3));
    ///
    /// let r = Rect::new(1, 1, 2, 2);
    /// assert_eq!(r.intersection(other), Rect::new(1, 1, 2, 2));
    ///
    /// let r = Rect::new(-1, -1, 3, 3);
    /// assert_eq!(r.intersection(other), Rect::new(0, 0, 3, 3));
    ///
    /// let r = Rect::new(-1, -1, 4, 4);
    /// assert_eq!(r.intersection(other), other);
    ///
    /// let r = Rect::new(4, 4, 5, 5);
    /// assert!(r.intersection(other).is_empty());
    /// ```
    pub fn intersection(&self, other: Rect<T>) -> Self
    where
        T: Ord + Copy,
    {
        let x1 = T::max(self.x1, other.x1);
        let y1 = T::max(self.y1, other.y1);
        let x2 = T::min(self.x2, other.x2);
        let y2 = T::min(self.y2, other.y2);

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

impl<T> std::ops::Add<Vector2<T>> for Rect<T>
where
    T: std::ops::Add<Output = T> + Copy,
{
    type Output = Self;

    fn add(self, vec: Vector2<T>) -> Self {
        Self {
            x1: self.x1 + vec.x,
            y1: self.y1 + vec.y,
            x2: self.x2 + vec.x,
            y2: self.y2 + vec.y,
        }
    }
}

impl<T> std::ops::AddAssign<Vector2<T>> for Rect<T>
where
    T: std::ops::AddAssign<T> + Copy,
{
    fn add_assign(&mut self, vec: Vector2<T>) {
        self.x1 += vec.x;
        self.y1 += vec.y;
        self.x2 += vec.x;
        self.y2 += vec.y;
    }
}

impl<T> std::ops::Sub<Vector2<T>> for Rect<T>
where
    T: std::ops::Sub<Output = T> + Copy,
{
    type Output = Self;

    fn sub(self, vec: Vector2<T>) -> Self {
        Self {
            x1: self.x1 - vec.x,
            y1: self.y1 - vec.y,
            x2: self.x2 - vec.x,
            y2: self.y2 - vec.y,
        }
    }
}

impl<T> std::ops::SubAssign<Vector2<T>> for Rect<T>
where
    T: std::ops::SubAssign<T> + Copy,
{
    fn sub_assign(&mut self, vec: Vector2<T>) {
        self.x1 -= vec.x;
        self.y1 -= vec.y;
        self.x2 -= vec.x;
        self.y2 -= vec.y;
    }
}

impl<T> std::ops::Mul<T> for Rect<T>
where
    T: std::ops::Mul<Output = T> + Copy,
{
    type Output = Self;

    fn mul(self, s: T) -> Self {
        Self {
            x1: self.x1 * s,
            y1: self.y1 * s,
            x2: self.x2 * s,
            y2: self.y2 * s,
        }
    }
}