Skip to content

Commit 7d7c31e

Browse files
committed
Run rustfmt on rotate.rs.
1 parent 5919f15 commit 7d7c31e

File tree

1 file changed

+124
-77
lines changed

1 file changed

+124
-77
lines changed

src/algorithm/rotate.rs

+124-77
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use algorithm::map_coords::MapCoords;
77
// origin can be an arbitrary point, pass &Point::new(0., 0.)
88
// for the actual origin
99
fn rotation_matrix<T>(angle: T, origin: &Point<T>, points: &[Point<T>]) -> Vec<Point<T>>
10-
where T: Float
10+
where
11+
T: Float,
1112
{
1213
let cos_theta = angle.to_radians().cos();
1314
let sin_theta = angle.to_radians().sin();
@@ -16,11 +17,13 @@ fn rotation_matrix<T>(angle: T, origin: &Point<T>, points: &[Point<T>]) -> Vec<P
1617
points
1718
.iter()
1819
.map(|point| {
19-
let x = point.x() - x0;
20-
let y = point.y() - y0;
21-
Point::new(x * cos_theta - y * sin_theta + x0,
22-
x * sin_theta + y * cos_theta + y0)
23-
})
20+
let x = point.x() - x0;
21+
let y = point.y() - y0;
22+
Point::new(
23+
x * cos_theta - y * sin_theta + x0,
24+
x * sin_theta + y * cos_theta + y0,
25+
)
26+
})
2427
.collect::<Vec<_>>()
2528
}
2629

@@ -46,7 +49,9 @@ pub trait Rotate<T> {
4649
/// let correct_ls = LineString(correct);
4750
/// assert_eq!(rotated, correct_ls);
4851
/// ```
49-
fn rotate(&self, angle: T) -> Self where T: Float;
52+
fn rotate(&self, angle: T) -> Self
53+
where
54+
T: Float;
5055
}
5156

