Algorithm template--Computational Geometry 2 (two-dimensional convex hull--andrew algorithm)

Source: Internet
Author: User

Realization function: Find out two-dimensional convex hull of a pair of scattered points in the plane (see Codevs 1298)

It's an amazing algorithm--sort the points by the coordinates, then turn left as we know it, find the convex hull of the half, and then turn to the other side of the convex bag.

I used to be because of the idea of one step, so every time I kneel very miserable (hansbug: In fact, this is the first time in my life a-drop convex bag problem)

And then nothing else, is the basic idea of convex hull

(By the way, the output convex hull circumference C and area s, praise like a tide oh)

1 typeArr=Array[0..100005] ofLongint;2 var3 I,j,k,l,m,n,m1,m2:longint;4A:Array[0..100005,1..2] ofLongint;5 b,c,d:arr;ans,are:extended;6 procedureSwapvarx,y:longint);7           varZ:longint;8           begin9z:=x;x:=y;y:=Z;Ten           End; One proceduresort (l,r:longint); A           varI,j,x,y:longint; -           begin -i:=l;j:=r;x:=a[(L+r)Div 2,1];y:=a[(L+r)Div 2,2]; the                Repeat -                       while(A[i,1]&LT;X)or(A[i,1]=X) and(A[i,2]<y)) DoInc (i); -                       while(A[j,1]&GT;X)or(A[j,1]=X) and(A[j,2]>y)) DoDec (j); -                      ifI<=j Then +                         begin -Swap (A[i,1],a[j,1]); +Swap (A[i,2],a[j,2]); A Inc (I);d EC (j); at                         End; -                untilI>J; -                ifI<r Thensort (i,r); -                ifL<j Thensort (l,j); -           End; - functionRight (X1,y1,x2,y2:longint): boolean; in          begin -Exit ((X1*y2) > (x2*y1)); to          End; + functionTrip (X1,y1,x2,y2,x3,y3:longint): boolean; -          begin theExit (Right (x2-x1,y2-y1,x3-x2,y3-y2)); *          End; $ functionCheck (X,y,z:longint): boolean;Panax Notoginseng          begin -Exit (Trip (A[x,1],a[x,2],a[y,1],a[y,2],a[z,1],a[z,2])); the          End; + procedureDoitvarB:arr;varm:longint); A           begin theb[1]:=d[1];b[2]:=d[2];j:=2; +                 fori:=3  toN Do -                    begin $                          while(j>1) and  not(Check (b[j-1],b[j],d[i])) DoDec (j); $Inc (J); b[j]:=D[i]; -                    End; -m:=J; the           End; - beginWuyi READLN (n); the       fori:=1  toN DoREADLN (A[i,1],a[i,2]); -Sort1, n); j:=1; Wu       fori:=2  toN Do//Go heavy -          begin About               if(A[i,1]<>a[j,1])or(A[i,2]<>a[j,2]) Then $                  begin - Inc (J); -A[j,1]:=a[i,1];a[j,2]:=a[i,2]; -                  End; A          End; +n:=J; the//Convex bag -       fori:=1  toN Dod[i]:=I;doit (B,M1); $       fori:=1  toN Dod[i]:=n+1-I;doit (c,m2); the//two halves of integration the       fori:=1  toM1 Dod[i]:=B[i]; the       fori:=2  toM2 DoD[i+m1-1]:=C[i]; theStart calculating perimeter +Area -m:=m1+m2-2; ans:=0; are:=0; in       fori:=1  toM DoAns:=ans+sqrt (Sqr (a[d[i),1]-a[d[i+1],1]) +sqr (A[d[i],2]-a[d[i+1],2])); //Perimeter the       fori:=1  toM DoAre:=are+a[d[i],1]*a[d[i+1],2]-a[d[i],2]*a[d[i+1],1]; //Area theAre:=abs (IS)/2; AboutWriteln ('Convex Hull:'); the       fori:=1  toM DoWriteln (A[d[i],1],' ', A[d[i],2]); theWriteln ('C =', ans:0:1); theWriteln ('S =', is:0:1); + Readln; - End.

Algorithm template--Computational Geometry 2 (two-dimensional convex hull--andrew 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.