• No results found

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

N/A
N/A
Protected

Academic year: 2021

Share "EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)

GEOMETRIC INTERSECTION

CHAPTER 27

EE602 Algorithms

(2)

The Problem

… “Given a set of N objects, do any two intersect?”

† Objects could be lines, rectangles, circles, polygons, or other geometric objects

… Simple to evaluate physical objects but takes some effort for a computer

… Work with only line intersection as base case

… Consider simple case first and expand implementation

(3)

Methods

… Brute force

… Horizontal and Vertical Lines

… General Line Intersection

(4)

Brute Force

… Check each pair of objects to see if they intersect using ccw

… Time proportional to O(N2)

… Not uncommon to deal with very large number of objects

(5)

Simple case

… Constrain lines to be horizontal or vertical

… Apply general strategy for horizontal-vertical case to lines with no constraints

… Focus on counting problem

… Algorithms can be extended to return all intersecting pairs

(6)

Horizontal and Vertical Lines

A

B

C D G

E H F I

Given a set of N horizontal and vertical lines, do any two intersect?

(7)

Horizontal and Vertical Lines

… Properties of line segment defined by P1(x1, y1) and P2(x2, y2):

† Line segment is vertical if x1 == x2

† Line segment is horizontal if y1 == y2

† All lines have one of these properties

… Idea: Sweep scan line from bottom to top

† Scan line acts like “time”

„ Line segments must occupy same space in same “time” to intersect

… Project Vertical and Horizontal line segments onto scan line

† Vertical line segments appear as single point

† Horizontal line segments appear as an interval

… Intersection found if vertical line segment projection (single point) lies in horizontal line segment (interval) on scan line

† Reduces problem into one-dimensional range search

† “Does interval contain point?”

(8)

Horizontal and Vertical Lines

… Build binary search tree (named x-tree) to represent relationships between vertical line segments

† Node stores lines’ x coordinates

… For vertical line segments:

† If bottom endpoint encountered, add x-coordinate into x- tree

„ If a vertical line is left to the root line, then add it to the left leaf, otherwise add it to right leaf

† If top endpoint encountered, delete from x-tree

… For horizontal line segments:

† Do interval range search using min and max x-coordinates

(9)

Scan Process

C E D G I B F C H B A I E D H F

insert

delete range

insert

insert insert

insert

insert insert

delete

range delete

delete

delete

delete

delete

Sort line endpoints by their y-coordinates

Each vertical line appears twice in list

Each horizontal line appears once

For vertical lines

Bottom endpoint encountered = insert into tree

Top endpoint encountered = delete from tree

For horizontal lines

Endpoints (L, R) encountered = range search A

F B H

E D G

C

I

(10)

Sweep Scanline

A

F B H

E D G

C

I

C

E

D I

B F

D

B

H

H H

(11)

Y-Tree instead of line sweep

… Instead of presorting on y-coordinates to get scan order, use binary search tree for initial y sort (called y-tree)

… y-tree contains all the line endpoints

… Scan is inorder traversal of y-tree A

B

B

I G E

D

C F

I

E

D

H F

C H

Figure 27.5 Sorting for scan using the y-tree

(12)

Generic Program

Procedure Intersection (y-tree) Set x-tree null and count 0

Loop

If y-tree completes traverse, then return count Set focus Å Suitable-pick (y-tree)

If it is a horizontal line, then RangeSearch x-tree and count the intersection numbers

else Update x-tree End-loop

Count: record the intersection number

Suitable-pick: pick one line’s endpoint from y-tree with left priority RangeSearch: take a range search in x-tree for intersection

Update: add line to x-tree when encountering the bottom endpoint; delete line from x- tree when encountering the top endpoint

(13)

Analysis

… Property 27.1: All intersections among N horizontal and vertical lines can be found in time proportional to N log N + I, where I is the number of intersections

† Tree manipulations on average take time proportional to log N

† Range searching depends on total number of intersections

