1

I have implemented the Bentley-Ottmann-algorithm to detect polygon-polygon intersections. This usually works very well: Since the polygons are not self-intersecting any line-segment intersection in the union of the line-segments of both polygons indicates that both polygons are intersecting.

But if I look at this case: common-node-intersection

there is no segment-intersection. But clearly both polygons intersect.

How can I detect this case without using the naive algorithm that checks for point-in-polygon for each point of each polygon in the other polygon and hence runs in O(m*n).

user2033412
  • 1,950
  • 2
  • 27
  • 47

2 Answers2

1

Hint:

The handling of special cases such as a vertex on an edge or two overlapping edges is an uneasy topic as the configurations are varied and cases can telescope (think of several collinear edges that overlap).

My best advice is to enforce coherence by assigning every vertex an inside or outside state (wrt the other polygon). Then when you intersect an edge with the other polygon, the parity of the number of intersections must match the change in the endpoint state (same state => even number of intersections).

I am not specifying in what way you assign the states, this will depend on your particular algorithm (in practice states are assigned as you go). What matters is that when a state is tested you may not change it anymore.

In case a anomaly appears (parity of number of intersections not matching the change of state), you must fix that, either by sacrificing an intersection or by duplicating one (or any other rule that you find appropriate).

Example:

In this sample case, the green dots denote vertices considered external to the blue polygon, and the digits indicate acceptable numbers of intersections assigned to the corresponding edges.

The black outline below represents the union polygon that would result from this assignment of states.

enter image description here

0

Your case (a vertical line segment) is a degenerate case for the algorithm (and indeed many sweep-line algorithms) because two events occur at the same instant (x-coordinate) during the sweep and at least one event is not reported as a consequence.

Without modifying your algorithm to handle such cases, a simple workaround is to first transform polygon vertices using shear mapping, and then run the algorithm on this transformed input. When the shear is parallel to the x-axis, multiple intersections with a vertical line segment are now reported, for they now occur at (slightly) different x coordinates.

If unsheared/proper coordinates of intersections are needed, simply apply the inverse of the shear map to the (sheared) points of intersection.

enter image description here

micycle
  • 3,328
  • 14
  • 33