Computational Geometry - PowerPoint PPT Presentation

About This Presentation
Title:

Computational Geometry

Description:

Computational Geometry. Some useful algorithms/formulas for ... (x, y) Cartesian coordinates (r, T) Polar coordinates. Segments. Position of the end points ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 21
Provided by: mel9153
Category:

less

Transcript and Presenter's Notes

Title: Computational Geometry


1
Computational Geometry
  • Some useful algorithms/formulas for solving
    geometric problems

2
Overview
  • Representation
  • Basic geometric problems
  • Other issues

3
Representation
  • Points
  • (x, y) Cartesian coordinates
  • (r, T) 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

4
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

5
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)
6
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
7
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) lt 0
    ccw(l2.p1, l2.p2, l1.p1)
    ccw(l2.p1, l2.p2, l1.p2) lt 0

l1.p2
l2.p1
l2.p2
l1.p1
8
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
9
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

10
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

11
Angle between two lines
  • Represent lines as vectors u (a, b) and v (c, d)
  • Definition of dot product
  • u.v uvcos T ac bd
  • u sqrt(u.u)
  • T cos-1(u.v / uv)

u
T
v
12
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

13
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

14
Point in a polygon (III)
  • One of the simplest algorithm is to compute the
    winding number of the point

15
Point in a polygon (IV)
  • Winding number
  • number of times the polygon winds around the
    point
  • Point is outside iff WN 0
  • Upward edges gt WN
  • Downward edges gt WN--

16
Point in a polygon (V)
  • //      Input   P a point,//              
    V vertex points of a polygon with
    VnV0//      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 i0 iltn i)    // edge
    from Vi to Vi1        if (Vi.y lt P.y)
             // start y lt P.y            if
    (Vi1.y gt P.y)      // an upward
    crossing                if (ccw( Vi, Vi1,
    P) gt 0)  // P left of edge                   
    wn            // have a valid up
    intersect                else // Vi.y gt
    P.y            if (Vi1.y lt P.y)     // a
    downward crossing                if (ccw( Vi,
    Vi1, P) lt 0)  // P right of
    edge                    --wn            //
    have a valid down intersect               
    return wn

17
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

18
Convex hull (II)
  • One simple algorithm is Graham Scan, often cited
    as the first computational geometry algorithm
  • Pseudocode
  • Select the lowest leftmost point P0 in S
  • Sort the rest of the points in S according to the
    angle made with P0, break ties base on distance
    to P0
  • Let P0..n-1 be the sorted array of points
  • Create an empty stack ST
  • ST.push(Pn-1) //Pn-1 must be on the hull
  • ST.push(P0) //P0 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, Pi) lt 0) ST.pop()
  • ST.push(Pi)

19
Other issues
  • Case analysis
  • Use of floating point numbers
  • Overflow

20
References
  • http//softsurfer.com/, Dan Sunday
  • Algorithms, Robert Sedgewick
  • Programming Challenges, Skiena and Revilla
Write a Comment
User Comments (0)
About PowerShow.com