From a7b681a04657354c56052106c89f0407ad952d32 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sat, 23 Nov 2024 15:01:02 +0100 Subject: [PATCH] geometry: implement width/height/left/top/right/bottom operations for rectangles --- src/geometry.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/geometry.rs b/src/geometry.rs index b6ea16f..c6e0642 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -1,5 +1,6 @@ use std::fmt::{Debug, Formatter}; use std::marker::PhantomData; +use std::ops::Sub; pub trait CoordinateSpace {} @@ -88,12 +89,12 @@ pub type VirtualCoordinates = Coordinates; pub type PhysicalCoordinates = Coordinates; #[derive(PartialEq, Eq, Copy, Clone, Debug, PartialOrd)] -pub struct Rectangle, Space: CoordinateSpace> { +pub struct Rectangle + Clone + Copy, Space: CoordinateSpace> { pub top_left: Coordinates, pub bottom_right: Coordinates } -impl, Space: CoordinateSpace> Rectangle { +impl + Sub + Clone + Copy, Space: CoordinateSpace> Rectangle { pub fn new(top_left: Coordinates, bottom_right: Coordinates) -> Self { assert!(top_left.x <= bottom_right.x); assert!(top_left.y <= bottom_right.y); @@ -109,4 +110,28 @@ impl, Space: CoordinateSpace> Rectangle bottom_right: Coordinates::::bottom_right() } } + + pub fn width(&self) -> CoordData { + self.bottom_right.x.clone() - self.top_left.x.clone() + } + + pub fn height(&self) -> CoordData { + self.bottom_right.y.clone() - self.top_left.y.clone() + } + + pub const fn left(&self) -> CoordData { + self.top_left.x + } + + pub const fn top(&self) -> CoordData { + self.top_left.y + } + + pub const fn right (&self) -> CoordData { + self.bottom_right.x + } + + pub const fn bottom(&self) -> CoordData { + self.bottom_right.y + } }