Skip to content

Commit

Permalink
Fix area formula (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthh committed Mar 11, 2024
1 parent fcab497 commit afaeecd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/area.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{Float, Pt};

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

Expand Down

0 comments on commit afaeecd

Please sign in to comment.