From afaeecd0a2562e131ee23c196c3a9c56752b601d Mon Sep 17 00:00:00 2001 From: Matthieu Viry Date: Mon, 11 Mar 2024 13:50:21 +0100 Subject: [PATCH] Fix area formula (fixes #11) --- src/area.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/area.rs b/src/area.rs index 2ed4f21..a3cb8f4 100644 --- a/src/area.rs +++ b/src/area.rs @@ -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 }