sark_grids 0.7.0

A set of grids for storing and accessing data in a grid-like way.
Documentation
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
use std::ops::Sub;

use glam::{ivec2, IVec2, UVec2};

use super::GridShape;
use crate::{GridPoint, GridSize, Pivot, PivotedPoint};

/// A rectangle of points on a 2d grid.
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
pub struct GridRect {
    /// The bottom-left most tile of the rect.
    pub pos: IVec2,
    pub size: UVec2,
}

impl GridRect {
    /// Create a [GridRect] from a position (bottom left tile) and a size.
    pub fn new(pos: impl GridPoint, size: impl GridSize) -> Self {
        GridRect {
            pos: pos.to_ivec2(),
            size: size.to_uvec2(),
        }
    }

    /// Create a [GridRect] with it's center at origin (`[0,0]`).
    pub fn center_origin(size: impl GridSize) -> Self {
        Self::from_center_size([0, 0], size)
    }

    /// Create a [GridRect] from two points in space. These points define
    /// the corners of the rect, meaning they will both be contained inside the
    /// rect.
    pub fn from_points(a: impl GridPoint, b: impl GridPoint) -> Self {
        let min = a.min(b);
        let max = a.max(b);

        let size = (max - min) + 1;
        GridRect {
            pos: min,
            size: size.as_uvec2(),
        }
    }

    /// Create a [GridRect] from a center position and rect size.
    pub fn from_center_size(center: impl GridPoint, size: impl GridSize) -> Self {
        let bl = center.to_ivec2() - size.to_ivec2() / 2;
        Self::new(bl, size.to_uvec2())
    }

    /// Returns a [GridRect] clipped by the bounds of the given [GridRect]
    pub fn clipped(&self, clipper: GridRect) -> GridRect {
        let [bmin, bmax] = [clipper.min(), clipper.max()];
        let [amin, amax] = [self.min(), self.max()];
        let max = amax.min(bmax);
        let min = amin.max(bmin);
        GridRect::from_points(min, max)
    }

    /// Returns a [GridRect] with it's position adjusted by the given amount
    pub fn translated(&self, pos: impl GridPoint) -> GridRect {
        GridRect::new(self.pos + pos.to_ivec2(), self.size)
    }

    /// Returns a [GridRect] with each side adjusted by the given delta.
    pub fn resized(&self, delta: impl GridPoint) -> GridRect {
        GridRect::from_points(self.min() - delta.to_ivec2(), self.max() + delta.to_ivec2())
    }

    /// Resizes the rect along a given pivot point.
    pub fn resize_from_pivot(&mut self, pivot: Pivot, amount: i32) {
        let p = match pivot {
            Pivot::TopLeft => self.top_left() + ivec2(-1, 1) * amount,
            Pivot::TopCenter => self.top_left() + ivec2(0, 1) * amount,
            Pivot::TopRight => self.top_right() + ivec2(1, 1) * amount,
            Pivot::LeftCenter => self.top_left() + ivec2(-1, 0) * amount,
            Pivot::RightCenter => self.top_right() + ivec2(1, 0) * amount,
            Pivot::BottomLeft => self.bottom_left() + ivec2(-1, -1) * amount,
            Pivot::BottomCenter => self.bottom_left() + ivec2(0, -1) * amount,
            Pivot::BottomRight => self.bottom_right() + ivec2(1, -1) * amount,
            Pivot::Center => self.center(),
        };
        self.envelope_point(p);
    }

    /// Returns a [GridRect] with both rects contained in it.
    pub fn merged(&self, mut other: GridRect) -> GridRect {
        other.envelope_point(self.min());
        other.envelope_point(self.max());
        other
    }

    /// Adjusts a single corner of the rect to contain the given point.
    pub fn envelope_point(&mut self, point: impl GridPoint) {
        let point = point.to_ivec2();
        let min = self.min().min(point);
        let max = self.max().max(point);
        *self = GridRect::from_points(min, max);
    }

    /// Adjust this rect so the given rect is entirely contained within it.
    pub fn merge(&mut self, rect: GridRect) {
        let [min, max] = [rect.min(), rect.max()];
        self.envelope_point(min);
        self.envelope_point(max);
    }

