From 8547ad9c2f2867da9b0e655dd4ff0fe78f04ffba Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 24 Dec 2023 20:03:12 +1300 Subject: First commit --- src/point.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/point.rs (limited to 'src/point.rs') 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 { + pub x: O, + pub y: O, +} + +pub trait HasPosition { + fn position(&self) -> Point; + + fn x(&self) -> O { + self.position().x + } + fn y(&self) -> O { + self.position().y + } +} + +impl Point { + pub const ZERO: Self = Self { x: O::ZERO, y: O::ZERO }; + + pub const fn new(x: O, y: O) -> Point { + Point { x, y } + } + + pub fn is_zero(&self) -> bool { + self.x == O::ZERO && self.y == O::ZERO + } +} + +impl HasPosition for Point { + fn position(&self) -> Point { + *self + } +} + +impl Add> for Point { + type Output = Self; + fn add(self, point: Point) -> Self { + Point::new(self.x + point.x, self.y + point.y) + } +} + +impl Sub> for Point { + type Output = Self; + fn sub(self, point: Point) -> Self { + Point::new(self.x - point.x, self.y - point.y) + } +} + +impl AddAssign> for Point { + fn add_assign(&mut self, point: Point) { + self.x += point.x; + self.y += point.y; + } +} + +impl SubAssign> for Point { + fn sub_assign(&mut self, point: Point) { + self.x -= point.x; + self.y -= point.y; + } +} + +impl fmt::Debug for Point { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Point {{ x:{:?} y:{:?} }}", self.x, self.y) + } +} -- cgit v1.2.3-70-g09d2