geometry: implement width/height/left/top/right/bottom operations for rectangles
This commit is contained in:
parent
ff610fa6aa
commit
a7b681a046
@ -1,5 +1,6 @@
|
|||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::ops::Sub;
|
||||||
|
|
||||||
pub trait CoordinateSpace {}
|
pub trait CoordinateSpace {}
|
||||||
|
|
||||||
@ -88,12 +89,12 @@ pub type VirtualCoordinates = Coordinates<u8, Virtual>;
|
|||||||
pub type PhysicalCoordinates = Coordinates<usize, Physical>;
|
pub type PhysicalCoordinates = Coordinates<usize, Physical>;
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Copy, Clone, Debug, PartialOrd)]
|
#[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 top_left: Coordinates<CoordData, Space>,
|
||||||
pub bottom_right: 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 {
|
pub fn new(top_left: Coordinates<CoordData, Space>, bottom_right: Coordinates<CoordData, Space>) -> Self {
|
||||||
assert!(top_left.x <= bottom_right.x);
|
assert!(top_left.x <= bottom_right.x);
|
||||||
assert!(top_left.y <= bottom_right.y);
|
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()
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user