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
use crate::geom::Point;
use crate::geom::Rect;
use crate::num::Num;

#[derive(Clone, Debug)]
pub struct RectIterator<N: Num = f32> {
    bottom_left: Point<N>,
    top_right: Point<N>,
    current: Point<N>,
}

impl<N: Num> RectIterator<N> {
    pub fn new(rect: Rect<N>) -> Self {
        Self {
            bottom_left: rect.bottom_left(),
            top_right: rect.top_right(),
            current: rect.bottom_left(),
        }
    }
}

impl<N: Num> Iterator for RectIterator<N> {
    type Item = Point<N>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.bottom_left.x() == self.top_right.x() {
            return None;
        }

        if self.bottom_left.y() == self.top_right.y() {
            return None;
        }

        if self.top_right.x() <= self.current.x() {
            self.current = Point(self.bottom_left.x(), self.current.y() + N::one());
        }

        if self.top_right.y() <= self.current.y() {
            return None;
        }

        let r = self.current;
        self.current += Point(N::one(), N::zero());
        Some(r)
    }
}

#[cfg(test)]
mod iterating {
    use super::*;
    use crate::geom::Size;
    use ::testcat::*;

    it!("should not iterate over empty rect", test_empty_rect);
    it!(
        "should not iterate over rect with zero width",
        test_rect_with_no_width
    );
    it!(
        "should not iterate over rect with zero height",
        test_rect_with_no_height
    );
    it!(
        "should return one value for a rect with 1x1 size",
        test_rect_with_size_one
    );
    it!(
        "should return one value for a rect with negative 1x1 size",
        test_rect_with_negative_size_one
    );
    it!(
        "should iterate over rect with width and height",
        test_rect_with_area
    );
    it!(
        "should iterate over rect with width and height, for usize",
        test_rect_with_area_usize
    );

    fn test_empty_rect() {
        let rect: Rect<usize> = Rect(Point(2, 3), Size(0, 0));
        let points: Vec<Point<usize>> = rect.into_iter().collect();

        assert_eq!(points, []);
    }

    fn test_rect_with_no_width() {
        let rect: Rect<usize> = Rect(Point(2, 3), Size(0, 4));
        let points: Vec<Point<usize>> = rect.into_iter().collect();

        assert_eq!(points, []);
    }

    fn test_rect_with_no_height() {
        let rect: Rect<usize> = Rect(Point(2, 3), Size(3, 0));
        let points: Vec<Point<usize>> = rect.into_iter().collect();

        assert_eq!(points, []);
    }

    fn test_rect_with_size_one() {
        let rect: Rect<usize> = Rect(Point(2, 3), Size(1, 1));
        let points: Vec<Point<usize>> = rect.into_iter().collect();

        assert_eq!(points, [Point(2, 3)]);
    }

    fn test_rect_with_negative_size_one() {
        let rect: Rect<isize> = Rect(Point(2, 3), Size(-1, -1));
        let points: Vec<Point<isize>> = rect.into_iter().collect();

        assert_eq!(points, [Point(1, 2)]);
    }

    fn test_rect_with_area() {
        let mut xs = vec![];
        let mut ys = vec![];

        for Point(x, y) in Rect(Point(2, 3), Size(3, 4)) {
            xs.push(x);
            ys.push(y);
        }

        #[rustfmt::skip]
        assert_eq!(xs, [
            2, 3, 4,
            2, 3, 4,
            2, 3, 4,
            2, 3, 4,
        ]);

        #[rustfmt::skip]
        assert_eq!(ys, [
            3, 3, 3,
            4, 4, 4,
            5, 5, 5,
            6, 6, 6,
        ]);
    }

    fn test_rect_with_area_usize() {
        let mut xs = vec![];
        let mut ys = vec![];

        for Point(x, y) in Rect(Point(0, 0), Size(2, 2)) {
            xs.push(x);
            ys.push(y);
        }

        #[rustfmt::skip]
        assert_eq!(xs, [
            0, 1,
            0, 1,
        ]);

        #[rustfmt::skip]
        assert_eq!(ys, [
            0, 0,
            1, 1,
        ]);
    }
}