a few nouns about convex hull:1. Support line:If a direct L is through a vertex of the convex polygon p, and the polygon is on one side of the line, it is said that L is the support line of P 2. To heel point:if the two points on the convex hull can draw a pair of parallel lines, so that the points on the convex hull are clamped between two parallel lines or The two points that fall on parallel lines are called pairs of heel points. Two parallel support lines over two points are a pair of heel points. Can beproving that the heel point of a convex N-shaped side has a maximum of 3N/23. Diameter of convex polygon:defines the maximum distance between any two points on a polygon as the diameter of a polygon. Determining the point logarithm of this diameter maymore than one pair.
to rotate the jam to find the diameter:Step1. Calculates the endpoint in the Y-direction of the polygon, Ymin,ymax. Step2. Through Ymin,ymax, dogs find two horizontal tangents, since they are already a pair of heel points, calculating the distance between them away fromand maintains a current maximum value. step3. Rotate between two bars simultaneously to one of the sides of the polygon coincident. Step4. A new pair of heel points, calculates a new distance, and compares it to the current maximum value, if it is greater than the current maximum updated. step5. Repetitive operations 3,4 know to produce pairs of heel points (Ymin,ymax).
Analysis: The complexity of rotating jam is O (n), the complexity of convex hull is O (nlogn).
Topic Link: PortalTo find the square of the diameter of a convex packageThe code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int maxn = 5e4+10;typedef struct point{int x, y; BOOL operator < (const struct point &b) const{if (x==b.x) return y<b.y; Return x<b.x; } bool operator = = (const struct point &b) const{return (x-b.x) ==0&& (Y-B.Y) ==0; }}vector; Vector P[maxn],st[maxn];int Cross (point A,point b,point o) {return (a.x-o.x) * (B.Y-O.Y)-(b.x-o.x) * (A . y-o.y);} int Convexhull (point *p,int n,point *st) {sort (p,p+n); n = Unique (p,p+n)-p; int m=0; for (int i=0;i<n;i++) {while (M>1&&cross (St[m-1],p[i],st[m-2]) <=0) m--; St[m++]=p[i]; } int k=m; for (int i=n-2;i>=0;i--) {1 while (M>k&&cross (St[m-1],p[i],st[m-2]) <=0) m--; St[m++]=p[i]; } if (n>1) m--; return m;} int dis (point a,point b) {return (a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b. y);} The width of the convex hull of a rotating jam is double rotating_calipers (point *p,int N) {int k = 1; Double ans = 0x7FFFFFFF; P[n] = p[0]; for (int i=0;i<n;i++) {while (Fabs (p[i],p[i+1],p[k)) < Fabs (Cross (p[i],p[i+1],p[k+1))) K = (k+1)% n; Double tmp = fabs (Cross (p[i],p[i+1],p[k)); Double d = dist (p[i],p[i+1]); ans = min (ans,tmp/d); } return ans; int main () {int n; while (~SCANF ("%d", &n)) {for (int i=0;i<n;i++) {scanf ("%d%d", &p[i].x,&p[i].y); } int m = Convexhull (p,n,st); int ans = rotatint_calipers (st,m); printf ("%d\n", ans); } return 0;}
Topic Link: PortalTest Instructions:n points on a plane, the area of the largest triangle that can be composed from these n points. Analysis:the convex hull is first asked. The vertex of the largest triangle must be on the convex hull, enumerating the vertex i,j,k,j=i+1,k=j+1.it's obvious .in the enumeration of K we can fix the two points of the i,j, and then find the current can be composed of the largest area of the point K., and Kthe changes are related to J, so do not enumerate from scratch. we loop through the k+1, know Area (i,j,k+1) <area (i,j,k), this time update the maximum size, and then you canRotateJ,k, if area (i,j,k) <area (i,j,k+1) k!=i, then k++, otherwise jump out, update areas, j + +, rotate a week canto get the largest triangular area, time complexity O (n^2)The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < cmath>using namespace Std;const int maxn = 5e4+10;const double eps = 1e-10;int dcmp (double x) {if (Fabs (x) <=0) ret Urn 0; if (x>0) return 1; else return-1;} typedef struct point{double x, y; BOOL operator < (const struct point &b) const{if (x==b.x) return y<b.y; Return x<b.x; } bool operator = = (const struct point &b) const{return dcmp (x-b.x) ==0&&dcmp (Y-B.Y) ==0; }}vector; Vector P[MAXN],ST[MAXN];d ouble Cross (point A,point b,point o) {return (a.x-o.x) * (B.Y-O.Y)-(b.x-o.x) * (A.Y-O.Y);} int Convexhull (point *p,int n,point *st) {sort (p,p+n); n = Unique (p,p+n)-p; int m=0; for (int i=0;i<n;i++) {while (M>1&&cross (St[m-1],p[i],st[m-2]) <=0) m--; St[m++]=p[i]; } int k=m; for (int i=n-2;i>=0;i--) {while (M>k&&cross (st[m-1],p[I],st[m-2] <=0) m--; St[m++]=p[i]; } if (n>1) m--; return m;} Double Dis (point A,point b) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));} Double Rotating_calipers (point *p,int N) {double ans = 0; int j,k; for (int i=0;i<n;i++) {j= (i+1)%n; k= (j+1)%n; while (Fabs (p[i+1],p[k],p[i)) <fabs (Cross (p[i+1],p[k+1],p[i))) k= (k+1)%n; while (j!=i&&k!=i) {Ans=max (Ans,fabs (P[I],P[K],P[J))); while (Fabs (p[i],p[k],p[j)) <fabs (Cross (p[i],p[(k+1)%n],p[j])) k= (k+1)%n; j= (j+1)%n; }} return ans/2.0;} int main () {int n; while (~SCANF ("%d", &n)} {if (n==-1) break; for (int i=0;i<n;i++) scanf ("%lf%lf", &p[i].x,&p[i].y); int m = Convexhull (p,n,st); cout<< "M" <<m<<endl; if (m<3) puts ("0.00"); if (m==3) printf ("%.2lf\n", Fabs (Cross (st[0],st[1],st[2)))/2.0); else{double ans = rotating_calipers (st,m); printf ("%.2lf\n", ans); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Convex hull and rotating jam