Obvious algorithm on2 e d a c b f sweep line algorithm

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: y pair intersect? d e a c b f Line-segment intersection Given n line segments, does any pair intersect? Obvious algorithm: O(n2). e d a c b f Sweep-line algorithm • Sweep a vertical line from left to right (conceptually replacing x-coordinate with time). • Maintain dynamic set S of segments that intersect the sweep line, ordered (tentatively) by y-coordinate of intersection. • Order changes when • new segment is encountered, segment • existing segment finishes, or endpoints • two segments cross • Key event points are therefore segment endpoints. d aa acc abbb e d c b d c b e ed db bf e d a c b f e b d f b ee dd ff Sweep-line algorithm Process event points in order by sorting segment endpoints by x-coordinate and looping through: • For a left endpoint of segment s: • Add segment s to dynamic set S. • Check for intersection between s and its neighbors in S. • For a right endpoint of segment s: • Remove segment s from dynamic set S. • Check for intersection between the neighbors of s in S. Analysis Use red-black tree...
View Full Document

Ask a homework question - tutors are online