description
|
课件中屌丝学长关于线段相交可是讲的很清楚很彻底了,这道线段相交的水题就当做是下道题的铺垫吧。
|
input
|
输入数据有多组,每组第一行有一个整数n,表示测试实例的数目,接下来有n行,每行8个数,分别表示2条线段的起点和终点:
线段1:(x1,y1)(x2,y2)
线段2: (x3,y3) (x4,y4)
n=0表示输入结束,该组不做任何处理。
|
output
|
你的任务就是,对于每组输入,判断他们是否相交。相交输出“YES”,否则输出“no”;
|
sample_input
|
2
0 0 0 1 1 0 1 1
0 0 0 1 0 0 0 1
0
|
sample_output
此题运用的判断俩线段是否相交有两点:
☼(1)快速排斥实验
下面附上代码仅供参考:
#include <iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
typedef struct node
{
double x,y;
}Point;
typedef struct line
{
Point start,end;
}vector;
double mul(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cross(vector v1,vector v2)
{
if(max(v1.start.x,v1.end.x)>=min(v2.start.x,v2.end.x)&&
max(v2.start.x,v2.end.x)>=min(v1.start.x,v1.end.x)&&
max(v1.start.y,v2.end.y)>=min(v2.start.y,v2.end.y)&&
max(v2.start.y,v2.end.y)>=min(v1.start.y,v1.end.y)&&
mul(v2.start,v1.end,v1.start)*mul(v1.end,v2.end,v1.start)>=0&&
mul(v1.start,v2.end,v2.start)*mul(v2.end,v1.end,v2.start)>=0
)
return true;
return false;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
vector line1,line2;
cin>>line1.start.x>>line1.start.y>>line1.end.x>>line1.end.y;
cin>>line2.start.x>>line2.start.y>>line2.end.x>>line2.end.y;
if(cross(line1,line2)>0)
cout<<"YES"<<endl;
else cout<<"no"<<endl;
}
}
return 0;
}
|
转载于:https://www.cnblogs.com/martinue/p/5490584.html