#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
#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
