JuliaGeometry / GeometryBasics.jl

Basic Geometry Types
MIT License
164 stars 54 forks source link

Area of 2D triangle can be negative #121

Open knuesel opened 3 years ago

knuesel commented 3 years ago

For example:

julia> area(Point2f0[[0,0], [0,1], [1,1]])
-0.5f0

The relevant area method is here: https://github.com/JuliaGeometry/GeometryBasics.jl/blob/24d0ee53b0d2ff3eca5bc3a8c864ad53d534df76/src/triangulation.jl#L36

I think there are two issues with this method:

  1. It's the generic implementation for N dimensions, but it only works for N=2, as it assumes that cross exists and returns a scalar.
  2. It's missing an abs call on the result.

Regarding the first point: there's already a specific method for N=3, and we can make one for N=2. Do we need a generic method for N>3?

SimonDanisch commented 3 years ago

Good point!

Do we need a generic method for N>3?

Probably not :D Sounds like a good case for only specializing on 2 & 3 .

knuesel commented 3 years ago

Looks like the negative area was intentional:

https://github.com/JuliaGeometry/GeometryBasics.jl/blob/24d0ee53b0d2ff3eca5bc3a8c864ad53d534df76/src/triangulation.jl#L127

Should we leave it like this (and document it)?

SimonDanisch commented 3 years ago

Oh, good catch :D Yeah, I guess the area function is mostly used inside GeometryBasics to figure out the winding order.

knuesel commented 3 years ago

I made the PR for the doc change, but it is a bit inconsistent to return a signed area for 2D an a positive area for 3D... Instead, we could have area return always a positive value, and a new oriented_area that returns a signed value for 2D and a vector for 3D?