diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-24 20:03:12 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2023-12-24 21:38:49 +1300 |
commit | 8547ad9c2f2867da9b0e655dd4ff0fe78f04ffba (patch) | |
tree | 554f4514177cc8c1436ed3ec3ae44f176684f664 /src/point.rs | |
download | geometry-8547ad9c2f2867da9b0e655dd4ff0fe78f04ffba.zip |
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) + } +} |