You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix half-plane interesection not correctly handling opposite parallel half-planes (#839)
* Fix typo in half-plane intersection
* Fix handling of special cases in half-plane intersection code.
Previous code for handling parallel half-planes of opposite orientation
could fail in the specific case the two half-planes formed an empty
intersection.
Also fixed the explanation of special case handling.
Copy file name to clipboardExpand all lines: src/geometry/halfplane-intersection.md
+27-13Lines changed: 27 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,8 +55,12 @@ Notice the half-plane $F$ makes $A$ and $E$ redundant in the intersection. So we
55
55
56
56
With all of this in mind, we have almost everything we need to actually implement the algorithm, but we still need to talk about some special cases. At the beginning of the article we said we would add a bounding box to take care of the cases where the intersection could be unbounded, so the only tricky case we actually need to handle is parallel half-planes. We can have two sub-cases: two half-planes can be parallel with the same direction or with opposite direction. The reason this case needs to be handled separately is because we will need to compute intersection points of half-plane lines to be able to check if a half-plane is redundant or not, and two parallel lines have no intersection point, so we need a special way to deal with them.
57
57
58
-
Notice that, because we're adding the bounding box to deal with the unbounded case, this also deals with the case where we have two adjacent parallel half-planes with opposite directions, since there will have to be at least one of the bounding-box half-planes in between these two (remember they are sorted by angle). Thus the only case we actually need to handle is having multiple half-planes with the same angle, and it turns out this is also very easy to handle: just keep the leftmost one and erase the rest, since the others will be completely redundant anyways.
58
+
For the case of parallel half-planes of opposite orientation: Notice that, because we're adding the bounding box to deal with the unbounded case, this also deals with the case where we have two adjacent parallel half-planes with opposite directions after sorting, since there will have to be at least one of the bounding-box half-planes in between these two (remember they are sorted by angle).
59
59
60
+
* However, it is possible that, after removing some half-planes from the back of the deque, two parallel half-planes of opposite direction end up together. This case only happens, specifically, when these two half-planes form an empty intersection, as this last half-plane will cause everything to be removed from the deque. To avoid this problem, we have to manually check for parallel half-planes, and if they have opposite direction, we just instantly stop the algorithm and return an empty intersection.
61
+
62
+
63
+
Thus the only case we actually need to handle is having multiple half-planes with the same angle, and it turns out this case is fairly easy to handle: we only have keep the leftmost half-plane and erase the rest, since they will be completely redundant anyways.
60
64
To sum up, the full algorithm will roughly look as follows:
61
65
62
66
1. We begin by sorting the set of half-planes by angle, which takes $O(N \log N)$ time.
@@ -81,7 +85,7 @@ struct Point {
81
85
long double x, y;
82
86
explicit Point(long double x = 0, long double y = 0) : x(x), y(y) {}
83
87
84
-
// Addition, substraction, multiply by constant, cross product.
88
+
// Addition, substraction, multiply by constant, dot product, cross product.
0 commit comments