cse53111 computational geometry topics q preliminaries q point in a polygon q polygon construction q...

22
CSE5311 1 Computational Geometry TOPICS Preliminaries Point in a Polygon Polygon Construction Convex Hulls Further Reading

Upload: everett-wheeler

Post on 25-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 1

Computational Geometry

TOPICS Preliminaries Point in a Polygon Polygon Construction Convex Hulls

Further Reading

Page 2: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 2

Geometric Algorithms

Geometric Algorithms find applications in such areas as Computer Graphics Computer Aided Design VLSI Design GIS Robotics

We will study algorithms dealing with

points, lines, line segments, and polygons

In particular, the algorithms will Determine whether a point is inside a Polygon Construct a Polygon Determine Convex Hulls

Page 3: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 3

Preliminaries:

A point p is represented as a pair of coordinates (x,y)A line is represented by a pair of pointsA path is a sequence of points p1,p2, . . . pn and the line segments connecting them,

p1-p2, p2-p3, . . . , p k-1-pk.

A closed path whose last point is the same as the first is a polygon.A simple polygon is one whose corresponding path does not intersect itself. It encloses a region in the plane.

A convex Polygon is a polygon such that any line segment connecting two points inside the polygon is itself entirely in the polygon.The convex hull of a set of points is defined as the smallest convex polygon enclosing all the given points.

Page 4: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 4

A line segment connecting two points: The points are inside the polygonThe line segment is not entirely in the polygon

This is not a convex

polygon

p(x1,y1)

q(x2,y2)

r(x3,y3)

s(x4,y4)

Page 5: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 5

Determining whether a point is inside a polygon

Given a simple polygon polygon P, and a point q, determine whether the point is inside or outside the polygon. (a non-convex polygon)

Page 6: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 6

Procedure Point_in_a_Polygon(P,q)