    /// The center position of the rect.
    pub fn center(&self) -> IVec2 {
        self.pos + self.size.as_ivec2() / 2
    }

    /// The y position of the top row of the rect.
    pub fn top(&self) -> i32 {
        self.max().y
    }

    /// The y position of the bottom row of the rect.
    pub fn bottom(&self) -> i32 {
        self.min().y
    }

    /// The x position of the left column of the rect.
    pub fn left(&self) -> i32 {
        self.min().x
    }

    /// The x position of the right column of the rect.
    pub fn right(&self) -> i32 {
        self.max().x
    }

    /// The 2d position of the top left tile of the rect.
    pub fn top_left(&self) -> IVec2 {
        [self.left(), self.top()].into()
    }

    /// The 2d position of the top right tile of the rect.
    pub fn top_right(&self) -> IVec2 {
        self.max()
    }

    /// The 2d position of the bottom left tile of the rect.
    pub fn bottom_left(&self) -> IVec2 {
        self.pos
    }

    /// The 2d position of the bottom right tile of the rect.
    pub fn bottom_right(&self) -> IVec2 {
        [self.right(), self.bottom()].into()
    }

    pub fn width(&self) -> usize {
        self.size.x as usize
    }

    pub fn height(&self) -> usize {
        self.size.y as usize
    }

    /// Bottom left position of the rect.
    pub fn min(&self) -> IVec2 {
        self.pos
    }

    /// Top right position of the rect.
    pub fn max(&self) -> IVec2 {
        self.pos + self.size.as_ivec2().sub(1)
    }

    /// Index of the bottom row of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    pub fn bottom_index(&self) -> usize {
        0
    }

    /// Index of the top row of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    pub fn top_index(&self) -> usize {
        self.height().sub(1)
    }

    /// Index of the left column of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    pub fn left_index(&self) -> usize {
        0
    }

    /// The index of the right most column of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    pub fn right_index(&self) -> usize {
        self.width().sub(1)
    }

    /// Iterate over each point of the rect.
    pub fn iter_points(&self) -> impl DoubleEndedIterator<Item = IVec2> + ExactSizeIterator {
        GridRectIter::new(*self)
    }

    /// Iterate over the tile positions of a single column of the rect.
    pub fn iter_column(
        &self,
        col: usize,
    ) -> impl DoubleEndedIterator<Item = IVec2> + ExactSizeIterator {
        self.iter_points().skip(col).step_by(self.width())
    }

    /// Iterate over the tile positions of a single row of the rect.
    pub fn iter_row(
        &self,
        row: usize,
    ) -> impl DoubleEndedIterator<Item = IVec2> + ExactSizeIterator {
        self.iter_points()
            .skip(row * self.width())
            .take(self.width())
    }

    /// Iterate over all the tile positions of the border of the rect in clockwise
    /// order, starting from the bottom left.
    pub fn iter_border(&self) -> impl DoubleEndedIterator<Item = IVec2> {
        let left = self.iter_column(0);
        let top = self
            .iter_row(self.top_index())
            .skip(1)
            .take(self.width().sub(2));
        let right = self.iter_column(self.right_index()).rev();
        let bottom = self
            .iter_row(self.bottom_index())
            .rev()
            .skip(1)
            .take(self.width().sub(2));
        left.chain(top).chain(right).chain(bottom)
    }

    /// Retrieve the position of the tile at the given pivot point on the rect.
    pub fn pivot_point(&self, pivot: Pivot) -> IVec2 {
        self.bottom_left() + pivot.pivot_position(self.size)
    }

    /// Retrieve a transformed point from a local pivot position.
    pub fn pivoted_point(&self, xy: impl Into<PivotedPoint>) -> IVec2 {
        let xy: PivotedPoint = xy.into();
        let pivot = xy.pivot.unwrap_or(Pivot::BottomLeft);
        let xy = xy.point;
        let origin = self.pivot_point(pivot);
        origin + (xy * pivot.axis())
    }

    /// Check if a given point lies inside the rect
    #[inline]
    pub fn contains_point(&self, p: impl GridPoint) -> bool {
        let p = p.to_ivec2();
        !(p.cmplt(self.min()).any() || p.cmpgt(self.max()).any())
    }

    /// Returns true if the given rect is entirely contained within this one.
    #[inline]
    pub fn contains_rect(&self, other: GridRect) -> bool {
        other.min().cmpge(self.min()).all() && other.max().cmple(self.max()).all()
    }

    /// Check if any part of a rect overlaps another.
    #[inline]
    pub fn overlaps_rect(&self, other: GridRect) -> bool {
        self.left() <= other.right()
            && other.left() <= self.right()
            && self.bottom() <= other.top()
            && other.bottom() <= self.top()
    }
}

