Skip to content

Commit afaeecd

Browse files
committed
Fix area formula (fixes #11)
1 parent fcab497 commit afaeecd

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/area.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::{Float, Pt};
22

33
pub fn area(ring: &[Pt]) -> Float {
4-
let mut i = 0;
5-
let n = ring.len() - 1;
4+
let n = ring.len();
65
let mut area = ring[n - 1].y * ring[0].x - ring[n - 1].x * ring[0].y;
7-
while i < n {
8-
i += 1;
6+
for i in 1..n {
97
area += ring[i - 1].y * ring[i].x - ring[i - 1].x * ring[i].y;
108
}
9+
// Note that in the shoelace formula you need to divide this result by 2 to get the actual area.
10+
// Here we skip this division because we only use this area formula to calculate the winding
11+
// order of polygons and to compare their relative sizes.
1112
area
1213
}
1314

0 commit comments

Comments
 (0)