Skip to content

Commit

Permalink
Merge #788
Browse files Browse the repository at this point in the history
788: Cleanup coordinate math ops r=frewsxcv a=nyurik

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
~- [ ] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.~
---



Co-authored-by: Yuri Astrakhan <[email protected]>
  • Loading branch information
bors[bot] and nyurik authored Mar 21, 2022
2 parents d040fe2 + 65b3237 commit 0f8a3fe
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 0f8a3fe

Please sign in to comment.