impl SizedGrid for GridRect {
    fn size(&self) -> glam::UVec2 {
        self.size
    }
}

impl PositionedGrid for GridRect {
    fn pos(&self) -> IVec2 {
        self.pos
    }
}

/// An iterator over the 2d grid points of a [GridRect].
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct GridRectIter {
    origin: IVec2,
    size: UVec2,
    head: IVec2,
    tail: IVec2,
}

impl GridRectIter {
    pub fn new(rect: GridRect) -> Self {
        Self {
            origin: rect.pos,
            size: rect.size,
            head: IVec2::ZERO,
            tail: rect.size.as_ivec2().sub(1),
        }
    }

    pub fn can_iterate(&self) -> bool {
        self.head.y < self.tail.y || (self.head.y == self.tail.y && self.head.x <= self.tail.x)
    }
}

impl Iterator for GridRectIter {
    type Item = IVec2;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.can_iterate() {
            return None;
        }
        let size = self.size.as_ivec2();
        let head = &mut self.head;

        let ret = self.origin + *head;
        head.x += 1;
        if head.x >= size.x {
            head.x = 0;
            head.y += 1;
        }

        Some(ret)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if !self.can_iterate() {
            return (0, Some(0));
        }
        let count = self
            .tail
            .as_index(self.size)
            .saturating_sub(self.head.as_index(self.size))
            + 1;
        (count, Some(count))
    }
}

impl DoubleEndedIterator for GridRectIter {
    fn next_back(&mut self) -> Option<Self::Item> {
        let size = self.size.as_ivec2();
        let tail = &mut self.tail;
        let head = self.head;

        if tail.y < head.y || (tail.y == head.y && tail.x < head.x) {
            None
        } else {
            let ret = self.origin + *tail;
            tail.x -= 1;
            if tail.x < 0 {
                tail.x = size.x.sub(1);
                tail.y -= 1;
            }

            Some(ret)
        }
    }
}

impl ExactSizeIterator for GridRectIter {}

impl IntoIterator for GridRect {
    type Item = IVec2;

    type IntoIter = GridRectIter;

    fn into_iter(self) -> Self::IntoIter {
        GridRectIter::new(self)
    }
}

impl std::fmt::Display for GridRect {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "GridRect {{
    MinMax {:?}, {:?}
    Center {:?}
    Size   {:?}
}}",
            self.min().to_array(),
            self.max().to_array(),
            self.center().to_array(),
            self.size.to_array()
        )
    }
}

impl GridShape for GridRect {
    fn iter(&self) -> super::GridShapeIterator {
        super::GridShapeIterator::Rect(self.into_iter())
    }

    fn pos(&self) -> IVec2 {
        self.pos
    }

    fn set_pos(&mut self, pos: IVec2) {
        self.pos = pos;
    }

    fn bounds(&self) -> GridRect {
        *self
    }
}

/// A rectangular grid with a defined size.
pub trait SizedGrid {
    fn size(&self) -> UVec2;

    fn width(&self) -> usize {
        self.size().x as usize
    }

    fn height(&self) -> usize {
        self.size().y as usize
    }

    fn tile_count(&self) -> usize {
        self.width() * self.height()
    }

    fn in_bounds(&self, p: impl Into<PivotedPoint>) -> bool {
        let p: IVec2 = p.into().calculate(self.size());
        p.cmpge(IVec2::ZERO).all() && p.cmplt(self.size().as_ivec2()).all()
    }

