Graham Scan convex packet algorithm

Source: Internet
Author: User

The algorithm for convex hull is one of the most basic algorithms in computational geometry. There are many algorithms for finding convex hull, and theGraham scan algorithm is a very simple and efficient two-dimensional convex hull algorithm that can find convex hull in O(NLOGN) time.

First, we introduce the cross product of two-dimensional vectors (here and the True cross product is different): for two-dimensional vector a= (x1,y2) and b= (x2,y2),axb is defined as x1*y2-y1*x2. and the geometrical meaning of it is | a| | b|sin<a,b>. If the angle of a and b is less than 180 degrees (counterclockwise), then this value is positive, greater than 180 degrees is negative. It is important to note that the left and right multiply are different. :

The method of the Graham scan algorithm is to first set a starting point, generally the leftmost point and the rightmost point, and then sweep through the past, if the new points and the points that have been found before the "shell" convex nature has not changed, continue to sweep, otherwise the last point has been found to delete, and then compare convexity, Until the convexity is not changed. Scan up and down two "shells", respectively, together, the convex hull is found. This is very abstract, we look at the picture to explain:

We find the "shell", up and down is actually the same. First add two points A and C:

Then insert the third point G, and calculate the cross product of ACXCG, but found that the cross product is less than 0, that is, the counter-clockwise direction of ∠ACG greater than 180 degrees, so delete C point, add G point:

Then you can follow this step to join the D point. Above ad, start with D. will be able to find AGD and dfea two convex shells. A convex hull is obtained by merging.

The order of the scans, there are two kinds of coordinate order and polar order. The coordinate order is the x-coordinate of the comparison between two points, if the small one is scanned first (in turn when the convex hull is scanned), and if the two points have the same x coordinate, then the y-coordinate is compared, and the small one is scanned first (and the other is reversed when the convex hull is scanned). The polar order is compared with the return value of the ARCTAN2 function, which I did not write so it is not clear.
Program can be written very concise, the following is I write in C + + convex package Program

/*d[] is an array of point, with a point that has two two properties X and Y, while the subtraction operation and the Det (cross product) are supported. The convex array holds the number of points of the selected convex hull, Ctotal is the number of midpoints of the convex hull */bool cmppoint (Const Point &a,Const Point &b)Comparison function used to compare the coordinate order {if (a.x!=b.x)Return a.x<b.x;return A.Y&LT;B.Y;} void Get_convex_hull () {SortDD+N,cmppoint); Inttotal=0,tmp;for (int i=0;i<N;++i)Scan under convex hull {while ((total>1) && ((d[convex[total-1]]-d[convex[TOTAL-2]]).DetObtains the vector of the last two points in a convex hulld[i]-d[convex[total-1]]) <=0)) total--; //get ready to insert the point and the last point in the convex hull of the vector, calculate the cross product Convex[total++]=i;} tmp=total; for (int i=n-2;i>=0;--i) // Scan on convex hull {while ((total>tmp) && ((d[convex[total-1]]-d[convex[total-2]]). det (d[i]-d[convex[total-1]]) <=0) total--; Convex[Total++ ]=i; } ctotal=total;}            

Let's take a look at a question: POJ1113 Wall, test instructions is to give some points, find a closed curve C, so that C can wrap all the points, and the given point to C is the smallest distance to L, ask the circumference of C. A little painting will tell you that the perimeter of C is the circumference of the convex hull of these points, plus the circumference of the circle with a radius of L. So I asked for a convex bag plus 2πl. My program is as follows:

#Include<cstdio>#Include<cstring>#Include<cstdlib>#Include<algorithm>#Include<cmath>UsingStd::sort;#Define MAXN 1002int n,l;DoubleSqr(Double A) {return a*a; }struct point{Double x, y;Inline pointoperator-(Const Point &t) {point ret; ret.x=x-t.x; ret.y=y-t.y;return ret; }Inline pointoperator+ (Const Point &t) {point ret; ret.x=x+t.x; ret.y=y+t.y;return ret; }InlineIntDet(Const Point &t) {return x*t.y-t.x*y; }InlineDoubleDist(Point &t) {Returnsqrt (SQR (x-t.x) +SQR (Y-T.Y)); }}D[MAXN];boolCmppoint(Const Point &a,Const Point &b) {if (a.x!=b.x)Return a.x<b.x;return A.Y&LT;B.Y;}int convex[maxn],ctotal;voidGet_convex_hull() {sort (d,d+n,cmppoint);int total=0,tmp;for (int i=0;i<n;++i) {while ((total>1) && ((d[convex[total-1]]-d[convex[total-2]]). Det (d[i]-d[convex[total-1]]) <=0)) total--; Convex[total++]=i; } tmp=total;for (int I=n-2;i>=0;--i) {while (total>tmp) && ((d[convex[total-1]]-d[convex[total-2]]). Det (d[i]-d[convex[total-1]]) <=0)) total--; Convex[total++]=i; } Ctotal=total;}IntMain(){scanf"%d%d", &n,&l);for (int i=0;i<n;++i) { scanf ("%lf%lf", &d[i].x,&d[i].y);} get_convex_hull (); double ans=0; For (int i=0;i<ctotal-1;++i) {ans+=d[convex[i]].dist (d[convex[i+1]]);} ans+=d[convex[0]].dist (d[convex[ctotal-1]]); ans+=3.1415926*2*l; printf ("%.0lf\n", Ans); return 0;}                  

Graham Scan convex packet algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.