21
Advanced Algorithms, Feodor F. Dragan, Kent State University 1 CHAPTER 33 Computational Geometry • Is the branch of computer science that studies algorithms for solving geometric problems. • Has applications in many fields, including • computer graphics • robotics, • VLSI design • computer aided design • statistics • Deals with geometric objects such as points, line segments, polygons, etc. • Some typical problems considered: • whether intersections occur in a set of lines. • finding vertices of a convex hull for points. • whether a line can be drawn separating two sets of points. • whether one point is visible from a second point, given some polygons that may block visibility. • optimal location of fire towers to view a region. • closest or most distant pair of points. • whether a point is inside or outside a polygon.

Computational Geometry - cs.kent.edu

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 1

CHAPTER 33

Computational Geometry• Is the branch of computer science that studies algorithms for solving geometric problems. • Has applications in many fields, including

• computer graphics• robotics,• VLSI design• computer aided design• statistics

• Deals with geometric objects such as points, line segments, polygons, etc. • Some typical problems considered:

• whether intersections occur in a set of lines.• finding vertices of a convex hull for points.• whether a line can be drawn separating two sets of points. • whether one point is visible from a second point, given some polygons that may block visibility.• optimal location of fire towers to view a region.• closest or most distant pair of points.• whether a point is inside or outside a polygon.

Page 2: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 2

Cross products• Line segments

• The convex combination of two distinct points and is any point such that for some real number with

• , the line segment joining and , is the set of all convex combinations of and .• Intuition problem: Show that if (x,y) is a convex combination of and

then

which is the standard equation of a line with slope • Cross products

