Computational Geometry - two-dimensional geometric algorithms commonly used

Content Reference books - "Algorithms Competition Getting Started Guide classical training."  

  In the process, represented by the polygon vertex array, wherein each of the vertices are arranged in counterclockwise order.

  Determining point is within the polygon. Using the angle method, the basic idea is to calculate polygon points determined with respect to the number of revolutions, specifically, each edge corner of the polygon together, if it is 360 °, described within the polygon; if it is 0 °, the polygon described If it is 180 ° in the polygon edge. This method is unaffected when the number of arcuate polygon processing, only the end of each segment to add up to the starting point of the corner. Also this may not be a triangle or even a simple polygon (i.e., can be selfed).

  However, direct calculation of inverse trigonometric function uses a lot, not only slow and prone to error accuracy. Competition in the algorithm, we do not do that, but there are imaginary ray of a right, through the polygon count how many times the pros and cons of this ray, this is a record number of winding number wn (Winding Number), counterclockwise wear obsolete, Wn of plus 1, while passing through clockwise, Wn of minus 1.

  Note that when the program implemented through determining whether, and through the direction of the cross product is determined by the need of input points in the left or right side.

  Points within the convex polygon determination is simpler, only determines whether all edges in the left (counterclockwise assuming each vertex sorted order) to

  Convex hull.

  Site-directed convex hull is to put inside the enclosed area of ​​the smallest convex polygon. Andrew algorithm based on order levels (faster and more stable than the original Graham). First of all tap x from small to large (if the same x, y in accordance with small to large), the sequence obtained after deleting duplicate points p1, p2, ..., and p1 and p2 into the convex hull. P3 from the beginning, when a new point in the convex hull of the left "forward" direction proceed, otherwise click Delete point recently joined the convex hull until a new point on the left.

(To be added explanation)

 

code show as below:

 1 //判断该点与多边形关系
 2 int isPointInPolygon(Point p, Polygon poly)
 3 {
 4     int wn = 0;
 5     int n = v.size();
 6     for (int i = 0; i < n; ++i)
 7     {
 8         if (isPointOnSegment(p,poly[i], poly[(i+1)%n])) return -1;
 9         int k = dcmp(Cross(poly[(i+1)%n]-poly[i]),p-poly[i]);
10         intdCMP = D1 (poly [I] .y- Py);
 . 11          int D2 = dCMP (poly [(I + . 1 )% n-] .y- Py);
 12 is          IF (K> 0 && D1 <= 0 && D2> 0 ) Wn of ++ ;
 13 is          IF (K < 0 && D2 <= 0 && D1> 0 ) wn-- ;        
 14      }
 15      IF ! (Wn of = 0 ) return  . 1 ;
 16      return  0 ;
 . 17  }
 18 is  // computing the convex hull, the input of points p, the number is p, the output of points ch. Function returns the number of vertices of the convex hull.
19 // input can not have duplicate points. Function execution order of the input point is destroyed after completion.
20  // if you do not want to have the input side of the convex hull points, the two <= change <
 21  // recommended dcmp at high precision comparator 
22 is  int , ConvexHull (Point * P, int n-, Point * CH )
 23 is  {
 24      Sort (P, P + n-);
 25      int m = 0 ;
 26 is      for ( int I = 0 ; I <n-; ++ I)
 27      {
 28          the while (m> . 1 && Cross (CH [M- . 1 ] -CH [M- 2 ], P [I] -CH [M- 2 ]) <= 0) m--;
29         ch[m++] = p[i];
30     }
31     int k = m;
32     for (int i = n-2; i >= 0; --i)
33     {
34         while(m >k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0) m--;
35         ch[m++] = p[i];
36     }
37     if (n > 1) m--;
38     return m;
39 }

 

Guess you like

Origin www.cnblogs.com/125418a/p/11588262.html