关闭

判断线段相交

标签: cstruct
240人阅读 评论(0) 收藏 举报


#include<stdio.h>
#include<math.h>
double MAX(double a,double b)
{
	return a>b?a:b;
}
double MIN(double a,double b)
{
	return a<b?a:b;
}

struct point 
{
	double x,y;
};
double dirction(point a,point b,point c)
{
	return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
int onsegment(point a,point b,point c)
{
	double d1=MIN(a.x,b.x);
	double d2=MAX(a.x,b.x);
	double d3=MIN(a.y,b.y);
	double d4=MAX(a.y,b.y);
	if(d1<=c.x&&d2>=c.x&&d3<c.y&&d4>=c.y)
	return 1;
	return 0;
}
int segments(point a,point b,point c,point d)
{
	double d1=dirction(c,d,a);
	double d2=dirction(c,d,b);
	double d3=dirction(a,b,c);
	double d4=dirction(a,b,d);
	if(((d1>0&&d2<0)||(d1<0&&d2>0))&&((d3>0&&d4<0)||(d3<0&&d4>0)))
	return 1;
	else if(d1==0&&onsegment(c,d,a))
	return 1;
	else if(d2==0&&onsegment(c,d,b))
	return 1;
	else if(d3==0&&onsegment(a,b,c))
	return 1;
	else if(d4==0&&onsegment(a,b,d))
	return 1;
	return 0;
}
int main()
{
	int i,m,k,j,n,ncase;
	point a,b,c,d;
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
		scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y);
		if(segments(a,b,c,d))
		printf("YES\n");
		else
		printf("NO\n");
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:147562次
    • 积分:1543
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:8篇
    • 译文:2篇
    • 评论:1条
    最新评论