From 12a50af544b7224921ffa26aa0ea7431918490e7 Mon Sep 17 00:00:00 2001 From: expectocode Date: Tue, 31 Dec 2019 00:21:43 +0000 Subject: [PATCH] start implementing Rectangle --- src/geometry.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/geometry.rs b/src/geometry.rs index 8bab225..d397cb3 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -76,6 +76,67 @@ impl PartialEq for Point { impl Eq for Point {} +impl Rectangle { + fn from_corner_width_height(corner: Point, width: u32, height: u32) -> Rectangle { + Rectangle { + x: corner.x(), + y: corner.y(), + width, + height, + } + } + + fn from_corners(corner_a: Point, corner_b: Point) -> Rectangle { + let left = corner_a.x().min(corner_b.x()); + let top = corner_a.y().min(corner_b.y()); + + let width = (corner_a.x() as i64 - corner_b.x() as i64).abs() as u32; + let height = (corner_a.y() as i64 - corner_b.y() as i64).abs() as u32; + + Rectangle { + x: left, + y: top, + width, + height, + } + } + + fn at_origin_with_size(width: u32, height: u32) -> Rectangle { + Rectangle { + x: 0, + y: 0, + width, + height, + } + } + + fn width(&self) -> u32 { + self.width + } + + fn height(&self) -> u32 { + self.height + } + + fn with_width(&self, width: u32) -> Rectangle { + Rectangle { + x: self.x, + y: self.y, + width, + height: self.height, + } + } + + fn with_height(&self, height: u32) -> Rectangle { + Rectangle { + x: self.x, + y: self.y, + width: self.width, + height, + } + } +} + #[cfg(test)] mod tests { use super::*;