geometry: implement width/height/left/top/right/bottom operations for rectangles

This commit is contained in:
Victoria Fischer 2024-11-23 15:01:02 +01:00
parent ff610fa6aa
commit a7b681a046

View File

@ -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<u8, Virtual>;
pub type PhysicalCoordinates = Coordinates<usize, Physical>;
#[derive(PartialEq, Eq, Copy, Clone, Debug, PartialOrd)]
pub struct Rectangle<CoordData: CoordLimits<Data = CoordData>, Space: CoordinateSpace> {
pub struct Rectangle<CoordData: CoordLimits<Data = CoordData> + Clone + Copy, Space: CoordinateSpace> {
pub top_left: Coordinates<CoordData, Space>,
pub bottom_right: Coordinates<CoordData, Space>
}
impl<CoordData: CoordLimits<Data = CoordData>, Space: CoordinateSpace> Rectangle<CoordData, Space> {
impl<CoordData: CoordLimits<Data = CoordData> + Sub<Output=CoordData> + Clone + Copy, Space: CoordinateSpace> Rectangle<CoordData, Space> {
pub fn new(top_left: Coordinates<CoordData, Space>, bottom_right: Coordinates<CoordData, Space>) -> Self {
assert!(top_left.x <= bottom_right.x);
assert!(top_left.y <= bottom_right.y);
@ -109,4 +110,28 @@ impl<CoordData: CoordLimits<Data = CoordData>, Space: CoordinateSpace> Rectangle
bottom_right: Coordinates::<CoordData, Space>::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
}
}