From f5db656a0fa172c3cb89dc70ec2f73f2c189faa6 Mon Sep 17 00:00:00 2001 From: expectocode Date: Tue, 31 Dec 2019 00:29:05 +0000 Subject: [PATCH] Finish implementing Rectangle --- src/geometry.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index cde9154..e0d1033 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -129,6 +129,36 @@ impl Rectangle { 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)]