Finish implementing Rectangle

This commit is contained in:
expectocode 2019-12-31 00:29:05 +00:00
parent 08def2bb42
commit f5db656a0f
1 changed files with 30 additions and 0 deletions

View File

@ -129,6 +129,36 @@ impl Rectangle {
height, height,
} }
} }
fn top_left(&self) -> Point {
self.top_left
}
fn top_right(&self) -> Point {
Point::new(self.top_left.x() + self.width, self.top_left.y())
}
fn bottom_right(&self) -> Point {
Point::new(
self.top_left.x() + self.width,
self.top_left.y() + self.height,
)
}
fn bottom_left(&self) -> Point {
Point::new(self.top_left.x(), self.top_left.y() + self.height)
}
fn area(&self) -> u32 {
self.width * self.height
}
fn intersects(&self, other: &Rectangle) -> bool {
!(self.top_left().right_of(&other.top_right())
|| self.top_left().below(&other.bottom_left())
|| self.top_right().left_of(&other.top_left())
|| self.bottom_left().above(&other.top_left()))
}
} }
#[cfg(test)] #[cfg(test)]