泫若蓼蓝吧 关注:19贴子:1,926
  • 4回复贴,共1
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef struct Point{
     int x,y;
}Point;
int Direction(Point a, Point b, Point c )
{
     return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool Onsegment(Point a, Point b, Point c)
{
     int maxx = max(a.x,b.x);
     int maxy = max(a.y,b.y);
     int minx = min(a.x,b.x);
     int miny = min(a.y,b.y);
     if( c.x >= minx && c.x <= maxx && c.y >= miny && c.y <= maxy )
         return true;
     return false;
}
bool SegIntersect(Point p1,Point p2, Point p3, Point p4)
{
     int d1 = Direction(p3,p4,p1);
     int d2 = Direction(p3,p4,p2);
     int d3 = Direction(p1,p2,p3);
     int d4 = Direction(p1,p2,p4);
     if( d1 > 0 && d2 < 0 || d1 < 0 && d2 > 0 ||   d3 > 0 && d4 < 0  
                 || d3 < 0 && d4 > 0 )
         return true;
     if( d1 == 0 && Onsegment(p3,p4,p1) )
         return true;
     if( d2 == 0 && Onsegment(p3,p4,p2) )
         return true;
     if( d3 == 0 && Onsegment(p1,p2,p3) )
         return true;
     if( d4 == 0 && Onsegment(p1,p2,p4) )
         return true;
     return false;
}
int main()
{
     Point p[4];
     for(int i=0; i<4; i++)
         cin >> p[i].x >> p[i].y;
     cout << SegIntersect(p[0],p[1],p[2],p[3]);
system("pause");
return 0;
}
有BUG


1楼2011-04-06 23:31回复
    什么神秘代码?


    来自Android客户端4楼2013-08-21 22:33
    回复
      广告
      立即查看
      此吧还更新否?


      来自Android客户端5楼2014-02-17 18:59
      收起回复