• No results found

3.5 Implementation & Ply Computation

3.5.1 Line Segment Intersection

The idea of the plane-sweep algorithm was introduced by Shamos and Hoey [80] and improved by Bentley and Ottman [10] and proved to be a powerful technique in computational geometry. To present the ideas we follow the description from the textbook [28]. The initial task of this algorithm is formulated as follows:

Given a set S = {s1, ..., sn} of n straight-line segments in the plane, detect all

intersections. In the following we assume general positioning of the line segments in the plane, that is

1. no two endpoints of line segments share the same x−coordinate

y

x

Figure 3.15: A crossing of two line segments can only occur in the union of their

x−intervals.

3. no two line segments lie on top of each other

4. no three line segments intersect at the same coordinate.

An intuitive solution to this problem would be to consider every pair of segments and test whether they intersect, which would result in a quadratic time complexity. In order to achieve a faster algorithm we can observe the following: A crossing of two

line segments can only occur in the intersection of their x-intervals. The x−interval

is defined as the projection of the line segment to the x−axis as presented in Figure

3.15. Thereby we only check for intersections between segments, whose x−intervals

overlap, that is pairs of segments for which there exists a vertical line intersecting both segments.

Sweeping the plane from left to right with a conceptual vertical line L, called sweep-line, we reduce the candidates to check for intersections to the segments, which are currently intersecting L. These segments are called active segments.

The status of the sweep line L is the set of active segments ordered from top to bottom along L. The status can change during the sweep at specific coordinates. These coordinates are called events. An event can be one of the following and between any two consecutive events the status remains unchanged:

1. at the leftmost coordinate of a segment, the segment will be marked active 2. at the rightmost coordinate of a segment, the segment will be marked inactive 3. at an intersection the order of segments swap in the sweep line status.

3.5 Implementation & Ply Computation

y

x

L L

0

Figure 3.16: The segments crossing the vertical sweep lineL are marked in red. Whenever

two line segments cross there exists a vertical line crossing both at the samex−coordinate.

At a crossing of two active line segments the order from top to bottom is swapped and

the staus L is updated to status L0.

At an intersection of two line segments, the order from top to bottom will change in the sweep line status. This is illustrated in Figure 3.16, where the order of active

segments is swapped between the status of L and the status of L0.

The second observation is that two line segments can have an intersection only

if they are consecutive in the sweep-line status. Thus, whenever a segment is

marked active, it will be checked for intersections with the segment above and the segment below in the sweep line status. Note that a new active segment requires at most two tests for intersections since it has at most two direct neighbours in the sweep line status. Marking a segment as inactive requires at most one test for intersections, since the top and bottom neighbour might not have been neighboured before. Finally an intersection, and thereby a swap in the order along L, requires at most two tests for intersections, since both candidates might get a new neighbour. The algorithm is executed as follows: Initially for every segment there is one event at the leftmost and one event for the rightmost coordinate. These events are stored in an event-queue ordered by the x-coordinate. For each event we add the segment to the sweep line status, if the event is a leftmost coordinate. The new active segment will be added according to its intersection with the sweep line L and will be tested for intersections with its neighbours. If there is an intersection, the intersection coordinate will be added to the event-queue. If the event is a rightmost coordinate, the segment will be removed from the sweep line status and the neighbours of the removed segment will be checked for intersections, if not done at some state already. Again, if there is an intersection it will be added to the event-

queue. If the event is an intersection of two segments, we report the intersection and the two segments will be swapped in the order of segments in the status and we will check for intersections with the new neighbours, if not done already.

If the event-queue is empty, we have swept the whole plane from left to right and the set of active events is empty. As the invariant of this algorithm, we can guarantee that every crossing to the left of the sweep line L has been detected. Note that during the sweep each pair of neighboured segments have to be tested for intersections exactly once.

As data structures for the event queue and the y-structure balanced binary search trees are used. This can be done, since a total ordering can be enforced. Thereby adding or removing events can be done in O(log n) time as well as the search neighbour operation. The total running time is O((n + k) log n) where n is the number of segments and k is the number of intersections.