ELEMENTARY GEOMETRIC METHODS (CH. 24) By Yuan Liu EE602 Fall 2010 OUTLINE | Line-Point Relationship (CCW) -Given a line and a point, is the point on the left side(CCW=counterclockwise), right side (CW=clockwise) or collinear? | Line Intersections -Do two given line segments intersect? | Simple Closed Path - Find a closed path through a set of N given points that does not intersect itself and returns to the start point. | Inclusion in a Polygon -Given a point and a polygon, determine whether the point is inside or outside the polygon. 2 GEOMETRIC DATA STRUCTURES Point - pair of integers y struct point {int x, y; char c;}; | Line – a pair of points connected by a straight line segment y struct line {struct point p1, p2;}; | Polygon – a list of points making a closed figure y struct point polygon[Nmax]; | Other data structures are available and may be convenient depending on the application. (e.g. polygon also represented as an array of lines, each point on the polygon included twice to define each line) | 3 1、Line-Point Relationship (CCW) z Given a line and a point, is the point on the left side (CCW=counterclockwise), right side (CW=clockwise) or collinear? z Applications – Fundamental calculation to determine relationship 4 DIRECTED SEGMENT | AB a line segment. AB is the directed segment which is taken AB with direction. | let 2-D vector, O= (0, 0), P = ( x1, y1 ),Q = ( x2 , y2 ) ¾ OP + OQ = ( x1 + x2, y1 + y2 ) ¾ OP - OQ = ( x1 - x2 , y1 - y2 ) ¾ OP × OQ = x1*y2 – x2*y1 =|OP||OQ| sinΘ ( -180° ≤ Θ ≤180°) 5 COUNTERCLOCKWISE (CCW) OP × OQ = x1*y2 – x2*y1 =|OP||OQ| sinΘ ( -180° ≤ Θ ≤180°) P1 P2 O P3 ¾In the Ⅰ and Ⅱ quadrant, OP × OQ ≥ 0 ( 0 ≤ Θ ≤180°) Q P4 ¾In the Ⅲ and Ⅳ quadrant, OP × OQ ≤ 0 (-180°≤ Θ ≤ 0) 6 COUNTERCLOCKWISE (CCW) | If OP × OQ < 0 , P in the clockwise direction of OQ. | If OP × OQ > 0 , P in the counterclockwise direction of OQ. | If OP × OQ = 0 , OP and OQ are collinear (P is in OQ) at same direction or reverse. 7 2、LINE INTERSECTION | Do two given line segments intersect? | Applications y CAD y Optimizing Bus Routes 8 EXAMPLES | Line Intersection 9 CONDITIONS z If both endpoints are on different sides of each other, the lines must intersect! z If one of the endpoints is in the second line segment, the lines also intersect! P S D R Q (P×R)*(Q×R) ≤ 0 && ( S×D)*( (-P)×D ) ≤ 0 10 Line Intersection Summary z There are several ways to calculate two lines crossing, but CCW will be useful when extended to deal with N lines. z The computation complexity is fixed since it is not extendable to N. 11 3、Simple Closed Path z Find a closed path through a set of N given points that z does not intersect itself and returns to the start point. Applications – Mechanical Plotter – Optimizing route for Mailman 12 Simple Closed Path Algorithm • pick an anchor • compute the angle (made by drawing a line from points and out in positive horizontal direction) • sort by the magnitude of the angles • connect adjacent points 13 Simple Closed Path z O(N) to calculate the angle of EACH point z O(Nlog N) to sort by the angles z O(N) for simple closed path solution based on angle sort 14 Method z Choose point with min ordinate value (min y) as anchor z “min y” becomes the reference for the angle calculation z The angle can be calculated by – arctan(dy/dx) z To avoid dx=0, one could alternatively use a slope ratio to provide the same ordering property – dy/(dy+dx) 15 4、Inclusion in a Polygon z Given a point and a polygon, determine whether the point is inside or outside the polygon. z Applications – CAD 16 Inclusion in a Polygon Algorithm z Draw a radial starting from P to left horizontal so that the tail of the radial is outside the polygon. Count the number of time it crossing the polygon boundary – The point is inside if the count is odd – The point is outside if the count is even z Special cases not handled well 17 SPECIAL CASES Radial L intersect polygon with a vertex, Count only once. Ignore the points that coincide with the collinear line segment Ignore the intersection with the vertex P belongs to polygon because it is in one of the line segment 18 TRICK when radial L intersect polygon with vertex, if the ordinate of the vertex is bigger than other points’ ordinate belonging to the same line segment, count the number; otherwise ignoring. | without considering the horizontal line segments of polygon | if P is in the line segment which belongs to polygon, P is inside of polygon. | 19 Inclusion in a Polygon Summary z O(N) to count the number of intersection 20 Summary z The apparent complexity of a problem that is easily solved visually may be significantly more difficult to calculate. z Distribution, order, and the use of trigonometric functions may all affect the performance of geometric algorithms. 21