The choice of common-line convex hull polar coordinate sorting and horizontal sorting

Source: Internet
Author: User
Tags sort

The principle of convex hull, here is not much introduced.

A few days ago, I encountered a common-line convex hull problem. Because of their own drawings, directly with the polar coordinates of the template up to do, WA a day.

Then you look for information, read other people's code, read books.

Finally, the polar order of the convex hull does not solve the collinear problem.


the polar coordinates of the general convex hull are sorted by finding the leftmost (y first) point and placing it in point[0], sorted according to the values of point[0]->point[a] and point[0]->point[b], and the last row point[0] As the origin point, the other points in order counterclockwise division. If three points collinear, then, press to point[0] the distance from small to large sort.

Sort as shown in figure A for point[0], B C Q .... P D E are all points sorted according to the polar coordinates.


Next, the disadvantage of the ranking of the polar coordinates (see LRJ Black Book P395).


See in the Polar panel The while (Top>0&&cross (List[stack[top-1]],list[stack[top]],list[i]) <=0) in line 73rd top-- ;

In the case of not collinear points

The meaning of this line is that if there are two or more points in the stack.   That takes stack[top-1] as the center point. Stack[top] For point A, list is the point at which the stack is going to be B point.

For example, the ABC point, before removing the = number in the while (), this template will delete the stack[top] point, that is, the B point, so that you can choose the least point, and finally left is the AC point

At the end of the corresponding PED point, D Point because of a near, so first into the stack, when the e point again into the stack, D point will be out of the stack. So the last one left is pea.

Can be seen in polar order, if there is no need to have collinear points exist, it is feasible.



If you want to put the collinear point into the stack stack, you have to remove the "=" in the while ()

If after sorting, select a far from the first into the stack, this obviously does not, in such a stack order, in judging CBQ when the B point will be deleted.

If after sorting, it is obviously not possible to select the first step from the A-in, and the D point will be deleted when judging the PDE.

Therefore, it is not possible to sort the polar coordinates in the case where the collinear points need to be stored.


So in order to calculate the common points of the topic, to use the horizontal sort.







This is the polar coordinate template;

#include <stdio.h> #include <math.h> #include <algorithm> #include <iostream> using namespace
Std
const int maxn=1005;//points//complexity Nlogn struct Point {int x, y;};
Point LIST[MAXN];

int stack[maxn],top;     
int Cross (point p0,point p1,point p2)//Calculate fork Product P0p1 X p0p2 {return (p1.x-p0.x) * (P2.Y-P0.Y)-(P1.Y-P0.Y) * (p2.x-p0.x);} Double dis (point p1,point p2)//calculates the distance of P1P2 {return sqrt ((double) (p2.x-p1.x) * (p2.x-p1.x) + (P2.Y-P1.Y) * (P2.Y-P1).
y));  } BOOL CMP (point p1,point P2)//Polar sort function, the same angle is the small distance in front {int tmp=cross (LIST[0],P1,P2);
    Counterclockwise if (tmp>0) return true;
    else if (Tmp==0&&dis (LIST[0],P1) <dis (LIST[0],P2)) return true;
else return false; } void init (int n)////input, and place the bottom-left point in list[0]. and make the extreme angle sort list, subscript [0,n];
    N==0 when there is a pit {if (n==0) return;
    int i,k;
    Point P0;
    scanf ("%d%d", &list[0].x,&list[0].y);
    p0.x=list[0].x;
    P0.Y=LIST[0].Y;     
    k=0;
        for (i=1;i<n;i++) {scanf ("%d%d", &list[i].x,&list[i].y); if ((p0.y>list[i].y) | | |
        ((P0.Y==LIST[I].Y) && (p0.x>list[i].x)))
            {p0.x=list[i].x;
            P0.Y=LIST[I].Y;
        K=i;
    }} List[k]=list[0];
    
    List[0]=p0; Sort (list+1,list+n,cmp);//Extreme angle sort//counterclockwise} void Graham (int n)//stack is the subscript of the convex hull perimeter. 
[0,top]
    {int i;
    if (n==1) {top=0;stack[0]=0;}
        if (n==2) {top=1;
        stack[0]=0;
    Stack[1]=1;
        } if (n>2) {for (i=0;i<=1;i++) stack[i]=i;
        
        Top=1; for (i=2;i<n;i++) {while (Top>0&&cross (list[stack[top-1]],list[stack[top]],list[i)) <=0             ) top--;//general direction counterclockwise top++;  ==0 will remove short, longer lines. <0, the top-1 point is the center, connecting the top point and the current I point. If the steering is clockwise, remove the top point.
            This guarantee is the most out of the way.
        Stack[top]=i;
	}}} double Sumlen ()//The circumference calculation function of the snake added by his own painting {double sum=0; for (int i=1;i<=top;i++) {sum+=dis (List[stack[i-1]],list[stack[i]);
	} Sum+=dis (List[stack[0]],list[stack[top]]);
return sum; }



This is a horizontal sort of template, the code goes from http://blog.csdn.net/a601025382s/article/details/11659113

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <iostream
> #include <algorithm> using namespace std;
const int maxn=1010;
Const Double Pi=atan (1.0); struct node{int x, y;}
E[MAXN],RES[MAXN];
int m;
    int CMP (node A,node b) {if (a.x==b.x) return a.y<b.y;
Return a.x<b.x; } int Cross (node A,node b,node c)//vector product {return (a.x-c.x) * (B.Y-C.Y)-(b.x-c.x) * (A.Y-C.Y);} int convex (int n)//Find the point on the convex packet
    into res a total of M subscript [0,m) {sort (e,e+n,cmp);
    int i,j,k;
    M=0//For the lower convex hull, counter-clockwise//known bump point M, if the new join point is I, then the vector (m-2,i) must be in (m-2,m-1) in the counterclockwise direction to meet the convex hull properties//If not, then the m-1 point is not on the convex package.
        for (i=0;i<n;i++) {while (M>1&&cross (Res[m-1],e[i],res[m-2]) <0) m--;//There is equal to no collinear, no focus
    Res[m++]=e[i];
    } k=m;
        The upper convex hull for (i=n-2;i>=0;i--) {while (M>k&&cross (Res[m-1],e[i],res[m-2]) <0) m--is obtained;
    Res[m++]=e[i];
    } if (n>1) m--;//start point duplicates.
return m;
  } int main () {  int t,n,l;
    scanf ("%d", &t);
        while (t--) {int i,j,k;
        scanf ("%d", &n);
        for (i=0;i<n;i++) scanf ("%d%d", &e[i].x,&e[i].y);
        
    Convex (n);
} return 0;  }


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.