线段相交
description |
课件中屌丝学长关于线段相交可是讲的很清楚很彻底了,这道线段相交的水题就当做是下道题的铺垫吧。 |
input |
|
output |
|
sample_input |
|
sample_output
此题运用的判断俩线段是否相交有两点:
☼(1)快速排斥实验
☼(2)跨立实验
具体内容网上都有详解。下面附上代码仅供参考:
#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;
}
|
持续更新博客地址:
blog.csdn.net/martinue

浙公网安备 33010602011771号