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

#412 MultiPolygon::rotate should use centroid as pivot #562

Closed
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
55 changes: 53 additions & 2 deletions geo/src/algorithm/rotate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::algorithm::centroid::Centroid;
use crate::algorithm::map_coords::MapCoords;
use crate::{Line, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon};
use crate::{
CoordinateType, Line, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
};
use num_traits::{Float, FromPrimitive};
use std::iter::Sum;

#[inline]
fn rotate_inner<T>(x: T, y: T, x0: T, y0: T, sin_theta: T, cos_theta: T) -> Point<T>
where
Expand Down Expand Up @@ -217,6 +218,16 @@ where
}
}

pub trait RotateCentroid<T: CoordinateType>: RotatePoint<T> + Centroid<Output = Point<T>> {
fn rotate_around_centroid(&self, angle: T) -> Self
where
T: Float,
Self: Sized,
{
self.rotate_around_point(angle, self.centroid())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some input which I thought would be better to discuss in the issue rather than the PR, please see here: #412 (comment)

Curious about your opinion here.

}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -346,4 +357,44 @@ mod test {
);
assert_eq!(line0.rotate_around_point(90., Point::new(0., 0.)), line1);
}

#[test]
fn test_rotate_around_centroid_point() {
let origin = Point::new(0., 0.);

let mpoly = MultiPolygon(vec![
// Box in quadrant 1.
polygon![
(x: 1., y: 1.),
(x: 2., y: 1.),
(x: 2., y: 2.),
(x: 1., y: 2.)
],
// 345-triangle quadrant 2.
polygon![
(x: -1., y: 1.),
(x: -1., y: 5.),
(x: -4., y: 1.),
],
]);

let expected = MultiPolygon(vec![
// Box rotate into quadrant 2.
polygon![
(x: -0.9999999999999999, y: 1.),
(x: -0.9999999999999999, y: 2.),
(x: -1.9999999999999998, y: 2.),
(x: -2., y: 1.0000000000000002),
(x: -0.9999999999999999, y: 1.0 )
],
// 345-triangle rotated into quadrant 3.
polygon![
(x: -1., y: -0.9999999999999999),
(x: -5., y: -0.9999999999999997),
(x: -1.0000000000000002, y: -4.),
(x: -1.0, y: -0.9999999999999999)
],
]);
assert_eq!(mpoly.rotate_around_point(90., origin), expected);
}
}