    /// Transform a local 2d grid position to a 1d array index.
    #[inline]
    fn transform_lti(&self, pos: impl Into<PivotedPoint>) -> usize {
        let pos: IVec2 = pos.into().calculate(self.size());
        debug_assert!(
            self.in_bounds(pos),
            "Attempting to create 1d index from out of bounds position {} in grid sized {}",
            pos,
            self.size()
        );
        pos.as_index(self.size())
    }

    /// Transform an 1d array index to a local 2d grid position.
    fn transform_itl(&self, i: usize) -> IVec2 {
        let x = i % self.width();
        let y = i / self.width();
        IVec2::new(x as i32, y as i32)
    }

    /// Attempt to transform a 2d grid position to a 1d array index, returning
    /// [None] if the position is out of bounds.
    fn try_transform_lti(&self, pos: impl Into<PivotedPoint>) -> Option<usize> {
        let pos: IVec2 = pos.into().calculate(self.size());
        pos.get_index(self.size())
    }

    /// Attempt to transform an 1d array index to a local 2d grid position.
    /// Returns [None] if the index is out of bounds.
    fn try_transform_itl(&self, i: usize) -> Option<IVec2> {
        if i >= self.tile_count() {
            return None;
        }
        let x = i % self.width();
        let y = i / self.width();
        Some(IVec2::new(x as i32, y as i32))
    }

    /// Index of the bottom row of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    fn bottom_index(&self) -> usize {
        0
    }

    /// Index of the top row of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    fn top_index(&self) -> usize {
        self.height().sub(1)
    }

    /// Index of the left column of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    fn left_index(&self) -> usize {
        0
    }

    /// The index of the right most column of the rect.
    ///
    /// The "index" is independent of the rect's position and goes from
    /// `0` to `size-1`.
    fn right_index(&self) -> usize {
        self.width().sub(1)
    }

    fn grid_bounds(&self) -> GridRect {
        GridRect::new([0, 0], self.size())
    }

    fn iter_grid_points(&self) -> GridRectIter {
        GridRectIter::new(self.grid_bounds())
    }
}

/// A rectangular grid with a defined size and world position.
pub trait PositionedGrid: SizedGrid {
    /// The position (bottom-left tile) of the grid in world space.
    fn pos(&self) -> IVec2;

    /// The center tile of the grid.
    fn center(&self) -> IVec2 {
        self.pos() + self.size().as_ivec2() / 2
    }

    /// The x position of the right-most grid column.
    fn right(&self) -> i32 {
        self.pos().x + self.width() as i32 - 1
    }

    /// The y position of the top grid row.
    fn top(&self) -> i32 {
        self.pos().y + self.height() as i32 - 1
    }

    /// The x position of the left-most grid column.
    fn left(&self) -> i32 {
        self.pos().x
    }

    /// The y position of the bottom grid row.
    fn bottom(&self) -> i32 {
        self.pos().y
    }

    /// The grid position of the top-left grid tile.
    fn top_left(&self) -> IVec2 {
        IVec2::new(self.left(), self.top())
    }

    /// The grid position of the top-right grid tile.
    fn top_right(&self) -> IVec2 {
        IVec2::new(self.right(), self.top())
    }

    /// The grid position of the bottom-left grid tile.
    fn bottom_left(&self) -> IVec2 {
        IVec2::new(self.left(), self.bottom())
    }

    /// The grid position of the bottom-right grid tile.
    fn bottom_right(&self) -> IVec2 {
        IVec2::new(self.right(), self.bottom())
    }

    /// The grid position of the bottom-left grid tile.
    fn min(&self) -> IVec2 {
        self.pos()
    }

    /// The grid position of the top-right grid tile.
    fn max(&self) -> IVec2 {
        self.pos() + self.size().as_ivec2() - 1
    }

    fn contains_point(&self, p: impl GridPoint) -> bool {
        let p = p.to_ivec2();
        !(p.cmplt(self.min()).any() || p.cmpgt(self.max()).any())
    }

