线段判交

线段判交模版

#include<iostream>

using namespace std;

inline int max(int a,int b)
{
	return a>b? a:b;
}
inline int min(int a,int b)
{
	return a<b? a:b;
}

struct point
{
	int x;
	int y;
};
struct line
{
	point s;
	point e;
}l1,l2;

inline int crossProduct(line a, line b)  //前一条叉乘后一条
{
	return (a.e.x-a.s.x) * (b.e.y-b.s.y) - (a.e.y-a.s.y) * (b.e.x-b.s.x);	
}

bool judge(line P,line Q);
bool judgeRec(line P,line Q);

int main()
{
	while(true)
	{
		cin>>l1.s.x>>l1.s.y>>l1.e.x>>l1.e.y;
		cin>>l2.s.x>>l2.s.y>>l2.e.x>>l2.e.y;

		if( judge(l1,l2) )
			cout<<"相交"<<endl;
		else
			cout<<"不相交"<<endl;
	}
	return 0;
}

bool judge(line P,line Q)
{
	if(!judgeRec(P,Q))
		return false;

	line L1,L2,L3;

	L1.s=Q.s;
	L1.e=P.s;
	L2=Q;
	L3.s=Q.s;
	L3.e=P.e;

	if( crossProduct(L1,L2)*crossProduct(L2,L3) <0)
		return false;
	L1.s=P.s;
	L1.e=Q.e;
	L2=P;
	L3.s=P.s;
	L3.e=Q.s;

	if( crossProduct(L1,L2)*crossProduct(L2,L3)<0)
		return false;

	return true;
}

bool judgeRec(line a,line b)         //判断以两条线段为对角线的矩形是否相交(快速排除)
{
	if( max(a.s.x,a.e.x)>min(b.s.x,b.e.x) && max(b.s.x,b.e.x)>min(a.s.x,a.e.x)
		&& max(a.s.y,a.e.y)>min(b.s.y,b.e.y) && max(b.s.y,b.e.y)>min(a.s.y,a.e.y) )
		return true;
	return false;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值