nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry
MIT License
638 stars 53 forks source link

flattenedPointsAreClockwise - unexpected results for self-intersecting polygons #113

Open luckyclan opened 6 months ago

luckyclan commented 6 months ago

I found that flattenedPointsAreClockwise may return wrong value for self-intersecting polygons. For example it returns true for: (0,0) (9,0) (0,10) (10,10) But returns false for: 0,0) (11,0) (0,10) (10,10)

To be more specific - the returned values says whether the polygon is "mostly clockwise" or not. It is not information that all neighbouring edges are clockwise. And for self-intersecting points orientation is undefined.

Suggestion: add assert(pointsAreSelfIntersecting(points) == false)

More informations: https://en.wikipedia.org/wiki/Curve_orientation

nicklockwood commented 6 months ago

@luckyclan thanks for reporting this. I'm curious how you bumped into it though since the method is internal. Is there a way to reproduce this issue using public APIs?

luckyclan commented 6 months ago

I'm author of Artstudio Pro app I just use similar function in my own (closed source) graphic library in Swift, and i encounter the same issue. So i started checking how is it implemented in other libraries, and it turned out that nobody cares of this case. "isClockwise" is very imporatant function for asserts, it helps to find a lot of bugs.

I think Euclid is great and i often use it to learn how this and that works, how to write good code etc. I just wanted to help to improve it a bit ;)

I can write that In my library i just added: assert(isPolygonSelfIntersecting() == false)