    /// Check if another [PositionedGrid] is entirely contained within this one.
    fn contains_rect(&self, other: impl PositionedGrid) -> bool {
        other.min().cmpge(self.min()).all() && other.max().cmple(self.max()).all()
    }

    /// Check if any tile of this [PositionedGrid] overlaps with another.
    fn overlaps(&self, other: impl PositionedGrid) -> bool {
        self.left() <= other.right()
            && other.left() <= self.right()
            && self.bottom() <= other.top()
            && other.bottom() <= self.top()
    }

    /// Iterate over each point of the [GridRect] starting from the bottom left
    /// and increasing right and up.
    fn iter_rect_points(&self) -> GridRectIter {
        GridRectIter::new(GridRect::new(self.pos(), self.size()))
    }

    /// Iterate over the grid points of a single column of the rect.
    fn iter_rect_column(
        &self,
        col: usize,
    ) -> impl DoubleEndedIterator<Item = IVec2> + ExactSizeIterator {
        self.iter_grid_points().skip(col).step_by(self.width())
    }

    /// Iterate over a the grid points of single row.
    fn iter_rect_row(
        &self,
        row: usize,
    ) -> impl DoubleEndedIterator<Item = IVec2> + ExactSizeIterator {
        self.iter_grid_points()
            .skip(row * self.width())
            .take(self.width())
    }

    /// Iterate over all the border tiles of the [GridRect] in clockwise order,
    /// starting from the bottom left.
    fn iter_rect_border(&self) -> impl DoubleEndedIterator<Item = IVec2> {
        let left = self.iter_rect_column(0);
        let top = self
            .iter_rect_row(self.top_index())
            .skip(1)
            .take(self.width().sub(2));
        let right = self.iter_rect_column(self.right_index()).rev();
        let bottom = self
            .iter_rect_row(self.bottom_index())
            .rev()
            .skip(1)
            .take(self.width().sub(2));
        left.chain(top).chain(right).chain(bottom)
    }

    /// Retrieve the position of the tile at the given pivot point on the grid.
    fn pivot_point(&self, pivot: Pivot) -> IVec2 {
        self.bottom_left() + pivot.pivot_position(self.size())
    }

    /// Retrieve a point in the grid from the perspective of the given pivot.
    fn pivoted_point(&self, point: impl Into<PivotedPoint>) -> IVec2 {
        let point: PivotedPoint = point.into();
        let pivot = point.pivot.unwrap_or(Pivot::BottomLeft);
        let point = point.point;
        let origin = self.pivot_point(pivot);
        origin + (point * pivot.axis())
    }

    /// Transform a world point to this grid's local coordinates
    fn transform_wtl(&self, pos: impl GridPoint) -> IVec2 {
        pos.to_ivec2() - self.min()
    }
}

#[cfg(test)]
mod tests {
    use glam::IVec2;

    use crate::{geometry::grid_rect::PositionedGrid, util::Canvas, GridPoint, Pivot};

    use super::{GridRect, SizedGrid};

    #[test]
    fn rect_min_max() {
        let rect = GridRect::from_points([1, 1], [3, 3]);
        assert_eq!([1, 1], rect.min().to_array());
        assert_eq!([3, 3], rect.max().to_array());

        let rect = GridRect::from_points([0, 0], [3, 3]);
        assert_eq!([0, 0], rect.min().to_array());
        assert_eq!([3, 3], rect.max().to_array());

        let rect = GridRect::from_points([-1, -1], [4, 4]);
        assert_eq!([-1, -1], rect.min().to_array());
        assert_eq!([4, 4], rect.max().to_array());

        let rect = GridRect::from_points([-5, -5], [3, 3]);
        assert_eq!([-5, -5], rect.min().to_array());
        assert_eq!([3, 3], rect.max().to_array());

        let rect = GridRect::from_points([6, 6], [7, 7]);
        assert_eq!([6, 6], rect.min().to_array());
        assert_eq!([7, 7], rect.max().to_array());
    }

