diff options
Diffstat (limited to 'src/point.rs')
-rw-r--r-- | src/point.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/point.rs b/src/point.rs new file mode 100644 index 0000000..d7bd577 --- /dev/null +++ b/src/point.rs @@ -0,0 +1,72 @@ +use crate::*; +use std::fmt; +use std::ops::{Add, Sub, AddAssign, SubAssign}; + +#[derive(Copy, Clone, PartialEq)] +pub struct Point<O: Internal> { + pub x: O, + pub y: O, +} + +pub trait HasPosition<O: Internal> { + fn position(&self) -> Point<O>; + + fn x(&self) -> O { + self.position().x + } + fn y(&self) -> O { + self.position().y + } +} + +impl<O: Internal> Point<O> { + pub const ZERO: Self = Self { x: O::ZERO, y: O::ZERO }; + + pub const fn new(x: O, y: O) -> Point<O> { + Point { x, y } + } + + pub fn is_zero(&self) -> bool { + self.x == O::ZERO && self.y == O::ZERO + } +} + +impl<O: Internal> HasPosition<O> for Point<O> { + fn position(&self) -> Point<O> { + *self + } +} + +impl<O: Internal> Add<Point<O>> for Point<O> { + type Output = Self; + fn add(self, point: Point<O>) -> Self { + Point::new(self.x + point.x, self.y + point.y) + } +} + +impl<O: Internal> Sub<Point<O>> for Point<O> { + type Output = Self; + fn sub(self, point: Point<O>) -> Self { + Point::new(self.x - point.x, self.y - point.y) + } +} + +impl<O: Internal> AddAssign<Point<O>> for Point<O> { + fn add_assign(&mut self, point: Point<O>) { + self.x += point.x; + self.y += point.y; + } +} + +impl<O: Internal> SubAssign<Point<O>> for Point<O> { + fn sub_assign(&mut self, point: Point<O>) { + self.x -= point.x; + self.y -= point.y; + } +} + +impl<T: Internal> fmt::Debug for Point<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Point {{ x:{:?} y:{:?} }}", self.x, self.y) + } +} |