1 use serde::{Serialize, Deserialize};
2
3 #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
4 pub struct Rect {
5 pub x1 : i32,
6 pub x2 : i32,
7 pub y1 : i32,
8 pub y2 : i32
9 }
10
11 impl Rect {
12 pub fn new(x:i32, y: i32, w:i32, h:i32) -> Rect {
13 Rect{x1:x, y1:y, x2:x+w, y2:y+h}
14 }
15
16 // Returns true if this overlaps with other
17 pub fn intersect(&self, other:&Rect) -> bool {
18 self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1
19 }
20
21 pub fn center(&self) -> (i32, i32) {
22 ((self.x1 + self.x2)/2, (self.y1 + self.y2)/2)
23 }
24 }