This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
line segment intersection
49 lines (47 loc) · 1.58 KB
/
line segment intersection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <math.h>
int main(void)
{
long long int x1,x2,x3,x4,y1,y2,y3,y4;
int t;
scanf("%d",&t);
while(t!=0)
{
t--;
scanf("%lld %lld %lld %lld %lld %lld %lld %lld",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
double s =0;
double t =0;
if(((x2-x1)*(y3-y4) - (y2-y1)*(x3-x4)) != 0)
{
double s = -1*((double)((x3-x4)*(y3-y1))-((y3-y4)*(x3-x1)))/((x2-x1)*(y3-y4) - (y2-y1)*(x3-x4));
double t= ((double)((x2-x1)*(y3-y1) - ((y2-y1)*(x3-x1))))/((x2-x1)*(y3-y4) - (y2-y1)*(x3-x4));
if(s>=0 && s<=1 && t>=0 && t<=1)
printf("Yes\n");
else
printf("No\n");
}
else
{
if((x3-x4)*(y3-y1) - (y3-y4)*(x3-x1) == 0 || (x2-x1)*(y3-y1) - (y2-y1)*(x3-x1)==0)
{
if((x1<=x3 && x1>=x4) || (x1<=x4 && x1>=x3) || (x2<=x3 && x2>=x4) || (x2<=x4 && x2>=x3) || (x1<=x3 && x1<=x4 && x2>=x3 && x2>=x4) || (x2<=x3 && x2<=x4 && x2>=x3 && x2>=x4))
{
if((y1<=y3 && y1>=y4) || (y1<=y4 && y1>=y3) || (y2<=y3 && y2>=y4) || (y2<=y4 && y2>=y3))
{
printf("Yes\n");
continue;
}
else if((y1<=y3 && y2<=y3 && y1>=y4 && y2>=y4) || (y1<=y4 && y2<=y4 && y2>=y3 && y2>=y3) || (y1<=y3 && y1<=y4 && y2>=y3 && y2>=y4 ) || (y2<=y3 && y2<=y4 && y1>=y3 && y1>=y4))
{
printf("Yes\n");
continue;
}
}
printf("No\n");
}
else
printf("No\n");
}
}
return 0;
}