Implements a sweepline algorithm that facilitates collision detection between two polygons. For two polygons A and B it determines a set of collision candidates, i.e. the edges of A and B that can collide.
Getting a good approximation of the set of colliding edges is important because given two polygons with n and m vertices each, checking every combination would take n*m operations.
To limit the number of candidates we project all edges of both polygons onto a line. This line is the direction of the sweepline, the sweepline itself would be perpendicular to it. The start and endpoints of the edges are sorted by their projection onto the sweep direction.
The collision candidates can now be determined by walking through the list of start- and endpoints of the edges and check which edges of A and B overlap. If two edges do not overlap in the projection, they will not intersect, therefore it is safe to discard the combination as a collision candidate.
Note that this approach is very similar to and indeed inspired by the separating axes theorem.
The effectiveness of this algorithm depends on the choice of sweep direction. For example using the line from one polygon's center to the other's center as our sweep direction will give great results if both polygons are more or less round. However, when one of the polygons is very long, this will be (depending on the polygon's positions) a very bad idea.
The choice for the sweep direction is left to the user of this class.
For this sweepline algorithm there is a major assumption regarding the complexity of the sorting. The used sorting algorithm uses the fact that the edges will mostly be inserted in order of their position on the sweepline (already sorted).
This seems justified by the observation that most polygons will be more or less monotone in any direction, including the sweep direction. For convex polygons this clearly holds, giving us a worst case complexity of O(n). Non-convex polygons can however cause trouble with a worst case complexity of O(n*n).
@author Gideon Smeding
|
|
|
|
|
|
|
|