    #[test]
    fn contains_point() {
        let rect = GridRect::center_origin([5, 5]);
        assert!(rect.contains_point([-2, -2]));
        assert!(rect.contains_point([2, 2]));
        assert!(!rect.contains_point([3, 3]));
        assert!(!rect.contains_point([-3, -3]));
    }

    #[test]
    fn from_bl() {
        let rect = GridRect::center_origin([5, 5]);
        let rect2 = GridRect::new([-2, -2], [5, 5]);

        assert_eq!(rect, rect2);
    }

    #[test]
    fn overlap() {
        let a = GridRect::new([-1, -1], [3, 3]);
        let b = GridRect::new([1, 1], [3, 3]);
        let c = GridRect::new([3, 3], [3, 3]);
        assert!(a.overlaps(b));
        assert!(b.overlaps(a));
        assert!(b.overlaps(c));
        assert!(c.overlaps(b));
        assert!(!a.overlaps(c));
        assert!(!c.overlaps(a));

        assert!(a.overlaps(a));
        assert!(b.overlaps(b));
        assert!(c.overlaps(c));

        let a = GridRect::new([-2, -2], [4, 4]);
        let b = GridRect::new([1, 1], [4, 4]);
        let c = GridRect::new([4, 4], [4, 4]);
        assert!(a.overlaps(b));
        assert!(b.overlaps(a));
        assert!(b.overlaps(c));
        assert!(c.overlaps(b));
        assert!(!a.overlaps(c));
        assert!(!c.overlaps(a));

        assert!(a.overlaps(a));
        assert!(b.overlaps(b));
        assert!(c.overlaps(c));
    }

    #[test]
    #[ignore]
    fn big() {
        let rect = GridRect::center_origin([30, 30]);
        let mut canvas = Canvas::new([32, 32]);

        for p in rect {
            canvas.put(p, '*');
        }
        canvas.print();
    }

    #[test]
    fn envelope_point() {
        let mut rect = GridRect::from_points([1, 1], [3, 3]);
        rect.envelope_point([0, 0]);
        assert_eq!([0, 0], rect.min().to_array());
        assert_eq!([3, 3], rect.max().to_array());
        assert_eq!(4, rect.width());

        rect.envelope_point([4, 3]);
        assert_eq!([0, 0], rect.min().to_array());
        assert_eq!([4, 3], rect.max().to_array());
        assert_eq!(4, rect.height());

        rect.envelope_point([0, 0]);
        assert_eq!([0, 0], rect.min().to_array());
        assert_eq!([4, 3], rect.max().to_array());
    }

    #[test]
    fn envelope_rect() {
        let mut rect = GridRect::from_points([1, 1], [3, 3]);
        rect.merge(GridRect::from_points([4, 4], [8, 8]));
        assert_eq!([8, 8], rect.max().to_array());
        assert_eq!([1, 1], rect.min().to_array());

        rect.merge(GridRect::from_points([-10, -10], [4, 8]));
        assert_eq!([-10, -10], rect.min().to_array());
    }

