rect.rs 608 B

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