P4557 [JSOI2018]战争 闵科夫斯基和

题目
两个凸包A,B,沿某一方向向量移动B,问是否与A有交

两个图形A,B的闵可夫斯基和C={p0+p1|p0∈A,p1∈B}
两个凸包的闵科夫斯基和仍然是凸包

设p0∈A,p1∈B,方向向量V:满足b+V=a
则V=a-b=a+(-b)
则V是一个凸包,且是A,B的闵科夫斯基和
我们先求A,B的闵科夫斯基和C
判断每个向量是否在C中

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int maxp=2e5+7;
const double inf=1e100;
const double eps=1e-12;
const double pi =acos(-1.0);
int sgn(double x){
	if(fabs(x)<eps)return 0;
	return x>0?1:-1;
}
//double hypot(double x,double y){return sqrt(x*x+y*y);}
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
	bool operator ==(const Point& b){return !sgn(x-b.x)&&!sgn(y-b.y);}
	bool operator  <(const Point& b)const{return !sgn(x-b.x)?y<b.y:x<b.x;}
	double operator^(const Point& b){return x*b.y-y*b.x;}//叉积
	double operator*(const Point& b){return x*b.x+y*b.y;}//点积
	Point operator +(const Point &b){return Point(x+b.x,y+b.y);}
	Point operator -(const Point& b){return Point(x-b.x,y-b.y);}
	Point operator *(const double &k){return Point(x*k,y*k);}
	Point operator /(const double &k){return Point(x/k,y/k);}
	double len(){return hypot(x,y);}
	double len2(){return x*x+y*y;}
	double rad(Point a,Point b){Point p=*this;return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));}//pa和pb夹角
	double angle(){return atan2(y,x);}//倾斜角
	double angle(Point B){//夹角
		Point A=(*this);
		return acos(A*B/A.len()/B.len());
	}
	double distance(Point b){return hypot(x-b.x,y-b.y);}
	Point trunc(double r){//化为长度为r的向量
		double l=len();
		if(!sgn(l))return *this;
		r/=l;return Point(x*r,y*r);
	}
	Point rotleft(){return Point(-y,x);}//逆时针旋转90 度
	Point rotright(){return Point(y,-x);}//顺时针旋转90 度
	Point rotate(double rad){//rad为弧度 逆时针旋转
		Point A=(*this);
		double c=cos(rad),s=sin(rad);
		return Point(A.x*c-A.y*s,A.x*s+A.y*c);
	}
	Point rotate(Point p,double angle){//绕p逆时针旋转后点
		Point v=((*this)-p).rotate(angle);
		return Point(p.x+v.x,p.y+v.y);
	}
	void input(){scanf("%lf%lf",&x,&y);}
	void show(){printf("(%.2lf %.2lf)\n",x,y);}
	void zero(){
		if(!sgn(x))x=0.0;
		if(!sgn(y))y=0.0;
	}
};
double cross(Point A,Point B,Point C){return (B-A)^(C-A);}//AB X AC
double dot(Point A,Point B,Point C){return (B-A)*(C-A);}//AB*AC
struct polygon{
	int n;
	Point *p;
	void input(int _n){n=_n;for(int i=0;i<n;++i)p[i].input();}
	void add(Point q){p[n++]=q;}
	struct cmp{
		Point p;
		cmp(const Point &p0){p=p0;}
		bool operator()(const Point &aa,const Point &bb){
			Point a=aa,b=bb;
			int d=sgn((a-p)^(b-p));
			if(!d)return sgn(a.distance(p)-b.distance(p))<0;
			return d>0;
		}
	};//进行极角排序//首先需要找到最左下角的点//需要重载号好Point 的< 操作符(min 函数要用)
	void norm(){
		Point mi=p[0];
		for(int i=1;i<n;++i)mi=min(mi,p[i]);
		sort(p,p+n,cmp(mi));
	}//得到凸包,点编号是0-n-1
	void Graham(polygon &convex){
		norm();
		int &top=convex.n;top=0;
		if(n==1){top=1,convex.p[0]=p[0];return;}
		if(n==2){
			top=2,convex.p[0]=p[0],convex.p[1]=p[1];
			if(convex.p[0]==convex.p[1])--top;
			return;
		}convex.p[0]=p[0],convex.p[1]=p[1],top=2;
		for(int i=2;i<n;++i){
			while(top>1&&sgn((convex.p[top-1]-convex.p[top-2])^(p[i]-convex.p[top-2]))<=0)--top;
			convex.p[top++]=p[i];
		}if(convex.n==2&&(convex.p[0]==convex.p[1]))--convex.n;//特判
	}
	void Minkowski(polygon C1,polygon C2,Point* s1,Point* s2){//闵科夫斯基和
		for(int i=0;i<C1.n;++i)s1[i]=C1.p[(i+1)%C1.n]-C1.p[i];
		for(int i=0;i<C2.n;++i)s2[i]=C2.p[(i+1)%C2.n]-C2.p[i];
		p[n=0]=C1.p[0]+C2.p[0],++n;
		int p1=0,p2=0;
		while(p1<C1.n&&p2<C2.n)p[n]=p[n-1]+(sgn(s1[p1]^s2[p2])>=0?s1[p1++]:s2[p2++]),++n;
		while(p1<C1.n)p[n]=p[n-1]+s1[p1++],++n;
		while(p2<C2.n)p[n]=p[n-1]+s2[p2++],++n;
	}
	int in(Point x){//点在凸包内
		if(sgn((x-p[0])^(p[1]-p[0]))>0||sgn((x-p[0])^(p[n-1]-p[0]))<0)return 0;
		int l=1,r=n-1,pos=l;
		while(l<=r){
			int mid=(l+r)>>1;
			if(sgn((p[mid]-p[0])^(x-p[0]))>=0)pos=mid,l=mid+1;
			else r=mid-1;
		}
		return sgn((p[(pos+1)%n]-p[pos])^(x-p[pos]))>=0;
	}
};
Point ap[maxp],bp[maxp],cp[maxp],Ap[maxp],Bp[maxp],Cp[maxp];
polygon a,b,c,A,B,C;
int main(){
	a.p=ap,b.p=bp,c.p=cp,A.p=Ap,B.p=Bp,C.p=Cp;
	int n,m,q;
	scanf("%d%d%d",&n,&m,&q);
	a.input(n),b.input(m);
	for(int i=0;i<m;++i)b.p[i]=Point(-b.p[i].x,-b.p[i].y);
	a.Graham(A),b.Graham(B);
	c.Minkowski(A,B,ap,bp);
	c.Graham(C);
	while(q--){
		Point f;f.input();
		printf("%d\n",C.in(f));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值