Input : P ( a simple polygon with vertices p1,p2, … pn, and edges e1,e2,e3, … en and q (x0,y0) a point.

Output: INSIDE ( a Boolean variable, True if q is inside P, and false otherwise)

Count 0;

for all edges ei of the polygon do

if the line x = x0 intersects ei then

yi y coordinate of the intersection between lines ei and x=x0;

if yi > y0 then

Count Count +1;

if count is odd then INSIDE TRUE;

else INSIDE FALSEThis does not work if the line passes through terminal points of edges

Page 7: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 7

It takes constant time to perform an intersection between two line segments.The algorithm computes n such intersections, where n is the size on the polygon.Total running time of the algorithm, O(n).

Page 8: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 8

Constructing a Simple Polygon

Given a set of points in the plane, connect them in a simple closed path.

Consider a large circle that contains all the points.Scan the area of C by a rotating line. Connect the points in the order they are encountered in the scan.

Page 9: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 9

Procedure Simple_Polygon

Input : p1,p2, . . . pn (points in the polygon)Output : P ( a simple polygon whose vertices p1,p2, . . . pn are in some order)p1 the point with the max ‘x’ value.

1. for i 2 to n2. i angle between line p1-pi and the x-axis;3. sort the points according to the angles

(use the corresponding priority for the point and do a heapsort)

4. P is the polygon defined by the list of points in the sorted order.

Complexity : Complexity of the sorting algorithm.

Page 10: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 10

Convex Hulls

The convex hull of a set of points is defined as the smallest convex polygon enclosing all the points in the set.

The convex hull is the smallest region encompassing a set of points.A convex hull can contain as little as three and as many as all the points as vertices.

Problem Statement : Compute the convex hull of n given points in the plane.

There are two algorithmsGift Wrapping O(n2)Graham's Scan O(nlogn)

Page 11: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 11

p1

p2

p3

p4

p5

p6

L

Page 12: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 12

Procedure Gift_Wrapping(p1,p2, . . . pn)

Input : p1,p2, . . . pn ( a set of points in the plane)Output : P (the convex hull of p1,p2, . . . pn )

1. P {0} or ;2. p a point in the set with the largest x-coordinate;3. Add p to P;4. L line containing p and parallel to the x-axis;5. while P < n do 6. q point such that the angle between the line -p-q-

and L is minimal among all points; 7. add q to P; 8. L line -p-q-;9. pq;

Page 13: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 13

Graham's Scan:

Given a set of n points in the plane, ordered according to the algorithm Simple Polygon, we can find a convex path among the first k points whose corresponding convex polygon encloses the first k points.

qm-1

p1

pk+1

qm

Page 14: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 14

Procedure Graham's Scan(p1,p2, . . . pn)

Input : p1,p2, . . . pn(a set of points in the plane)Output : q1,q2, . . . qn( the convex hull of p1,p2, . . . pn)

p1 the point in the set with the largest x-coordinate(and smallest y-coordinate if there are more than one point with the same x-coordinate)

Construct Simple Polygon and arrange points in order Let order be p1,p2, . . . pn

q1 p1;q2 p2;q3 p3; (initially P consists of p1,p2,and p3)m 3;for k 4 to n do while the angle between lines -q m-1-qm- and -q m-pk - 180o do

m m-1; m m+1; qm pk;

[Internal to the polygon]

Page 15: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 15

p1

p2

p3

p4

p5

p6

p7

p8

q1:p1

q2:p2

q3:p3

m:3

p4

q1

q2

q3

p5

p6

p7

p8

-q2-q3- and -q3-p4-

k =4

The angle is < 180

Page 16: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 16

q4

q1

q2

q3

p5

p6

p7

p8

qm:p4

m:4

-q3-q4- and -q4-p5-

q4

q1

q2

q3

q5

p6

p7

p8

qm:p5

m:5

k =5

k=6

Page 17: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 17

Angle between -q4-q5- and

-q5-p6- is greater than 180

Therefore m = m-1 = 4

We skip p5

Angle between -q3-q4- and

-q4-p6- is greater than 180

Therefore m = m-1 = 3

We skip p4

q4

q1

q2

q3

q5

p6

p7

p8

k=6

-q3-q4- and -q4-p6-

q1

q2

q3p6

p7

p8

q4

k=6

Page 18: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 18

q1

q2

q3p6

p7

p8Angle between -q2-q3- and

-q3-p6- is less than 180

Therefore m = m+1 = 4

and q4 = p6

k=6

Page 19: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 19

q1

q2

q3q4

p7

p8

k=7

q1

q2

q3q4

q5

p8

k=8

Page 20: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 20

q1

q2

q3q4

q5

q6

Page 21: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 21

Procedure Graham's Scan(p1,p2, . . . pn)

Input : p1,p2, . . . pn(a set of points in the plane)Output : q1,q2, . . . qn( the convex hull of p1,p2, . . . pn)

p1 the point in the set with the largest x-coordinate(and smallest y-coordinate if there are more than one point with the same x-coordinate)

Construct Simple Polygon and arrange points in order Let order be p1,p2, . . . pn

q1 p1;q2 p2;q3 p3; (initially P consists of p1,p2,and p3)m 3;for k 4 to n do while the angle between lines -q m-1-qm- and -q m-pk - 180o do

m m-1; m m+1; qm pk;

[Internal to the polygon]

Page 22: CSE53111 Computational Geometry TOPICS q Preliminaries q Point in a Polygon q Polygon Construction q Convex Hulls Further Reading

CSE5311 22

Exercise Problems

1. Let P be a simple (not necessarily convex) polygon enclosed in a given rectangle R, and q be an arbitrary point inside R. Design an efficient algorithm to find a line segment connecting q to any point outside R such that the number of edge of P that this line intersects is minimum.

2. Let P be a set of n points in a plane. We define the depth of a point p in P as the number of convex hulls that need to be ‘peeled’ (removed) for p to become a vertex of the convex hull. Design an O(n2) algorithm to find the depths of all points in P.

3. Given a set of n points in the plane P. A straight forward or brute force algorithm will take O(n2) to compute a pair of closest points. Give an O(nlog2n) algorithm find a pair of closest points. You get a bonus if you can give an O(n log n) algorithm