Complex intersecting line segments
Time Limit: 2000 MS Memory Limit: 65536 K
Description
In this problem, you are asked to determine if a set of line segments intersect.
Input
The first line of input is a number c ≤ 100, the number of test cases.
Each test case is a single line containing four complex numbers s1, t1, s2, t2 separated by spaces. Each complex number a+bi represents a point (a, b) in the xy-plane. In all cases, a and b will be integers between -100 and 100 for a complex number a + bi.
Note that the real part of the complex number may be omitted if it is 0 and the imaginary part may be omitted if it is 0 (and if both are 0, the number is written 0). Also, if the imaginary part is +1 or -1, that part may be written as +i or -i (rather than +1i and -1i). See sample input for examples.
All line segments given as input have non-zero length (i.e., the two endpoints are always different).
Output
For each test case, you should output a single line containing “intersect” if the line segment from s1 to t1 intersects the line segment from s2 to t2 (the endpoints are considered to be part of the segment) and “do not intersect” otherwise.
Sample Input
3 2-2i -2-2i 2+2i -2+2i 1+i 2-i 2+i 1-i 0 i 1 -1+2i
Sample Output
do not intersect intersect intersect
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const double eps=1e-10;
struct Node
{
double x, y;
};
//吉大模板
bool inter(Node a, Node b, Node c, Node d){
if ( min(a.x, b.x) > max(c.x, d.x) ||
min(a.y, b.y) > max(c.y, d.y) ||
min(c.x, d.x) > max(a.x, b.x) ||
min(c.y, d.y) > max(a.y, b.y) ) return 0;
double h, i, j, k;
h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}
int main()
{
int t;
scanf("%d",&t);
string s;
while(t--)
{ Node po[4];
int ppo=0;
for(int i=0;i<4;i++)
{
cin>>s;
double x,y;
double temp;
char ch;
int i,flag,k;
flag=1;
x=0,y=0;
k=0,temp=0;
if(s[k]=='-')
{
flag=-1;
k=1;
}
for(i=k;i<s.length();i++)//从符号后开始
if(s[i]=='-')
{
x=temp*flag;
flag=-1;
temp=0;
}
else if(s[i]=='+') {
x=temp*flag;
flag=1;
temp=0;
}
else if(s[i]=='i'){
if(temp>0)
y=temp*flag;
else y=flag;
temp=0;
}
else temp=temp*10.0+s[i]-'0';
if(temp>0) x=temp*flag;
po[ppo].x=x;
po[ppo].y=y;
ppo++;
}
if(inter(po[0],po[1],po[2],po[3]))
printf("intersect/n");
else printf("do not intersect/n");
}
return 0;
}