… Bad-case: cross-hatch pattern

† Number of intersections proportional to N2

… If I is known to be large, use brute force (N2)/2 instead of (N log N + N2)

(14)

General Line Intersection

A

B C

D

G E

H

I

F

Given a set of N lines, do any two intersect?

(15)

General Line Intersection

… Changes

† Interval range test no longer sufficient, necessary to explicitly test using the line intersect function

… Sort on (min) y-coordinate to divide space into strips

… Use binary search tree (BST) to store order of lines

† Node stores line

… Proceed through sorted list [same idea as scan]

† If bottom endpoint encountered, add line to BST

† If top endpoint encountered, delete line from BST

… Building tree not that simple…

(16)

Building The Tree

… Use a more general ordering relationship

† Line x is to the right of line y

„ if both endpoints of x are on the same side of y as a point infinitely far to the right

„ Or if y is to the left of x

† Line x is to the left of line x

„ If both endpoints of x are on the same size of y as a point infinitely far to the left

„ Or if y is to the right of x

… Line comparison can be implemented using ccw procedure

(17)

CCW Review

Slope Comparison Collinear Cases

(18)

Line Ordering Example

… B is to the right of F

… F is to the left of B

… H is to the right of B

… B is to the left of H A

B C

D

G E

H

I

F

(19)

Building the Tree – inserting a line

… Line X

† Defined by P0(x0, y0) and P1(x1, y1)

† Already in tree

… Line Y

† Defined by P2(x2, y2) and P3(x3, y3)

† Inserting into tree

… Comparison Test

† If ccw(X.P0, X.P1, Y.P2) == ccw(X.P0, X.P1, Y.P3) == counterclockwise

„ Insert Y as left child of X

† If ccw(X.P0, X.P1, Y.P2) == ccw(X.P0, X.P1, Y.P3) == clockwise

„ Insert Y as right child of X

† If ccw(X.P0, X.P1, Y.P2) != ccw(X.P0, X.P1, Y.P3)

„ Lines must intersect

(20)

Building the tree – comparison operation

… The ordering relationship between lines for the binary tree is more complicated

… Comparison operation is not transitive

… F is the left of B,

… B is to the left of D,

… but F is not to the left of D

A

B C

D

G E

H

I

F

(21)

Building the tree – tree manipulation

… Insertion: Starting at root, compare line

† Each “comparison” performed during the tree manipulation procedures is a line-intersection test

… Deletion: Explicitly test the 2 children of the line being deleted, because comparison is not transitive

… We must test explicitly that comparisons are valid each time we change the tree structure.

† Default BST assumes transitivity

(22)

General Line Intersection

A

B C

D

G E

H

I

F

B

DB G

F

C

H D

(23)

What happens after intersection?

… The y values is likely to be different between the

intersection point and line endpoints

… D and F should swap

places after the point of intersection

A

B C

D

G E

H

I

F

(24)

After Intersection

… Problem: After a point of intersection, the two lines should swap places

… Solution 1: use priority queue instead of binary tree for sort on y-coordinate

† Work the “scan line” by taking the smallest y-coordinate

† When intersection is found, add new entries for each line using the intersection point as the lower endpoints

… Solution 2: remove one of the intersecting lines when intersection found

† Appropriate if not too many intersections expected

† All intersecting pairs must involve removed line

„ After scan is complete, use brute-force to enumerate all intersections

(25)

Analysis

… All intersections among N lines can be found in time proportional to (N+I) log N

† I is the number of intersections

† log N for tree manipulations

… If there is an intersection, we need to add two new lines’ endpoint into the priority queue, so we have N+I lines.

(26)

General geometric shapes

… Rectangles

† Defined by four lines

† Define “to the left of” and “to the right of”

† X is left of Y is right edge of X is left of the left edge of Y

… Circles

† Circle defined as center coordinate and radius

† Use center coordinate to order and compare distance between centers and distance between radii

… Brute force

References

Related documents