• let and be points on the plane• The cross product corresponds to the signed area in the parallelogram.

),( 111 yxp = ),( 222 yxp =),( 333 yxp = α ,10 ≤≤ α

).,)(1(),(),( 221133 yxyxyx αα −+=

21 pp 1p 2p1p 2p

),( 11 yx),( 22 yx

21

21

2

2

xxyy

xxyy

−−=

−−=α

1p 2p

21 pp ×

1p

2p 21 pp +.det 121221

21

2121 ppyxyx

yy

xxpp ×−=−=

(0,0)

y

x

Page 3: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 3

• If is negative, then is counterclockwise from . • If is positive, then is clockwise from . • If then and are collinear.

0p

21 pp ×

1p

2p

1op 2op

21 pp × 1op2op

(0,0)

y

x

Prohibited for If is clockwise from

1op1op

2op

,021 =× pp 1op 2op

• To determine if is clockwise from , we translate to the origin and consider where

• Consider now whether two consecutive line segments and turn left or right at .

• Check whether is clockwise from

10 pp 20 pp21 '' pp ×

.',' 022011 pppppp −=−=

).)(())(('' 0102020121 yyxxyyxxpp −−−−−=×

10 pp 21 pp 1p

20 pp 10 pp

1p

2p

0p

1p

2p

0p

0)()( 0102 <−×− ppppSo, counterclockwise or left turn

0)()( 0102 >−×− ppppSo, clockwise or right turn

Cross products (cont.)

Page 4: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 4

Intersection of two line segments• A two stage process is used. The first stage is called quick-rejection.• Bounding Box: The bounding box of a geometric figure is the smallest rectanglethat contains the figure and whose segments are parallel to x and y axis. • The bounding box of the line segment is the box with lower left-hand point where

and with upper right-hand point where

• Intersection of bounding boxes: Rectangles and intersect if and only if the following is true:

• this will not pass the quick rejection test

)ˆ,ˆ( 21 pp 21 pp),ˆ,ˆ(ˆ 111 yxp =

),ˆ,ˆ(ˆ 222 yxp =

),min(ˆ),min(ˆ

211

211

yyyxxx

==

),max(ˆ),max(ˆ

212

212

yyyxxx

==

)ˆ,ˆ( 43 pp)ˆ,ˆ( 21 pp

(0,0)

)ˆˆ()ˆˆ()ˆˆ()ˆˆ( 14321432 yyyyxxxx ≥Λ≥Λ≥Λ≥

)ˆ,ˆ( 21 pp

)ˆ,ˆ( 43 pp

1x

2y4y

1y

3y

2x4x3x

Page 5: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 5

Intersection of two line segments (cont.)• Second stage: Decide whether each segment meets (“straddles”) the line containing the other. • A segment straddles a line if lies on one side of the line and on the other side. (the segment straddles the line also if or lies on the line)• Observation: Two segments intersect iff they pass the quick rejection test and each segment straddles the other. • Testing straddle with cross products:

• we show how to check if straddles the line L determined by and

21 pp 2p1p2p1p

43 pp 2p1p

2p

1p 1p

2p3p

4p

4p3p

If does straddle the line containing and , then the following have different signs.

43 pp

1p 2p

)()()()(

1214

1213

pppppppp

−×−−×−

2p

1p 1p

2p3p

4p

3p

4p

Boundary cases where straddles LAt least one cross product is zero. Both cases pass the quick rejection test.

43 pp

Page 6: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 6

Determining whether any pair of segments intersect.• Given n line segments, determine whether any pair of them intersect.• Sweeping: An imaginary vertical sweep line moves across the x-axis (which acts as a time axis) from left to right. • All line segment endpoints are considered in a left-to-right order. • Whenever endpoint is found, a check for an intersection is made. • Algorithm runs in O(n logn) time, where n is the number of segments. • Algorithm does not look for all intersections, but just whether or not there is at least one intersection. • It takes time in the worst case to find all intersections (see Exercise 33.2-1)• Standard simplifying assumptions:

• no input segment is vertical• no three input segments intersect at a single point.

)( 2nΩ

21 ss t>

• Order definition: Let s1 and s2 be two segments • These segments are comparable at t if the vertical sweep line with x-coordinate t intersects both segments. • If s1 and s2 are comparable at t, then s1 is above s2, written , if s1 intersects the sweep line at t at a higher point than s2. xt

s1

s2

Page 7: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 7

• is a total order for each value of x (reflexive, antisymmetric, transitive)• order changes as x changes (sweep line status and event point schedule).

• a segment obtains an order when its left endpoint is encountered by sweep line.• a segment looses its order when sweep line reaches its right endpoint.

• sweep line behavior near an intersection point

x>

xw

a

b

x>

p

v

• if the intersection point is not on a sweep line, then there exist lines v and wimmediately to the right and left of the intersection.

• the segment that has the higher intersection at v reverses its order with other segment as sweep line passes from v to w.

• if p is the leftmost intersection point and is at intersection of segments a and b, then

• a and b intersect v at adjacent points• a and b are called consecutive at v.

Page 8: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 8

• To maintain a total ordering T on the sweep line as it moves, we need the following operations:

• INSERT(T,s) insert segment s into T• DELETE(T,s) delete segment s from T• ABOVE(T,s) return the segment from T immediately above segment s• BELOW(T,s) return the segment from T immediately below segment s

• Red-black trees can provide a “balanced” search tree which can support above operations in O(log n) time (see Ch. 13 in CLRS)(1) TÅ emptyset(2) Sort segment endpoints from left to right. Break ties by putting point with

lower y-coordinate first.(3) For each point p in the sorted list (in order)(4) if p is left endpoint of segment s then (5) INSERT(T,s)(6) if [ABOVE(T,s) exists and it intersects s] or

[BELOW(T,s) exists and it intersects s] then return TRUE(7) if p is the right endpoint of segment then(8) if both [ABOVE(T,s) and BELOW(T,s) exist] and

[ABOVE(T,s) intersects BELOW(T,s)] then return TRUE(9) DELETE(T,s)(10) return FALSE

The Algorithm ANY-SEGMENTS-INTERSECTION(S)

Animated demohttp://www.lupinho.de/gishur/html/Sweeps.html#segment

Page 9: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 9

Comments and Correctness• Sorting on line (2) will sort points lexicographically and require O(n logn) time.• A boundary case occurs if point p in algorithm also lies on another segment s’. In this case s and s’ should be in consecutive order at p. Correctness Theorem: A call to ANY-SEGMENTS-INTERSECTION(S) returns true iff there is an intersection among the segments of S. • If a true is returned, an intersection has been found between the two segments.• Suppose a false is returned, but an intersection actually exists.• Let p be the leftmost intersection point (break ties by taking lowest y-coordinate).• Let a and b be segments intersecting at p. • Since no intersection occurs to left of p, T gives the correct order prior to p.

• that is, no reversals in order have occurred.• Since no three segments intersect at the same point, there exists a sweep line z at which a and b become consecutive.• z is to the left of p or goes through p.

• There is an endpoint q on z that is the event at which a and b become consecutive.• If p is on sweep line z, then p=q. If not, then q is left of p. x

c

a

b

p

z

q

Page 10: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 10

Correctness and Runtime • In either case, the order of T is correct before q is processed.• Only two cases are possible.

1. Either a or b is inserted into T at z and the other segment is above or below it. (In this case, intersection is detected by line (6) .)

2. Segments a and b are already in T and a segment c between them is deleted at z. (In this case, an intersection is detected by line (8).)

• In either case the intersection of a and b at p is detected, so procedure cannot return a FALSE, as supposed.

x

c

a

b

p

z

q

• Running Time:• sort on line (2) takes O(n logn) time, using merge or heap sort. • since there are 2n events (endpoints) where sweep line stops, the loop iterates at most 2n times.• each iteration of loop takes O(log n) time, since red-black tree operations take O(log n) time. • Total time is O(n logn). READ Ch. 33.1 and Ch 33.2 in CLRS.

Page 11: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 11

Convex Hull Algorithms• Definitions and Properties: Given n points on the plane

– Intersection of all convex sets containing Q– Smallest convex set containing Q– Intersection of all half-planes containing Q– Union of all triangles formed by points of Q– Smallest convex polygon containing Q

– All vertices of hull are some points of Q

rubber band

NOTE: convex-hull(Q) is the closed solid region, not just the boundary CH(Q)

extreme point

not extreme pointalways unique

.,...,, 21 npppQ =

Page 12: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 12

• Problem: Given n points on the plane find CH(Q).

• Approaches:• Brute Force• Gift Wrapping• QuickHull• Graham Scan• Incremental• Divide and Conquer• By Delaunay Triangulation & Voronoi Diagram

,,...,, 21 npppQ =

• Brute-Force Approach • Determine extreme edges:for each pair of points p,q∈Q do

if all other points lie on one side of line passing thru p and q

then keep edge (p, q)

• Sort edges in counterclockwise order (we want output in counterclockwise)

pq

• Running time: O(n3)– bad but not the worst yet

The Problem and Approaches

Page 13: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 13

Gift Wrapping or The Jarvis March Algorithm

Method:– Let p0 be the lowest point (take leftmost point in case of tie)– The next convex hull vertex p1 has the least polar angle w.r.t. the positive

horizontal ray from p0. – Measure in counterclockwise– If tie, choose the furthest such point

– Vertices p2, p3,…, pk are picked similarly where pk is the highest point (leftmost)– The sequence p0, p1, p2, p3,…, pk is the right chain of CH(Q).– To choose the left chain of CH(Q), start with pk

• First suggested by Chand and Kapur (1970). Then by Jarvis (1973). • Worst-case time: O(n2)• Output-sensitive time: O(nh), where h is the # of vertices of hull• Can be extended to higher dimension

– was the primary algorithm for higher dimensions for quite some time

pk

p0

• choose pk+1 as the point with least angle w.r.t. the negative horizontal ray from pk

again, measure in counterclockwise direction and if tie occurs, choose furthest such point.

• continue in same fashion until you obtain pk, pk+1,…, pt= p0

Page 14: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 14

Complexity of Jarvis March • For each vertex p from CH(Q), it takes

• O(1) time to compare the polar angle of and using cross-product• O(n) time to find minimum polar angle• O(n) total time

• If CH(Q) has h vertices, then the running time is O(nh). • In worst case h=O(n), hence is worst case time bound. Worst case occurs

when O(n) of points lie on the convex hull, e.g., all points lie on a circle.• If points in Q are generated by random generator, then we expect h= O(logn)• In practice, Jarvis march is normally very fast.• If h=o(log n), then this algorithm is asymptotically fastest.

jpq ipq

)( 2nO

Theorem: is a lower bound for computing the convex hull.)log( nnΩ• Assume we want to sort n numbers

• create n points on plane where

• construct

• get from description of CH original numbers in sorted order

• Conclusion: If we can construct CH in o(n logn) time then we can sort n numbers in o(n logn) time, which is impossible.

,...,, 21 nxxx

).,...,1(),( 2 nixyyxp iiiii ===),...,,( 21 npppCHCH =

(0,0)

y

x

Page 15: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 15

Graham Scan Algorithm• First a base point p0 is selected. Normally this is the point with minimum y-coordinate (select leftmost in case of tie)• Next all points are sorted w.r.t. the polar angle they make with a half-ray with left endpoint p0 and parallel to x-axis. (!!! We do not need to compute those angles!!!)

• Remaining points are stored in counterclockwise order w.r.t. p0

• Let p0, p1, p2, p3,…, pm be the sorted list of remaining points.• Clearly, p0 and p1 are in CH(Q).• Let S be a stack in which points that are potentially convex hull points will be stored. • Initially S = p0 p1 p2 . Remaining steps of the algorithm follow

for i Å 3 to mdo while (the angle formed by points NEXT_TO_TOP(S) , TOP(S), and pi

make a non-left turn) POP(S)PUSH(S, pi)

return S

p0

p1p2

p3

removep4

p5p6

p7

Interior points on ray cannot be convex hull points and are

removed during sort.

)( nm ≤

Page 16: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 16

Example and Runtime Computation

• Requires O(n) to find p0• Sorting based on polar angle takes O(n logn) time• Removal of n-m points with duplicate angles takes O(n).• For loop is executed m-2 times, hence O(n).• Interior while statement is a “problem”. It may iterate as many as O(n) time. • Above observation can easily lead to an over-estimate of • Note that each pass through while statement, POP is executed. • As in analysis of MULTIPOP, there is at most one POP operation for each PUSH operation (see amortized analysis)• Since p0, p1, pm are not popped, at most m-3 POP operations occur. • Note, both POP and test for while take O(1) time and . Hence amortized cost for each iteration of while loop is O(1).• Overall worst-case cost of for-loop is O(n)• Worst-case running time of the algorithm is O(n logn) (= O(n logn) + O(n)).

p0

p1p2

p3

Note straight line here.p4 is eliminated, as non-left turn.

p4p5

p6p7

nm ≤

p8 CH(Q)=p0, p1, p3, p5, p7 , p8

).( 2nO

Page 17: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 17

Correctness of Graham ScanClaim 1: Each point p popped from stack S is not a vertex of CH(Q) and lies inside

new CH(S).• Suppose pj is popped from S because makes a non-left turn. • Since points are ordered in increasing polar angle w.r.t p0 , the points lie in counterclockwise order w.r.t. p0and contains pj• Then pj cannot be a vertex of CH(Q).

Claim 2: The points on stack S always form the vertices of a convex polygon.

• The claim holds initially for points p0, p1, p2. They form a triangle.• The stack changes by points being popped or pushed.• If a point is popped, then a vertex is removed from convex polygon, leaving the

resulting polygon convex. • If a point pi is pushed, the resulting points of S form a convex polygon.

• by choice of p0, p1, pi lies above p0 p1 (see diagram on next slide). . • since a left turn occurs at pj, pi lies below pj pk. • since pi occurs later in the sequence than pj , the polar angle of p0 pi.

is greater than the polar angle of p0 pj. • thus, pi lies to the left of line p0 pj,, i.e., pi lies in the shaded region in diagram. • so, after pi is pushed on the stack, a convex region results.

ijk ppp∠

ijk ppp

ijk ppp∆

p1

pk

pj

pi

p0

Page 18: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 18

Correctness of Graham Scan (cont.)

Claim 3: At the conclusion of the algorithm, the points of S joined together consequently form a convex polygon that contains Q.

• By claim 1, all points dropped from S are not vertices of CH(Q).• By claim 2, the points of S are points from Q that form a convex polygon• By construction and claim 1, all points of Q are either in S or lie interior to

CH(S’) for some earlier S’ with CH(S’) CH(S).• Then, at conclusion the points of S joined together consequently give a

convex polygon that contains all points of Q.

p1

pk

pi

pj

p0

Diagram

Animated demoshttp://www.cs.princeton.edu/~ah/alg_anim/version0/Graham.htmlhttp://www.gris.uni-tuebingen.de/gris/grdev/java/algo/solutions/lesson12/ConvexHull.htmlhttp://www.piler.com/convexhull/http://www.geom.umn.edu/software/qhull/

Page 19: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 19

Closest Pair of Points• Given n points on the plane, find closest pair of points.

– The Euclidean distance between two points and is

– An obvious but naïve approach is to compute the distance between any two points and take minimum. However, running time is

),( 111 yxp = ),( 222 yxp =2

212

2121 )()(),( yyxxppd −+−=

).(2

2nOn

=

• A high-level description of a much better algorithm (at least for large sets) is given below.• Let Q be a set of n planar points. • If |Q|<4, then the distances between all pairs of points are computed and the closest pair is reported.• If |Q|>3, then a “Divide & Conquer & Combine” procedure is followed. • Each recursive call receives as input

• a set P Q• arrays X and Y containing points P sorted by x and y coordinates, respectively

Page 20: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 20

Divide and Conquer Steps

• Find a vertical line L that bisects the points in P into P’ and P’’ with almost equal sizes. All points in P’ are left of L or on L, all points in P’’ are right of L or on L.

• The array X is divided into arrays X’ and X’’ which contain the points of P’ and P’’, respectively, sorted by x-coordinates. • Likewise the array Y is split into arrays Y’ and Y’’ which contain the points of P’and P’’, respectively, sorted by y-coordinates.• At the beginning, Q has to be presorted by x-coordinates to obtain X and by y-coordinates to obtain Y. This we do before the first recursive call. • Now, on each recursive call, the set P can easily be split in linear time, giving sets P’, P’’, X’, and X’’.

• Both Y’ and Y’’ can be formed by Y in linear time by walking through Y and putting each element in Y’ (or Y’’) if it is in P’ (respectively, P’’).

Divide step

Conquer Step• Make two recursive calls to find closest pair of points in P’ and P’’, respectively.

• First call gets input (P’, X’, Y’)• Second call gets input (P’’,X’’,Y’’)

• Set closest pair returned for P’ and P’’ have distance d’ and d’’, respectively. • Set d = mind’, d’’

Page 21: Computational Geometry - cs.kent.edu

Advanced Algorithms, Feodor F. Dragan, Kent State University 21

Combine Step and Runtime

Runtime• The initial preprocessing step costs O(n logn)• The recurrence relation for the recursive portion is T(n),

where • Its solution is T(n)= O(n logn). This gives also the total running time.

L

ddd

dL

Possible coincident points, one in P’ and one in P’’.

Combine• Observe that if there is a pair in P closer than d, then they must lie in a box about L, as follows.• There can be at most 8 points in each strip.• Find the array Y consisting of the points of Y within d of L, sorted by their y-coordinate.

dd 2×

dd 2×

• Next, for each point p from Y, check to see if d(p,q)<d for each of the 7 points q that follow p in Y.• Find the closest-pair distance d, computed in preceding step over all points p in Y.• If d<d, then the closest pair with distance d is returned. Otherwise, the closest pair formed by the two recursive calls is returned.

≤>+=

3)1(3)()2/(2)(

nifOnifnOnTnT

READ Ch. 33.3 and 33.4 in CLRS.