310 likes | 740 Views
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
E N D
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 • 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
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
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)] (x2, y2) … (x1, y1) (xn, yn)
CCW 3 • 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 2 1 ccw(p1, p2, p3) returns 1
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.p2 l2.p1 l2.p2 l1.p1
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 h B A
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|
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
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
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:
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
Point in a polygon (III) • One of the simplest algorithm is to compute the winding number of the point
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--
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;}
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
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])
Other issues • Case analysis • Use of floating point numbers • Overflow
References • http://softsurfer.com/, Dan Sunday • Algorithms, Robert Sedgewick • Programming Challenges, Skiena and Revilla