GeometryBasics.jl
GeometryBasics.jl copied to clipboard
Area of 2D triangle can be negative
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:
- It's the generic implementation for N dimensions, but it only works for N=2, as it assumes that
crossexists and returns a scalar. - It's missing an
abscall 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?
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 .
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)?
Oh, good catch :D Yeah, I guess the area function is mostly used inside GeometryBasics to figure out the winding order.
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?