Convex hull convex bag

Source: Internet
Author: User

The smallest convex polygon that is included in the points given on a plane. Outputs each vertex of the convex hull counter-clockwise.

1.Graham Scanning Method (O (N*LOGN))-------Rotary Sweep technology:

2.Jarvis March Stepping Method (O (N*H)) H is the number of vertices of a convex hull--------packaging technology

Application: To find the furthest point of two-dimensional plane.

uva,109

1#include <iostream>2#include <vector>3#include <algorithm>4 5 using namespacestd;6 7 structnode8 {9     Doublex, y;Ten     //easy to find the initial point One     BOOL operator< (Constnode& a)Const{ A         returnx<a.x | | (x==a.x && y<a.y); -     } - }; the  -  -typedef vector<node> Vnode;//Convex bag -typedef vector<vnode> Vvnode;//number of convex bags +  - //cross product determines whether to turn left.  + DoubleIsleft (node& p0,node& p1,node&p2) A { at     return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y); - } -  - //Convex bag - intConvex_hull (Vnode P,vnode &h) { -     intn=p.size (); in     intk=0; -     //by x Ascending and then y in ascending order to sort (P.begin (), P.end ()); +      -      for(intI=0; i<n;i++){ the //wind Up *          while(k>=2&& Isleft (h[k-2],h[k-1],p[i]) <=0) $k--;Panax Notoginsengh[k++]=P[i]; -     } the     //Down Chain +      for(inti=n-2, t=k+1; i>=0; i--){ A          while(K>=t && Isleft (h[k-2],h[k-1],p[i]) <=0) thek--; +h[k++]=P[i]; -     } $     returnK; $ } - //Check if it is inside a convex polygon - intCheck (Vnode h, node p) { the     intn =h.size (); -     intI, j, C =0;Wuyi      for(i =0, j = N1; I < n; j = i++) the         if((((h[i].y <= p.y) && (P.y < h[j].y)) | | ((h[j].y <= p.y) && (P.y < H[I].Y))) && (P.x < (h[j].x-h[i].x) * (P.Y-H[I].Y)/(H[J].Y-H[I].Y) +h[i].x)) -c =!C; Wu     returnC; - } About //convex hull Area according to test instructions $ DoubleArea (Vnode p) { -     intn=p.size (); -     Doubles=0; -      A      for(intI=1; i<n;i++){ +s+= ((p[i-1].x*p[i].y-p[i].x*p[i-1].Y))/2; the     } -     returns; $ } the  the  the intMain () the { -     intN; in Vvnode Hull; the      the      while(Cin>>n && n!=-1) About     { the Vnode p (n); the          for(intI=0; i<n;i++) theCin>>p[i].x>>p[i].y; +          -Vnode H (n+1); then=Convex_hull (p,h);Bayi h.resize (n); the hull.push_back (h); the     } -      - node K; the     DoubleA=0; the      while(cin>>k.x>>k.y) { the Vvnode::iterator it; the          for(It=hull.begin (); It) -         { the             if(Check (*it,k)) the             { theA+=area (*it);94 Hull.erase (it); the                  Break; the             } the         }98     } About //output with two decimal places -Cout.precision (2);101 //fixed format, Floatfield for floating-point output102COUT.SETF (iOS::fixed, Ios::floatfield);103cout<<a<<Endl;104      the     return 0;106}
View Code

Convex hull convex bag

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.