Tuesday, March 07, 2006

Sweep line algorithm to computing the intersecting points.

The naive approach to compute the intersection points between line segments is use two for loops
for(int i = 0; i< count ; i++)
for( int j=i+1, j< count; j++)
{
compute the intersection between line[i] and line[j]
}

This is O(n*n) , not very efficient.

A better approach is to use the sweep-line approach. The sweep line will sweep from downwards. It will do some update at some event points.

Three types of event points:

1> The first type: the upper point of a line segment, we need to add that line segment into the set, and compute the intersection between this line segment and its immediate left and right neighbor.

2> The second type: the intersection point. The two line segments exchange position there, so we need to compute the intersection between those two line segments and thier new neighbors.

3> The third type: the end point of a line segment, this line segment will be deleted from the set. Its lef and right neighbor become immediate neighbor, we need to compute the intersections between them.

No comments: