Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup coordinate math ops #788

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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