POJ-3349 Geometric Shapes

题解:https://blog.csdn.net/miranda_ymz/article/details/81980053

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=26+10;
//Point定义
const double eps=1e-8;
const double PI=acos(-1.0);
int sgn(double x)
{
	if(fabs(x)<eps) return 0;
	if(x<0) return -1;
	else return 1;
}
struct Point
{
	double x,y;
	Point(){}
	Point(double _x,double _y)
	{
		x=_x;y=_y;
	}
	Point operator - (const Point &b) const
	{
		return Point(x-b.x,y-b.y);
	}
	//叉积
	double operator ^ (const Point &b) const
	{
		return x*b.y-y*b.x;
	}
	//点积
	double operator * (const Point &b) const
	{
		return x*b.x+y*b.y;
	}
	///绕原点旋转角度B(弧度值),后x,y的变化 
	void transXY(double B)
	{
		double tx=x,ty=y;
		x=tx*cos(B)-ty*sin(B);
		y=tx*sin(B)+ty*cos(B);
	}
};
//两点描述直线
struct Line
{  
	Point s,e;  
 	Line(){}  
 	Line(Point _s,Point _e)  {   s = _s;e = _e;  }  
 	//两直线相交求交点  
 	//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交 
 	 //只有第一个值为2时,交点才有意义     
 	pair<int,Point> operator &(const Line &b)const     
 	{        
 	    Point res = s;        
 	  	if(sgn((s-e)^(b.s-b.e)) == 0)      
 	    {            
 	    	if(sgn((s-b.e)^(b.s-b.e)) == 0)               
 	        	return make_pair(0,res);//重合           
 	        else return make_pair(1,res);//平行        
 	    }         
 	    double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));     
        res.x += (e.x-s.x)*t;    
 	    res.y += (e.y-s.y)*t;        
 	    return make_pair(2,res);     
 	}
}; 
struct shape
{
	string name;
	int cnt;
	Line l[N];
	bool operator < (const shape &u) const
	{
		return name<u.name;
	}
	void read()
	{
		string c;
		cin>>c;
		if(c[0]=='s')
		{
			Point p1,p2,p3,p0;
			scanf(" (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p2.x,&p2.y);
			p1.x=(p0.x+p2.x+p2.y-p0.y)/2.0;
			p1.y=(p0.y+p2.y-p2.x+p0.x)/2.0;
			p3.x=(p0.x+p2.x-p2.y+p0.y)/2.0;
			p3.y=(p0.y+p2.y+p2.x-p0.x)/2.0;
			cnt=4;
			l[0]=Line(p0,p1);
			l[1]=Line(p1,p2);
			l[2]=Line(p2,p3);
			l[3]=Line(p3,p0);
		}
		else if(c[0]=='l')
		{
			cnt=1;
			scanf(" (%lf,%lf) (%lf,%lf)",&l[0].s.x,&l[0].s.y,&l[0].e.x,&l[0].e.y);
		}
		else if(c[0]=='t')
		{
			cnt=3;
			scanf(" (%lf,%lf) (%lf,%lf)",&l[0].s.x,&l[0].s.y,&l[0].e.x,&l[0].e.y);
			l[1].s=l[0].e;
			scanf(" (%lf,%lf)",&l[1].e.x,&l[1].e.y);
			l[2].s=l[1].e;l[2].e=l[0].s;
		}
		else if(c[0]=='r')
		{
			Point p1,p2,p3,p0;
			scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p1.x,&p1.y,&p2.x,&p2.y);
			p3.x=(p2.x-p1.x+p0.x);
			p3.y=(p2.y-p1.y+p0.y);
			cnt=4;
			l[0]=Line(p0,p1);
			l[1]=Line(p1,p2);
			l[2]=Line(p2,p3);
			l[3]=Line(p3,p0);
		}
		else if(c[0]=='p')
		{
			cin>>cnt;
			for(int i=0;i<cnt;i++)
			{
				scanf(" (%lf,%lf)",&l[i].s.x,&l[i].s.y);
				l[(i-1+cnt)%cnt].e=l[i].s;
			}
			
		}
	}
}S[N];
//判断线段相交 
bool inter(Line l1,Line l2) 
{
	return 
	max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&     
	max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&     
	max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&     
	max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&     
	sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&     
	sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0; 
}
int n;
string c;
int main()
{
	while(1)
	{
		int n=0;
		cin>>c;
		if(c[0]=='.') break;
		if(c[0]=='-') continue;
		S[n].name=c;
		S[n++].read();
		while(cin>>c)
		{
			if(c[0]=='-') break;
			S[n].name=c;
			S[n++].read();
		}
		sort(S,S+n);
		for(int i=0;i<n;i++)
		{
			int num=0;
			int ans[N];
			for(int j=0;j<n;j++)
			{
				if(i==j) continue;
				bool flag=false;
				for(int p=0;p<S[i].cnt&&!flag;p++)
					for(int q=0;q<S[j].cnt;q++)
						if(inter(S[i].l[p],S[j].l[q]))
						{
							flag=true;
							break;
						}
				if(flag) ans[num++]=j;
			}
			if(num==0)
			{
				cout<<S[i].name<<" has no intersections"<<endl;
			}
			else if(num==1)
			{
				cout<<S[i].name<<" intersects with "<<S[ans[0]].name<<endl;
			}
			else if(num==2)
			{
				cout<<S[i].name<<" intersects with "<<S[ans[0]].name<<" and "<<S[ans[1]].name<<endl;
			}
			else
			{
				cout<<S[i].name<<" intersects with ";
				for(int i=0;i<num-1;i++)
					cout<<S[ans[i]].name<<", ";
				cout<<"and "<<S[ans[num-1]].name<<endl;
			}
		}
		puts("");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值