计算几何(五)旋转卡壳

存个板子,内容以后补

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e4+50;
const double eps=1e-10;
int n;
struct V 
{
	double x,y;	
}p[maxn],zhan[maxn];

inline void input(const V &a){scanf("%lf%lf",&a.x,&a.y);};

inline V operator + (const V &a,const V &b){return (V){a.x+b.x,a.y+b.y};}
inline V operator - (const V &a,const V &b){return (V){a.x-b.x,a.y-b.y};}

inline V operator * (const V &a,const double x){return (V){a.x*x,a.y*x};}
inline V operator * (const double x,const V &a){return (V){a.x*x,a.y*x};}
inline V operator / (const V &a,const double x){return (V){a.x/x,a.y/x};}

inline double operator * (const V &a,const V &b){return a.x*b.x+a.y*b.y;}
inline double operator ^ (const V &a,const V &b){return a.x*b.y-b.x*a.y;}

inline bool operator == (const V &a,const V &b){return abs(a.x-b.x)<eps && abs(a.y-b.y)<eps;}
inline bool operator != (const V &a,const V &b){return !(a==b);}

inline bool operator < (const V &a,const V &b){return a.x<b.x-eps || (abs(a.x-b.x)<eps && a.y<b.y-eps);}

inline double len(const V &a){return sqrt(a.x*a.x+a.y*a.y);}

bool cmp(V a,V b)
{
	double d=(a-p[1])^(b-p[1]);
	if(abs(d) > eps) return d>0;
	else return len(a-p[1])<len(b-p[1]);	
}

void ConvexHull(V *p,int &n)
{
	int top=0;
	sort(p+1,p+n+1);
	sort(p+2,p+n+1,cmp);
	for(int i=1;i<=n;++i)
	{
		while((top>1) && ((zhan[top]-zhan[top-1])^(p[i]-zhan[top]))<=0) --top;
		zhan[++top]=p[i];
	}
	n=top;
	memcpy(p,zhan,sizeof(zhan));
	return;
}

inline int calc(V a,V b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+eps;	
}

int rotate_calipers(V *p,int n)
{
	int res=0;
	int pl=1;
	for(int i=2;i<=n;++i)
	{
		if(((p[2]-p[1])^(p[pl]-p[2])) < ((p[2]-p[1])^(p[i]-p[2]))-eps) pl=i; 
	}
	for(int i=1;i<=n;++i)
	{
		while(((p[i+1]-p[i])^(p[pl]-p[i+1])) < ((p[i+1]-p[i])^(p[pl+1]-p[i+1]))-eps)
		{
			pl=(pl+1)%n;
			res=max(res,max(calc(p[i],p[pl]),calc(p[i+1],p[pl])));
		}
		res=max(res,max(calc(p[i],p[pl]),calc(p[i+1],p[pl])));
	}
	return res;
}

int main()
{
//	freopen("P1452.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;++i) input(p[i]);
	ConvexHull(p,n);
	p[0]=p[n];
	p[n+1]=p[1];
	printf("%d",rotate_calipers(p,n)); 
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值