计算凸包直径: #include <cmath> #include <algorithm> #include <iostream> using namespace std; #define MAXN 50005 struct Point { int x, y; bool operator < (const Point& _P) const { return y<_P.y||(y==_P.y&&x<_P.x); }; }pset[MAXN],ch[MAXN]; //计算凸包 void convex_hull(Point *p,Point *ch,int n,int &len) { sort(p, p+n); ch[0]=p[0]; ch[1]=p[1]; int top=1; for(int i=2;i<n;i++) { while(top>0&&cross(ch[top],p[i],ch[top-1])<=0) top--; ch[++top]=p[i]; } int tmp=top; for(int i=n-2;i>=0;i--) { while(top>tmp&&cross(ch[top],p[i],ch[top-1])<=0) top--; ch[++top]=p[i]; } len=top; } //计算叉积 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 dist2(Point a,Point b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } //计算凸包直径 int rotating_calipers(Point *ch,int n) { int q=1,ans=0; ch[n]=ch[0]; for(int p=0;p<n;p++) { while(cross(ch[p+1],ch[q+1],ch[p])>cross(ch[p+1],ch[q],ch[p])) q=(q+1)%n; ans=max(ans,max(dist2(ch[p],ch[q]),dist2(ch[p+1],ch[q+1]))); } return ans; } int main() { //freopen("in.txt","r",stdin); int n, len; while(scanf("%d", &n)!=EOF) { for(int i = 0;i < n;i++) { scanf("%d %d",&pset[i].x,&pset[i].y); } convex_hull(pset,ch,n,len); printf("%d/n",rotating_calipers(ch,len)); } return 0; } 求凸包坐标: //求凸包坐标(百度百科) #include<iostream> #include<algorithm> using namespace std; struct point { int x,y; }p[30005],res[30005];//p标记图上所有的点,res标记凸包上的点 int cmp(point p1,point p2)//按照y坐标从小到大,如果相等按照x坐标从小到大排列 { return (p1.y<p2.y) || (p1.y==p2.y && p1.x<p2.x); } bool ral(point p1,point p2,point p3)//按照叉乘判断点的位置,如果p3在p1p2的逆时针,则返回 true,否则返回false { if((p2.x-p1.x)*(p3.y-p1.y)>(p3.x-p1.x)*(p2.y-p1.y)) return true; return false; } int main() { int n,i; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); if(n==1) { printf("(%d,%d)/n",p[0].x,p[0].y); continue; } if(n==2) { printf("(%d,%d)/n",p[0].x,p[0].y); printf("(%d,%d)/n",p[1].x,p[1].y); continue; } sort(p,p+n,cmp); res[0]=p[0]; res[1]=p[1]; int top=1; for(i=2;i<n;i++) { while(top && ral(res[top],res[top-1],p[i])) top--; res[++top]=p[i]; } int len=top; res[++top]=p[n-2]; for(i=n-3;i>=0;i--) { while(top!=len && ral(res[top],res[top-1],p[i])) top--; res[++top]=p[i]; } for(i=0;i<top;i++) printf("(%d,%d)/n",res[i].x,res[i].y); } return 0; } //Graham scan.cpp #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXSIZE 256 typedef struct { double x,y; }Point; Point a[MAXSIZE]; double Cross(Point p0,Point p1, Point p2) { return ((p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y)); } double Distance(Point p1, Point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } int Compare(const void * pa, const void * pb) { Point p1 = *(Point *)pa; Point p2 = *(Point *)pb; double m = Cross(a[0],p1,p2); if(m < 0) return 1; else if(m ==0 && Distance(p1,a[0]) < Distance(p2,a[0])) return 1; else return -1; } void Swap(Point * p1, Point * p2) { Point t; t.x = p1->x; t.y = p1->y; p1->x = p2->x; p1->y = p2->y; p2->x = t.x; p2->y = t.y; } int main() { int n,i,stack[MAXSIZE],top = 0; scanf("%d",&n); for(i = 0; i < n; i++) scanf("%lf %lf",&(a[i].x),&(a[i].y)); if(n == 1) { printf("V1: %lf %lf/n",a[0].x,a[0].y); return 0; } for(i = 0; i < n; i++) { if(a[i].y < a[0].y) Swap(&a[i],&a[0]); else if(a[i].y == a[0].y && a[i].x < a[0].x) Swap(&a[i],&a[0]); } if(n == 2) { printf("V1 %lf %lf/n",a[0].x,a[0].y); printf("V2 %lf %lf/n",a[1].x,a[1].y); return 0; } qsort(a+1,n-1,sizeof(Point),Compare); stack[top++] = 0; stack[top] = 1; for(i = 2; i < n;i++) { while(top>=1 && Cross(a[stack[top-1]],a[stack[top]],a[i])<=0) top--; stack[++top] = i; } for(i = 0; i <= top; i++) { printf("v%d: %lf %lf/n",i+1,a[stack[i]].x,a[stack[i]].y); } return 0; }