SWJTU 1688 线段相交

Complex segments

Time Limit:1000MS  Memory Limit:65536K Total Submit:30 Accepted:9

Description

Do two segments intersect? Can you tell me?

Input

The first line of the input contains an integer T (0 < T < 1000), indicating the number of cases. Each test case contains four complex numbers s1, t1, s2, and 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 (include) 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 case, you should print a line containing the case number (beginning with 1) and print “YES” if the two segments intersect, or print “NO”.

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

 

 

Case #1: NO
Case #2: YES
Case #3: YES

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<ctype.h>
#include<iomanip>
#include<set>
#include<map>
#include<list>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const double pi=acos(-1.0);
const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
struct Line
{
    Point s,e;
    double k;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
        k = atan2(e.y - s.y,e.x - s.x);
    }
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)
                return make_pair(0,res);
            else return make_pair(1,res);
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};
bool inter(Line l1,Line l2)
{
    return
        max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
        max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
        max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
        max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
        sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
        sgn((l1.s-l2.s)^(l2.e-l1.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
Point change(string s)
{
      int s1,s2;
      if(s.find('i')==-1)
      {
           s2=0;
           s1=0;
           int i=0;
           if(s[i]>='0'&&s[i]<='9')s1=10*s1+s[i]-'0';i++;
           while(i<s.length())s1=10*s1+s[i]-'0',i++;
           if(s[0]=='-')s1=-s1;
           return Point(s1+0.0,0.0);
      }
      else
      {
           int i=0;
           if(s[i]=='+'||s[i]=='-')i++;
           if(s.find('+',i)!=-1||s.find('-',i)!=-1)
           {
                 s1=s2=0;
                 while(s[i]!='+'&&s[i]!='-')s1=10*s1+s[i]-'0',i++;
                 if(s[0]=='-')s1=-s1;
                 int k=i;i++;
                 while(s[i]!='i')s2=10*s2+s[i]-'0',i++;
                 if(s2==0)s2=1;
                 if(s[k]=='-')s2=-s2;
                 return Point(s1+0.0,s2+0.0);
           }
           else
           {
                 s1=0;s2=0;
                 while(s[i]!='i')s2=10*s2+s[i]-'0',i++;
                 if(s2==0)s2=1;
                 if(s[0]=='-')s2=-s2;

                 return Point(s1+0.0,s2+0.0);
           }
      }
}
int main()
{
      int i,j,k,m,n,T,t;
      string s;Point a,b,c,d;
      Line s1,s2;
      scanf("%d",&T);
      for(t=1;t<=T;t++)
      {
            cin>>s;a=change(s);
            cin>>s;b=change(s);
            cin>>s;c=change(s);
            cin>>s;d=change(s);
            Line s1(a,b);
            Line s2(c,d);
            printf("Case #%d: ",t);
            if(inter(s1,s2))puts("YES");
            else puts("NO");
      }
      return 0;
}
View Code

 

posted @ 2013-09-20 21:08  线性无关  阅读(158)  评论(0)    收藏  举报