Convex packet algorithm

Source: Internet
Author: User

I. Concept:Convex hull (convex Hull) is a concept in computational geometry (graphics). In a real vector space V, for a given set X, all the intersection s of the convex sets that contain x are called convex packages of x. X's convex hull can be used for all points within X (X1, ... Xn). In two-dimensional Euclidean space, a convex hull can be imagined as a rubber band that is just wrapped around all points. In the case of non-rigorous words, given a set of points on a two-dimensional plane, a convex hull is a convex multilateral type that joins the outermost point together, and it can contain all points in a point set. Example: Suppose a plane has p0~p12 a total of 13 points, over certain points as a polygon, so that the polygon can be all points are "wrapped" up. When this polygon is convex, we call it "convex hull". Such as:
two. Solution:

Graham Scanning method

Complexity of Time: O (n㏒n)
Idea: The idea of Graham scanning is to find a point on the convex hull, and then start by looking at the point on the convex hull in a counterclockwise direction from that point, in fact, to sort the poles and use them for queries.

Steps:

    1. Placing all the points in a two-dimensional coordinate system, the smallest point of the ordinate must be the point on the convex hull, and the P0 in it.
    2. Translate the coordinates of all points to make the P0 as the origin, such as.
    3. Calculates the amplitude angle α of each point relative to the P0, sorting the points in order from small to large. When the α phase is at the same time, the distance P0 is closer to the front. For example, the results obtained are P1,P2,P3,P4,P5,P6,P7,P8. We can know by geometrical knowledge that the first point in the result P1 and the last point P8 must be the point on the convex hull.
      (The above is the preparation step, the following begins to find convex hull)
      Above, we already know the first point on the convex hull P0 and the second point P1, we put them in the stack. Now from the result of step 3, take the point behind P1 to do the current point , that is, P2. Then we start looking for a third point:
    4. Connect the P0 and the top of the stack to get straight L. See if the current point is on the right or left side of the line L. If you are on the right side of the line, perform step 5, or Step 6 if you are on a straight line or on the left side of the line.
    5. If on the right, the element at the top of the stack is not the point on the convex hull, and the stack top element is stacked. Perform step 4.
    6. The current point is the point on the convex hull and pushes it into the stack, performing step 7.
    7. Checking the current point P2 is not the last element of the result of step 3. is the last element to end. If not, take the point behind the P2 to the current point and return to step 4.

Finally, the elements in the stack are the points on the convex hull.
The following is a dynamic solution process using the Graham Sweep method:

The following static solution process

Three. Templates

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define PI 3.1415926535using namespace std;struct node{int x, y;}; Node vex[1000];//deposits All points in the node stackk[1000];//convex package in all points int Xx,yy;bool CMP1 (node A,node b)//Sort Find First point {if (A.Y==B.Y) RET    Urn a.x<b.x; else return a.y<b.y;} int Cross (node A,node b,node c)//Calculate fork Product {return (b.x-a.x) * (C.Y-A.Y)-(c.x-a.x) * (B.Y-A.Y);} Double Dis (node A,node b)//calculate distance {return sqrt ((a.x-b.x) * (a.x-b.x) *1.0+ (A.Y-B.Y) * (A.Y-B.Y));} BOOL CMP2 (node A,node b)//Polar angle Sort Another method, fast {if (atan2 (a.y-yy,a.x-xx)!=atan2 (B.Y-YY,B.X-XX)) return (atan2 (a.y-yy,a.x    -XX) < (atan2 (B.Y-YY,B.X-XX)); return a.x<b.x;}    BOOL CMP (node A,node b)//polar Sort {int M=cross (VEX[0],A,B);    if (m>0) return 1;    else if (M==0&&dis (vex[0],a)-dis (vex[0],b) <=0) return 1;    else return 0;    /*if (m==0) return dis (vex[0],a)-dis (vex[0],b) <=0?true:false; else return m&Gt;0?true:false;*/}int Main () {int t,l;        while (~SCANF ("%d", &t), t) {int i;        for (i=0; i<t; i++) {scanf ("%d%d", &vex[i].x,&vex[i].y);        } if (t==1) printf ("%.2f\n", 0.00);        else if (t==2) printf ("%.2f\n", Dis (vex[0],vex[1]));            else {memset (stackk,0,sizeof (Stackk));            Sort (VEX,VEX+T,CMP1);            STACKK[0]=VEX[0];            xx=stackk[0].x;            YY=STACKK[0].Y; Sort (VEX+1,VEX+T,CMP2);//CMP2 is faster, CMP is easier to understand stackk[1]=vex[1];//the first two points in the convex hull are stored in the structure of the convex hull, the int top=1;//the last convex package has The number of points for the (i=2; i<t; i++) {while (I>=1&&cross (stackk[top-1],stackk[top],v                Ex[i] <0)//To use the i>=1 of the pole angle can sometimes be used, but add always good top--;                                    Stackk[++top]=vex[i];            Control <0 or <=0 can control the focus, common line, depending on the topic.            } double s=0; for (I=1; i<=top;i++)//output bump on the point//cout<<stackk[i].x<< "" <<stackk[i].y<<endl;            for (I=1; i<=top; i++)//Calculate the circumference of the convex hull S+=dis (stackk[i-1],stackk[i]);            S+=dis (stackk[top],vex[0]);//The distance between the last point and the first point/*s+=2*pi*l;        int ans=s+0.5;//rounding printf ("%d\n", ans); */printf ("%.2lf\n", s); }    }}

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.