    #[test]
    fn pivot_corners() {
        let rect = GridRect::from_points([0, 0], [5, 5]);
        assert_eq!([0, 0], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([0, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, 0], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([0, 0], [6, 6]);
        assert_eq!([0, 0], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([0, 6], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([6, 6], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([6, 0], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([-5, -5], [5, 5]);
        assert_eq!([-5, -5], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([-5, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, -5], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([-4, -4], [5, 5]);
        assert_eq!([-4, -4], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([-4, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, -4], rect.pivot_point(Pivot::BottomRight).to_array());
    }

    #[test]
    fn iter_grid_points() {
        let rect = GridRect::new([0, 0], [3, 3]);
        let points: Vec<IVec2> = rect.iter_grid_points().collect();
        assert_eq!([0, 0], points[0].to_array());
        assert_eq!([1, 0], points[1].to_array());
        assert_eq!([2, 0], points[2].to_array());
        assert_eq!([0, 1], points[3].to_array());
        assert_eq!([1, 1], points[4].to_array());
        assert_eq!([2, 1], points[5].to_array());
        assert_eq!([0, 2], points[6].to_array());
        assert_eq!([1, 2], points[7].to_array());
        assert_eq!([2, 2], points[8].to_array());
    }

    #[test]
    fn iter_border() {
        let rect = GridRect::from_points([0, 0], [5, 5]);
        let points: Vec<_> = rect.iter_rect_border().map(|v| v.to_array()).collect();

        assert_eq!([0, 0], points[0]);
        assert_eq!([0, 1], points[1]);
        assert_eq!([0, 2], points[2]);
        assert_eq!([0, 3], points[3]);
        assert_eq!([0, 4], points[4]);
        assert_eq!([0, 5], points[5]);
        assert_eq!([1, 5], points[6]);
        assert_eq!([2, 5], points[7]);
        assert_eq!([3, 5], points[8]);
        assert_eq!([4, 5], points[9]);
        assert_eq!([5, 5], points[10]);
        assert_eq!([5, 4], points[11]);
        assert_eq!([5, 3], points[12]);
        assert_eq!([5, 2], points[13]);
        assert_eq!([5, 1], points[14]);
        assert_eq!([5, 0], points[15]);
        assert_eq!([4, 0], points[16]);
        assert_eq!([3, 0], points[17]);
        assert_eq!([2, 0], points[18]);
        assert_eq!([1, 0], points[19]);
    }

    #[test]
    fn clipped() {
        let a = GridRect::from_points([4, 5], [10, 13]);
        let b = GridRect::from_points([9, 10], [13, 15]);
        let c = GridRect::from_points([1, 2], [7, 10]);

        let clipped = b.clipped(a);
        assert_eq!([9, 10], clipped.min().to_array());
        assert_eq!([10, 13], clipped.max().to_array());

        let clipped = a.clipped(c);
        assert_eq!([4, 5], clipped.min().to_array());
        assert_eq!([7, 10], clipped.max().to_array());
    }

    #[test]
    fn pivot_point() {
        let rect = GridRect::from_points([0, 0], [5, 5]);
        assert_eq!([0, 0], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([0, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, 0], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([0, 0], [6, 6]);
        assert_eq!([0, 0], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([0, 6], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([6, 6], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([6, 0], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([-5, -5], [5, 5]);
        assert_eq!([-5, -5], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([-5, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, -5], rect.pivot_point(Pivot::BottomRight).to_array());

        let rect = GridRect::from_points([-4, -4], [5, 5]);
        assert_eq!([-4, -4], rect.pivot_point(Pivot::BottomLeft).to_array());
        assert_eq!([-4, 5], rect.pivot_point(Pivot::TopLeft).to_array());
        assert_eq!([5, 5], rect.pivot_point(Pivot::TopRight).to_array());
        assert_eq!([5, -4], rect.pivot_point(Pivot::BottomRight).to_array());
    }

    #[test]
    fn pivoted_point() {
        let rect = GridRect::from_points([0, 0], [5, 5]);
        assert_eq!(
            [1, 1],
            rect.pivoted_point([1, 1].pivot(Pivot::BottomLeft))
                .to_array()
        );
        assert_eq!(
            [1, 4],
            rect.pivoted_point([1, 1].pivot(Pivot::TopLeft)).to_array()
        );
        assert_eq!(
            [4, 4],
            rect.pivoted_point([1, 1].pivot(Pivot::TopRight)).to_array()
        );
        assert_eq!(
            [4, 1],
            rect.pivoted_point([1, 1].pivot(Pivot::BottomRight))
                .to_array()
        );

        let rect = GridRect::from_points([-5, -4], [3, 3]);
        assert_eq!(
            [-4, -3],
            rect.pivoted_point([1, 1].pivot(Pivot::BottomLeft))
                .to_array()
        );
        assert_eq!(
            [-4, 2],
            rect.pivoted_point([1, 1].pivot(Pivot::TopLeft)).to_array()
        );
        assert_eq!(
            [2, 2],
            rect.pivoted_point([1, 1].pivot(Pivot::TopRight)).to_array()
        );
        assert_eq!(
            [2, -3],
            rect.pivoted_point([1, 1].pivot(Pivot::BottomRight))
                .to_array()
        );
    }
}