rect.rs 512 B

123456789101112131415161718192021
  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{x1:x, y1:y, x2:x+w, y2:y+h}
  10. }
  11. // Returns true if this overlaps with other
  12. pub fn intersect(&self, other:&Rect) -> bool {
  13. self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1
  14. }
  15. pub fn center(&self) -> (i32, i32) {
  16. ((self.x1 + self.x2)/2, (self.y1 + self.y2)/2)
  17. }
  18. }