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
use sdl2::rect::Rect; pub trait RectExt { fn touches(&self, other: Self) -> bool; fn overlaps_vertically(&self, other: Self) -> bool; fn horizontal_distance(&self, other: Self) -> i32; } impl RectExt for Rect { fn touches(&self, other: Self) -> bool { let mut r1 = *self; r1.w += 1; r1.h += 1; let mut r2 = other; r2.w += 1; r2.h += 1; r1.has_intersection(r2) } fn overlaps_vertically(&self, other: Self) -> bool { if self.y as i32 + self.h as i32 <= other.y as i32 { return false; } if other.y as i32 + other.h as i32 <= self.y as i32 { return false; } true } fn horizontal_distance(&self, other: Self) -> i32 { if self.right() < other.left() { self.right() - other.left() } else if other.right() < self.left() { self.left() - other.right() } else { 0 } } }