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
use crate::coord::Coord;
use crate::drawing::Renderable;
use crate::shapes::{DrawType, Shape};
use crate::Graphics;

#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Rect {
    start: Coord,
    end: Coord,
    width: isize,
    height: isize,
    draw_type: DrawType,
}

impl Rect {
    /// Creates a new Rect
    ///
    /// If necessary the points will be swapped so start_x < end_x and start_y < end_y
    pub fn new<P1: Into<Coord>, P2: Into<Coord>>(start: P1, end: P2, draw_type: DrawType) -> Self {
        let start = start.into();
        let end = end.into();
        let start_x = start.x.min(end.x);
        let end_x = start.x.max(end.x);
        let start_y = start.y.min(end.y);
        let end_y = start.y.max(end.y);
        let width = end_x - start_x;
        let height = end_y - start_y;
        Self {
            start: Coord::new(start_x, start_y),
            end: Coord::new(end_x, end_y),
            width,
            height,
            draw_type,
        }
    }

    pub fn width(&self) -> isize {
        self.width
    }

    pub fn height(&self) -> isize {
        self.width
    }

    pub fn topleft(&self) -> Coord {
        self.points()[0]
    }

    pub fn bottomright(&self) -> Coord {
        self.points()[1]
    }

    /// 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.start.x.min(other.start.x);
        let y1 = self.start.y.min(other.start.y);
        let x2 = self.end.x.max(other.end.x);
        let y2 = self.end.y.max(other.end.y);

        Rect::new((x1, y1), (x2, y2), self.draw_type)
    }

    /// 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.start.x.max(other.start.x);
        let y1 = self.start.y.max(other.start.y);
        let x2 = self.end.x.min(other.end.x);
        let y2 = self.end.y.min(other.end.y);
        if x1 < x2 && y1 < y2 {
            Rect::new((x1, y1), (x2, y2), self.draw_type)
        } else {
            self.clone()
        }
    }

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

        x1 < x2 && y1 < y2
    }
}

impl Shape for Rect {
    fn draw_type(&self) -> DrawType {
        self.draw_type
    }

    fn with_draw_type(&self, draw_type: DrawType) -> Self {
        Rect::new(self.start, self.end, draw_type)
    }

    fn translate_by<P: Into<Coord>>(&self, delta: P) -> Self {
        let delta = delta.into();
        let start = self.start + delta;
        let end = self.end + delta;
        Rect::new(start, end, self.draw_type)
    }

    fn move_to<P: Into<Coord>>(&self, xy: P) -> Self {
        let start = xy.into();
        let end = start + Coord::from(self.start.diff(self.end));
        Rect::new(start, end, self.draw_type)
    }

    fn points(&self) -> Vec<Coord> {
        vec![self.start, self.end]
    }
}

impl Renderable for Rect {
    fn render(&self, graphics: &mut Graphics) {
        match self.draw_type {
            DrawType::Stroke(color) => {
                for x in self.start.x..self.end.x {
                    graphics.update_pixel(x, self.start.y, color);
                    graphics.update_pixel(x, self.end.y, color);
                }
                for y in self.start.y..=self.end.y {
                    graphics.update_pixel(self.start.x, y, color);
                    graphics.update_pixel(self.end.x, y, color);
                }
            }
            DrawType::Fill(color) => {
                for x in self.start.x..self.end.x {
                    for y in self.start.y..self.end.y {
                        graphics.update_pixel(x, y, color);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::color::BLACK;

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

        let union = rect.union(&other);

        assert_eq!(
            union,
            Rect::new((10, 10), (25, 25), DrawType::Stroke(BLACK))
        );

        let rect = Rect::new((50, 1), (50, 100), DrawType::Stroke(BLACK));
        let other = Rect::new((1, 50), (100, 50), DrawType::Stroke(BLACK));

        let union = rect.union(&other);

        assert_eq!(
            union,
            Rect::new((1, 1), (100, 100), DrawType::Stroke(BLACK))
        );
    }

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

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

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

        let intersection = rect.intersect(&other);

        assert_eq!(
            intersection,
            Rect::new((15, 15), (20, 20), DrawType::Stroke(BLACK))
        );

        let rect = Rect::new((50, 1), (51, 100), DrawType::Stroke(BLACK));
        let other = Rect::new((1, 50), (100, 51), DrawType::Stroke(BLACK));

        let intersection = rect.intersect(&other);

        assert_eq!(
            intersection,
            Rect::new((50, 50), (51, 51), DrawType::Stroke(BLACK))
        );

        let rect = Rect::new((10, 10), (20, 20), DrawType::Stroke(BLACK));
        let doesnt_intersect = Rect::new((30, 30), (40, 40), DrawType::Stroke(BLACK));

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