Skip to content

Commit 3dc78d4

Browse files
netthiermthh
andcommitted
Always use f64 for area calculation
Signed-off-by: netthier <[email protected]> Co-Authored-by: Matthieu Viry <[email protected]>
1 parent 96db165 commit 3dc78d4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/area.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use crate::{Float, Pt};
22

3-
pub fn area(ring: &[Pt]) -> Float {
3+
#[allow(clippy::unnecessary_cast)]
4+
// Note that we need to disable the clippy warning about unnecessary casts
5+
// because of the "f32" optional feature (and because we want to ensure we always
6+
// use "f64" in this function, both in the default feature and in the "f32" feature).
7+
pub fn area(ring: &[Pt]) -> f64 {
48
let n = ring.len();
5-
let mut area = ring[n - 1].y * ring[0].x - ring[n - 1].x * ring[0].y;
9+
let mut area =
10+
ring[n - 1].y as f64 * ring[0].x as f64 - ring[n - 1].x as f64 * ring[0].y as f64;
611
for i in 1..n {
7-
area += ring[i - 1].y * ring[i].x - ring[i - 1].x * ring[i].y;
12+
area += ring[i - 1].y as f64 * ring[i].x as f64 - ring[i - 1].x as f64 * ring[i].y as f64;
813
}
914
// Note that in the shoelace formula you need to divide this result by 2 to get the actual area.
1015
// Here we skip this division because we only use this area formula to calculate the winding

0 commit comments

Comments
 (0)