I need to get rid of self-intersections in a shape. Shape is constructed from an array of points, so all segments of that shape are lines. (only lines, no curves and arcs)
Previously, I tried to create Path2D from that points, construct an Area from it, and then using its PathIterator I created several Path2Ds, which somehow were subpaths of previous path, and so self-intersetions were gone. But this isn't working for some paths - self-intersections still remain there.
So could you point me to some place where I can find good algorithm to do similar thing?
Edit: I haven't found anything useful anywhere, so I written my own algorithm. See the answers.
I bookmarked your question/answer in case I had to implement something similar myself, but then I found the GEOS project which has a simple way of achieving this. I'm calling GEOS from Python/Django, but the whole thing is based on JTS (Java Topology Suite) so I'd start there and treat the following Python as psuedo-code.
Basically, the UNION operation will split a line into simply connected parts if it is not simply connected (explained here), so UNIONing the line with it's first point does what we need:
If
Area
is not working for you, you could try using a GLUtessellator to decompose yourShape
into a set of triangles, or (using theGL_LINE_LOOP
option) just the boundary edges.So, since I was unable to find anything like this on the web, I written my own algorithm.
It may be insanely ineffective, but it works fast enough for me.
Here it goes: