Computational geometry-convex hull summary

Source: Internet
Author: User

Calculating geometric convex hull

Convex hull: give you n scattered points, let you find the smallest convex polygon to include all the points, or point on the edge. The algorithm used is Graham.

Graham algorithm: First find a vertex as a base point, then connect the point to other points and then sort by the size of the angle.

Then add, the first point must be on the convex polygon, and then start adding points, each add a point, to use the X-product of the vector to determine whether there was a side in his, if there is, the point is replaced, the process is a backtracking process

First, to find a base point, the minimum ordinate must be on the convex hull, if the ordinate is equal then choose the horizontal axis small.

The following is a simulation of the Graham algorithm scanning process (from HDU courseware)

The Prime Minister found a base point, and it was clear that P0 would be on the convex hull

Then the P0 and the other points are connected, according to the order of the angle, sorting, do not need to find out the specific angle, only need to use the vector of the difference between the size of the order, and then according to the sequence of the angle of the dot

When adding P3, it is found that the vector p1p3 is outside the vector p2p3, so p2 this point is definitely not on the convex hull, so delete it (this process is a backtracking process)

Then dot P4 no side outside of P3P4, continue

Add P5 time, p3p5 in the p4p5 outside, so P4 this point is not cut off the convex bag

Repeat this process every time you add a point, you have to go back to the side, and there is no edge on the side to add the outside

......

Finally found the convex hull

How do you implement it with code?

Template:

/**************************** Convex package template *******************************/Const DoubleEPS = 1e-8;intSgnDoublex) {    if(Fabs (x) < EPS)return 0; if(X <0)return-1; Else return 1;}structpoint{Doublex, y; Point () {}, point (Double_x,Double_y) {x= _x;y =_y; } Pointoperator-(ConstPoint &b)Const    {        returnPoint (X-b.x,y-b.y); }    //Cross Product    Double operator^(ConstPoint &b)Const    {        returnx*b.y-y*b.x; }    //dot Product    Double operator*(ConstPoint &b)Const    {        returnx*b.x + y*b.y; }    voidinput () {scanf ("%LF%LF",&x,&y); }};structLine {point s,e; Line () {} line (point _s,point _e) {s= _s; E =_e; }}; //* Distance between two pointsDoubleDist (Point A,point b) {returnsqrt (A-B) * (Ab));} /** Find convex hull, Graham algorithm * point number 0~n-1* return convex hull result Stack[0~top-1] is the number of the convex hull*/Const intMAXN =1010; Point LIST[MAXN];intSTACK[MAXN];//the point used to store the convex hullintTop//indicates the number of midpoints in the convex hull//Polar Order relative to list[0]BOOL_cmp (Point p1,point p2) {DoubleTMP = (p1-list[0]) ^ (p2-list[0]); if(SGN (TMP) >0)        return true; Else if(SGN (tmp) = =0&& sgn (Dist (p1,list[0])-Dist (p2,list[0])) <=0)        return true; Else         return false;}voidGraham (intN)    {Point p0; intK =0; P0= list[0]; //find the bottom one .     for(inti =1; I < n;i++)    {        if((P0.y > list[i].y) | | (P0.y = = List[i].y && p0.x >list[i].x)) {P0=List[i]; K=i; }} swap (list[k],list[0]); Sort (List+1, list+n,_cmp); if(n = =1) {Top=1; stack[0] =0; return; }    if(n = =2) {Top=2; stack[0] =0; stack[1] =1; return ; } stack[0] =0; stack[1] =1; Top=2;  for(inti =2; I < n;i++)    {         while(Top >1&& SGN (list[stack[top-1]]-list[stack[top-2]]) ^ (list[i]-list[stack[top-2]]) <=0) Top--; Stack[top++] =i; }}/**************************** Convex package template *******************************/

Computational geometry-convex hull summary

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.