rect.rs 586 B

1234567891011121314151617181920212223242526
  1. pub struct Rect {
  2. pub x1: i32,
  3. pub x2: i32,
  4. pub y1: i32,
  5. pub y2: i32,
  6. }
  7. impl Rect {
  8. pub fn new(x: i32, y: i32, w: i32, h: i32) -> Rect {
  9. Rect {
  10. x1: x,
  11. y1: y,
  12. x2: x + w,
  13. 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. pub fn center(&self) -> (i32, i32) {
  21. ((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
  22. }
  23. }