Skip to content

Commit

Permalink
Cleanup coordinate math ops
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Mar 19, 2022
1 parent d040fe2 commit 65b3237
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ use std::ops::{Add, Div, Mul, Neg, Sub};
/// # Examples
///
/// ```
/// use geo_types::Coordinate;
/// use geo_types::coord;
///
/// let p: Coordinate<_> = (1.25, 2.5).into();
/// let p = coord! { x: 1.25, y: 2.5 };
/// let q = -p;
///
/// assert_eq!(q.x, -p.x);
Expand All @@ -113,7 +113,10 @@ where
type Output = Self;

fn neg(self) -> Self {
(-self.x, -self.y).into()
coord! {
x: -self.x,
y: -self.y,
}
}
}

Expand All @@ -122,10 +125,10 @@ where
/// # Examples
///
/// ```
/// use geo_types::Coordinate;
/// use geo_types::coord;
///
/// let p: Coordinate<_> = (1.25, 2.5).into();
/// let q: Coordinate<_> = (1.5, 2.5).into();
/// let p = coord! { x: 1.25, y: 2.5 };
/// let q = coord! { x: 1.5, y: 2.5 };
/// let sum = p + q;
///
/// assert_eq!(sum.x, 2.75);
Expand All @@ -135,7 +138,10 @@ impl<T: CoordNum> Add for Coordinate<T> {
type Output = Self;

fn add(self, rhs: Self) -> Self {
(self.x + rhs.x, self.y + rhs.y).into()
coord! {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}

Expand All @@ -144,10 +150,10 @@ impl<T: CoordNum> Add for Coordinate<T> {
/// # Examples
///
/// ```
/// use geo_types::Coordinate;
/// use geo_types::coord;
///
/// let p: Coordinate<_> = (1.5, 2.5).into();
/// let q: Coordinate<_> = (1.25, 2.5).into();
/// let p = coord! { x: 1.5, y: 2.5 };
/// let q = coord! { x: 1.25, y: 2.5 };
/// let diff = p - q;
///
/// assert_eq!(diff.x, 0.25);
Expand All @@ -157,7 +163,10 @@ impl<T: CoordNum> Sub for Coordinate<T> {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
(self.x - rhs.x, self.y - rhs.y).into()
coord! {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}

Expand All @@ -166,10 +175,10 @@ impl<T: CoordNum> Sub for Coordinate<T> {
/// # Examples
///
/// ```
/// use geo_types::Coordinate;
/// use geo_types::coord;
///
/// let p: Coordinate<_> = (1.25, 2.5).into();
/// let q: Coordinate<_> = p * 4.;
/// let p = coord! { x: 1.25, y: 2.5 };
/// let q = p * 4.;
///
/// assert_eq!(q.x, 5.0);
/// assert_eq!(q.y, 10.0);
Expand All @@ -178,7 +187,10 @@ impl<T: CoordNum> Mul<T> for Coordinate<T> {
type Output = Self;

fn mul(self, rhs: T) -> Self {
(self.x * rhs, self.y * rhs).into()
coord! {
x: self.x * rhs,
y: self.y * rhs,
}
}
}

Expand All @@ -187,10 +199,10 @@ impl<T: CoordNum> Mul<T> for Coordinate<T> {
/// # Examples
///
/// ```
/// use geo_types::Coordinate;
/// use geo_types::coord;
///
/// let p: Coordinate<_> = (5., 10.).into();
/// let q: Coordinate<_> = p / 4.;
/// let p = coord! { x: 5., y: 10. };
/// let q = p / 4.;
///
/// assert_eq!(q.x, 1.25);
/// assert_eq!(q.y, 2.5);
Expand All @@ -199,7 +211,10 @@ impl<T: CoordNum> Div<T> for Coordinate<T> {
type Output = Self;

fn div(self, rhs: T) -> Self {
(self.x / rhs, self.y / rhs).into()
coord! {
x: self.x / rhs,
y: self.y / rhs,
}
}
}

Expand Down

0 comments on commit 65b3237

Please sign in to comment.