5257
pub trait RotatePoint<T> {
@@ -71,12 +76,15 @@ pub trait RotatePoint<T> {
7176
/// let correct_ls = LineString(correct);
7277
/// assert_eq!(rotated, correct_ls);
7378
/// ```
74-
fn rotate_around_point(&self, angle: T, point: &Point<T>) -> Self where T: Float;
79+
fn rotate_around_point(&self, angle: T, point: &Point<T>) -> Self
80+
where
81+
T: Float;
7582
}
7683

7784
impl<T, G> RotatePoint<T> for G
78-
where T: Float,
79-
G: MapCoords<T, T, Output=G>,
85+
where
86+
T: Float,
87+
G: MapCoords<T, T, Output = G>,
8088
{
8189
fn rotate_around_point(&self, angle: T, point: &Point<T>) -> Self {
8290
let cos_theta = angle.to_radians().cos();
@@ -86,14 +94,17 @@ impl<T, G> RotatePoint<T> for G
8694
self.map_coords(&|&(x, y)| {
8795
let x = x - x0;
8896
let y = y - y0;
89-
(x * cos_theta - y * sin_theta + x0,
90-
x * sin_theta + y * cos_theta + y0)
97+
(
98+
x * cos_theta - y * sin_theta + x0,
99+
x * sin_theta + y * cos_theta + y0,
100+
)
91101
})
92102
}
93103
}
94104

95105
impl<T> Rotate<T> for Point<T>
96-
where T: Float
106+
where
107+
T: Float,
97108
{
98109
/// Rotate the Point about itself by the given number of degrees
99110
/// This operation leaves the point coordinates unchanged
@@ -103,7 +114,8 @@ impl<T> Rotate<T> for Point<T>
103114
}
104115

105116
impl<T> Rotate<T> for Line<T>
106-
where T: Float
117+
where
118+
T: Float,
107119
{
108120
fn rotate(&self, angle: T) -> Self {
109121
let pts = vec![self.start, self.end];
@@ -113,7 +125,8 @@ impl<T> Rotate<T> for Line<T>
113125
}
114126

115127
impl<T> Rotate<T> for LineString<T>
116-
where T: Float
128+
where
129+
T: Float,
117130
{
118131
/// Rotate the LineString about its centroid by the given number of degrees
119132
fn rotate(&self, angle: T) -> Self {
@@ -122,7 +135,8 @@ impl<T> Rotate<T> for LineString<T>
122135
}
123136

124137
impl<T> Rotate<T> for Polygon<T>
125-
where T: Float + FromPrimitive
138+
where
139+
T: Float + FromPrimitive,
126140
{
127141
/// Rotate the Polygon about its centroid by the given number of degrees
128142
fn rotate(&self, angle: T) -> Self {
@@ -131,16 +145,21 @@ impl<T> Rotate<T> for Polygon<T>
131145
false => self.exterior.centroid().unwrap(),
132146
true => self.centroid().unwrap(),
133147
};
134-
Polygon::new(LineString(rotation_matrix(angle, &centroid, &self.exterior.0)),
135-
self.interiors
136-
.iter()
137-
.map(|ring| LineString(rotation_matrix(angle, &centroid, &ring.0)))
138-
.collect())
148+
Polygon::new(
149+
LineString(rotation_matrix(angle, &centroid, &self.exterior.0)),
150+
self.interiors
151+
.iter()
152+
.map(|ring| {
153+
LineString(rotation_matrix(angle, &centroid, &ring.0))
154+
})
155+
.collect(),
156+
)
139157
}
140158
}
141159

142160
impl<T> Rotate<T> for MultiPolygon<T>
143-
where T: Float + FromPrimitive
161+
where
162+
T: Float + FromPrimitive,
144163
{
145164
/// Rotate the contained Polygons about their centroids by the given number of degrees
146165
fn rotate(&self, angle: T) -> Self {
@@ -149,7 +168,8 @@ impl<T> Rotate<T> for MultiPolygon<T>
149168
}
150169

151170
impl<T> Rotate<T> for MultiLineString<T>
152-
where T: Float + FromPrimitive
171+
where
172+
T: Float + FromPrimitive,
153173
{
154174
/// Rotate the contained LineStrings about their centroids by the given number of degrees
155175
fn rotate(&self, angle: T) -> Self {
@@ -158,7 +178,8 @@ impl<T> Rotate<T> for MultiLineString<T>
158178
}
159179

160180
impl<T> Rotate<T> for MultiPoint<T>
161-
where T: Float + FromPrimitive
181+
where
182+
T: Float + FromPrimitive,
162183
{
163184
/// Rotate the contained Points about their centroids by the given number of degrees
164185
fn rotate(&self, angle: T) -> Self {
@@ -195,74 +216,97 @@ mod test {
195216
}
196217
#[test]
197218
fn test_rotate_polygon() {
198-
let points_raw = vec![(5., 1.), (4., 2.), (4., 3.), (5., 4.), (6., 4.), (7., 3.),
199-
(7., 2.), (6., 1.), (5., 1.)];
219+
let points_raw = vec![
220+
(5., 1.),
221+
(4., 2.),
222+
(4., 3.),
223+
(5., 4.),
224+
(6., 4.),
225+
(7., 3.),
226+
(7., 2.),
227+
(6., 1.),
228+
(5., 1.),
229+
];
200230
let points = points_raw
201231
.iter()
202232
.map(|e| Point::new(e.0, e.1))
203233
.collect::<Vec<_>>();
204234
let poly1 = Polygon::new(LineString(points), vec![]);
205235
let rotated = poly1.rotate(-15.0);
206-
let correct_outside = vec![(4.628808519201685, 1.1805207831176578),
207-
(3.921701738015137, 2.405265654509247),
208-
(4.180520783117657, 3.3711914807983154),
209-
(5.405265654509247, 4.0782982619848624),
210-
(6.371191480798315, 3.819479216882342),
211-
(7.0782982619848624, 2.594734345490753),
212-
(6.819479216882343, 1.6288085192016848),
213-
(5.594734345490753, 0.9217017380151371),
214-
(4.628808519201685, 1.1805207831176578)];
215-
let correct = Polygon::new(LineString(correct_outside
216-
.iter()
217-
.map(|e| Point::new(e.0, e.1))
218-
.collect::<Vec<_>>()),
219-
vec![]);
236+
let correct_outside = vec![
237+
(4.628808519201685, 1.1805207831176578),
238+
(3.921701738015137, 2.405265654509247),
239+
(4.180520783117657, 3.3711914807983154),
240+
(5.405265654509247, 4.0782982619848624),
241+
(6.371191480798315, 3.819479216882342),
242+
(7.0782982619848624, 2.594734345490753),
243+
(6.819479216882343, 1.6288085192016848),
244+
(5.594734345490753, 0.9217017380151371),
245+
(4.628808519201685, 1.1805207831176578),
246+
];
247+
let correct = Polygon::new(
248+
LineString(
249+
correct_outside
250+
.iter()
251+
.map(|e| Point::new(e.0, e.1))
252+
.collect::<Vec<_>>(),
253+
),
254+
vec![],
255+
);
220256
// results agree with Shapely / GEOS
221257
assert_eq!(rotated, correct);
222258
}
223259
#[test]
224260
fn test_rotate_polygon_holes() {
225-
let ls1 = LineString(vec![Point::new(5.0, 1.0),
226-
Point::new(4.0, 2.0),
227-
Point::new(4.0, 3.0),
228-
Point::new(5.0, 4.0),
229-
Point::new(6.0, 4.0),
230-
Point::new(7.0, 3.0),
231-
Point::new(7.0, 2.0),
232-
Point::new(6.0, 1.0),
233-
Point::new(5.0, 1.0)]);
261+
let ls1 = LineString(vec![
262+
Point::new(5.0, 1.0),
263+
Point::new(4.0, 2.0),
264+
Point::new(4.0, 3.0),
265+
Point::new(5.0, 4.0),
266+
Point::new(6.0, 4.0),
267+
Point::new(7.0, 3.0),
268+
Point::new(7.0, 2.0),
269+
Point::new(6.0, 1.0),
270+
Point::new(5.0, 1.0),
271+
]);
234272

235-
let ls2 = LineString(vec![Point::new(5.0, 1.3),
236-
Point::new(5.5, 2.0),
237-
Point::new(6.0, 1.3),
238-
Point::new(5.0, 1.3)]);
273+
let ls2 = LineString(vec![
274+
Point::new(5.0, 1.3),
275+
Point::new(5.5, 2.0),
276+
Point::new(6.0, 1.3),
277+
Point::new(5.0, 1.3),
278+
]);
239279

240-
let ls3 = LineString(vec![Point::new(5., 2.3),
241-
Point::new(5.5, 3.0),
242-
Point::new(6., 2.3),
243-
Point::new(5., 2.3)]);
280+
let ls3 = LineString(vec![
281+
Point::new(5., 2.3),
282+
Point::new(5.5, 3.0),
283+
Point::new(6., 2.3),
284+
Point::new(5., 2.3),
285+
]);
244286

245287
let poly1 = Polygon::new(ls1, vec![ls2, ls3]);
246288
let rotated = poly1.rotate(-15.0);
247-
let correct_outside = vec![(4.628808519201685, 1.180520783117658),
248-
(3.921701738015137, 2.4052656545092472),
249-
(4.180520783117657, 3.3711914807983154),
250-
(5.405265654509247, 4.078298261984863),
251-
(6.371191480798315, 3.8194792168823426),
252-
(7.0782982619848624, 2.594734345490753),
253-
(6.819479216882343, 1.628808519201685),
254-
(5.594734345490753, 0.9217017380151373),
255-
(4.628808519201685, 1.180520783117658)]
256-
.iter()
257-
.map(|e| Point::new(e.0, e.1))
258-
.collect::<Vec<_>>();
259-
let correct_inside = vec![(4.706454232732441, 1.4702985310043786),
260-
(5.37059047744874, 2.017037086855466),
261-
(5.672380059021509, 1.2114794859018578),
262-
(4.706454232732441, 1.4702985310043786)]
263-
.iter()
264-
.map(|e| Point::new(e.0, e.1))
265-
.collect::<Vec<_>>();
289+
let correct_outside = vec![
290+
(4.628808519201685, 1.180520783117658),
291+
(3.921701738015137, 2.4052656545092472),
292+
(4.180520783117657, 3.3711914807983154),
293+
(5.405265654509247, 4.078298261984863),
294+
(6.371191480798315, 3.8194792168823426),
295+
(7.0782982619848624, 2.594734345490753),
296+
(6.819479216882343, 1.628808519201685),
297+
(5.594734345490753, 0.9217017380151373),
298+
(4.628808519201685, 1.180520783117658),
299+
].iter()
300+
.map(|e| Point::new(e.0, e.1))
301+
.collect::<Vec<_>>();
302+
let correct_inside = vec![
303+
(4.706454232732441, 1.4702985310043786),
304+
(5.37059047744874, 2.017037086855466),
305+
(5.672380059021509, 1.2114794859018578),
306+
(4.706454232732441, 1.4702985310043786),
307+
].iter()
308+
.map(|e| Point::new(e.0, e.1))
309+
.collect::<Vec<_>>();
266310
// println!("INSIDE {:?}", rotated.interiors[0].0);
267311
assert_eq!(rotated.exterior.0, correct_outside);
268312
assert_eq!(rotated.interiors[0].0, correct_inside);
@@ -282,7 +326,10 @@ mod test {
282326
#[test]
283327
fn test_rotate_line_around_point() {
284328
let line0 = Line::new(Point::new(0., 0.), Point::new(0., 2.));
285-
let line1 = Line::new(Point::new(0., 0.), Point::new(-2., 0.00000000000000012246467991473532));
329+
let line1 = Line::new(
330+
Point::new(0., 0.),
331+
Point::new(-2., 0.00000000000000012246467991473532),
332+
);
286333
assert_eq!(line0.rotate_around_point(90., &Point::new(0., 0.)), line1);
287334
}
288335
}

0 commit comments

Comments
 (0)