geometry: simplify coord traits, add sized, clone, copy, etc

This commit is contained in:
Victoria Fischer 2024-11-24 18:58:36 +01:00
parent 35ccf20142
commit 450fbbf9c9

View File

@ -1,6 +1,8 @@
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::Sub; use std::ops::{Mul, Sub, Add};
use num::{One, pow, integer::Roots};
use std::cmp::{min, max};
pub trait CoordinateSpace {} pub trait CoordinateSpace {}
@ -11,7 +13,7 @@ pub struct Coordinates<T: CoordLimits<Data = T>, S: CoordinateSpace> {
space: PhantomData<S>, space: PhantomData<S>,
} }
impl<T: CoordLimits<Data = T> + Debug + PartialOrd, S: CoordinateSpace> Debug for Coordinates<T, S> { impl<T: CoordLimits<Data = T> + Debug, S: CoordinateSpace> Debug for Coordinates<T, S> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("@") f.debug_tuple("@")
.field(&self.x) .field(&self.x)
@ -20,8 +22,8 @@ impl<T: CoordLimits<Data = T> + Debug + PartialOrd, S: CoordinateSpace> Debug fo
} }
} }
pub trait CoordLimits: PartialOrd + PartialEq { pub trait CoordLimits: PartialOrd + PartialEq + Sub + Clone + Mul + Copy + One + Add {
type Data: Sized + PartialOrd + PartialEq; type Data: CoordLimits;
const MIN: Self::Data; const MIN: Self::Data;
const MAX: Self::Data; const MAX: Self::Data;
} }
@ -89,12 +91,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> + Clone + Copy, Space: CoordinateSpace> { pub struct Rectangle<CoordData: CoordLimits<Data = CoordData>, 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> + Sub<Output=CoordData> + Clone + Copy, Space: CoordinateSpace> Rectangle<CoordData, Space> { impl<CoordData: CoordLimits<Data = CoordData> + Sub<Output=CoordData>, 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 {
debug_assert!(top_left.x <= bottom_right.x); debug_assert!(top_left.x <= bottom_right.x);
debug_assert!(top_left.y <= bottom_right.y); debug_assert!(top_left.y <= bottom_right.y);