Computational Geometry

20
Computational Geometry Some useful algorithms/formulas for solving geometric problems

description

Computational Geometry. Some useful algorithms/formulas for solving geometric problems. Overview. Representation Basic geometric problems Other issues. Representation. Points (x, y) Cartesian coordinates (r, Θ ) Polar coordinates Segments Position of the end points Lines - PowerPoint PPT Presentation

Transcript of Computational Geometry

Page 1: Computational Geometry

Computational Geometry

Some useful algorithms/formulas for

solving geometric problems

Page 2: Computational Geometry

Overview Representation Basic geometric problems Other issues

Page 3: Computational Geometry

Representation Points

(x, y) Cartesian coordinates (r, Θ) Polar coordinates

Segments Position of the end points

Lines 2-tuple (m, c) using y = mx + c 3-tuple (a, b, c) using ax + by = c Two points P0, P1 on the line using P(t) = (1-t)P0 + t P1

Polygon Ordered list of points

Page 4: Computational Geometry

Basic geometric problems Area of polygon CCW Line segment intersection Distance between a point and a line Closest point Angle between two lines Point in a polygon Convex hull

Page 5: Computational Geometry

Area of polygon Given a simple polygon, find its area.

Area = 1/2 * [(x1.y2 + x2.y3 +…+ xn.y1) - (y1.x2 + y2.x3 + … + yn, x1)]

(x1, y1)

(x2, y2)

(xn, yn)

Page 6: Computational Geometry

CCW Basic geometric primitive

Returns 1 if the points are ccw Returns -1 if the points are cw Returns 0 if the points are

collinear Area given in the previous slide is

positive when the points are listed in counter-clockwise direction and negative otherwise

3

1 2

ccw(p1, p2, p3) returns 1

Page 7: Computational Geometry

Line segment intersection (I) Given two line segments l1 and l2,

determine if they intersect ?

isIntersect = ccw(l1.p1, l1.p2, l2.p1) *ccw(l1.p1, l1.p2, l2.p2) <= 0 && ccw(l2.p1, l2.p2, l1.p1) * ccw(l2.p1, l2.p2, l1.p2) <= 0

l1.p1

l1.p2l2.p1

l2.p2

Page 8: Computational Geometry

Distance between a point and a line Given a point and a line, determine

the shortest distance between them.

Select two points A, B on the line ½ * AB * h = area of triangle ABP

P

A Bh

Page 9: Computational Geometry

Closest point (I) Given a line L and a point P, find the

point on L closest to P Recall that if u is a unit vector and v

is any vector. The projection of v on u is given by (u.v)u

u.v is the number k which minimized |v – ku|

Page 10: Computational Geometry

Closest point (II) The theorem in the previous slide

can only be applied to lines which pass through the origin

This is sufficient to compute the general case as we can apply a translation if the line does not pass through the origin

Page 11: Computational Geometry

Angle between two lines Represent lines as vectors u (a, b)

and v (c, d) Definition of dot product:

u.v = |u||v|cos Θ = ac + bd |u| = sqrt(u.u) Θ = cos-1(u.v / |u||v|)

Θ

u

v

Page 12: Computational Geometry

Point in a polygon (I) Given a point and a closed polygon,

determine whether the point lies inside the polygon.

Most algorithms extends a ray from the given point and considers the interaction of the polygon with the ray

Such algorithms needs to handle the following boundary cases:

Page 13: Computational Geometry

Point in a polygon (II) A standard way to resolve this issue

is to adopt the following set of rules Edge crossing rules

an upward edge includes its starting endpoint and excludes its endpoint

a downward edge excludes its starting endpoint and includes its endpoint

horizontal edges are excluded the edge-ray intersection point must be

strictly right of the point P

Page 14: Computational Geometry

Point in a polygon (III) One of the simplest algorithm is to

compute the winding number of the point

Page 15: Computational Geometry

Point in a polygon (IV) Winding number

number of times the polygon winds around the point

Point is outside iff WN = 0 Upward edges => WN++ Downward edges => WN--

Page 16: Computational Geometry

Point in a polygon (V) //      Input:   P = a point,

//               V[] = vertex points of a polygon with V[n]=V[0]//      Return: wn = the winding number (=0 only if P is outside V[])int wn_PointInPoly(Point P, Point* V, int n){    int wn = 0;    // the winding number counter    // loop through all edges of the polygon    for (int i=0; i<n; i++) {   // edge from V[i] to V[i+1]        if (V[i].y <= P.y) {         // start y <= P.y            if (V[i+1].y > P.y)      // an upward crossing                if (ccw( V[i], V[i+1], P) > 0)  // P left of edge                    ++wn;            // have a valid up intersect        }        else { // V[i].y > P.y            if (V[i+1].y <= P.y)     // a downward crossing                if (ccw( V[i], V[i+1], P) < 0)  // P right of edge                    --wn;            // have a valid down intersect        }    }    return wn;}

Page 17: Computational Geometry

Convex hull (I) Given a set of points, determine the

smallest convex set containing all the points

A set S is convex if whenever two points P and Q are inside S, then the whole line segment PQ is also in S

Page 18: Computational Geometry

Convex hull (II) One simple algorithm is Graham Scan, often cited

as the first computational geometry algorithm Pseudocode

Select the lowest leftmost point P[0] in S Sort the rest of the points in S according to the angle

made with P[0], break ties base on distance to P[0] Let P[0..n-1] be the sorted array of points Create an empty stack ST ST.push(P[n-1]) //P[n-1] must be on the hull ST.push(P[0]) //P[0] must be on the hull For i from 2 to n – 1

Let PT1 denote the topmost point on ST Let PT2 denote the second topmost point on ST while (ccw(PT2, PT1, P[i]) <= 0) ST.pop() ST.push(P[i])

Page 19: Computational Geometry

Other issues Case analysis Use of floating point numbers Overflow

Page 20: Computational Geometry

References http://softsurfer.com/, Dan Sunday Algorithms, Robert Sedgewick Programming Challenges,

Skiena and Revilla