Simple polygon intersection#101
Conversation
|
Thanks for the PR! I'd be very nervous about publishing this as-is, though, since I know that polygon intersection is a very tricky thing to get right and will probably require a pretty sophisticated algorithm to be appropriate for general-purpose use. I've also tried very hard to avoid having any hidden 'epsilon' or 'tolerance' values in I do agree that there's a lot of useful code in here, though - what would you think of moving the code into a non-exposed (Side note: not sure what's going on with Travis, seems like it might be an issue with the Elm NPM package? If it doesn't go away in a few days I will investigate further...) |
|
Makes sense, will do in a couple of weeks. Regarding the tolerances: It's possible neither may be necessary. The area check is just checking if all three points lie on a single line - there must be a better way to compute this than checking if the area of the triangle formed is zero. The distance filter is there just to remove extra zero-ish length edges, which is not necessary, but leads to simpler results. It's possible the rounding in |
This is an odd one.
I've built this the simplest, dumbest way possible just to get something out of the door (I happen to need this for my elm-europe talk). It is most certainly not state-of-the-art algorithmically. Nor do I claim it to handle degenerate cases. It seems to do a good enough job for my kind of use-case (i.e. reasonably complex polygons without holes or kinks).
So I can foresee some things you might want to do with this PR:
Some details on how this works.
The algorithm works in 2 phases. First there is the
subdividephase. In this phase extra vertices are added into the intersections of the edges of the two polygons. This now means we have polygons with more edges, but we now have all the edges for the resulting polygon. I'm not entirely sure about the runtime here, but it must be at the minimum O(n*m) (where n is the number of vertices in the first polygon, and m is the number of vertices in the second polygon; so effectively quadratic performance).Next we need to actually assemble the intersected polygon. We go through each edge in both the subdivided polygons, and check if the midpoint of that edge is in the other polygon. We then attempt to order the edges into proper loops. (Note: at the moment I haven't built support for holes, but conceptually this shouldn't be too hard - we could run
buildChainuntil no more edges remain inedgeDict, then simply figuring out which contains which). This operation is at least O(n'*m + m'*n), where the primes are the vertex counts after subdivision.