不知道怎么判断两条线段相交,学习学习吧
#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;
const double eps = 0.00;
typedef struct Point{
double x,y;
}Point;
typedef struct Line{
Point a,b;
}Line;
double 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)
{
double maxx = max(a.x,b.x);
double maxy = max(a.y,b.y);
double minx = min(a.x,b.x);
double 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)
{
double d1 = Direction(p3,p4,p1);
double d2 = Direction(p3,p4,p2);
double d3 = Direction(p1,p2,p3);
double d4 = Direction(p1,p2,p4);
if( d1 * d2 < eps && d3 * d4 < eps )
return true;
if( d1 == 0.0 && onSegment(p3,p4,p1) )
return true;
if( d2 == 0.0 && onSegment(p3,p4,p2) )
return true;
if( d3 == 0.0 && onSegment(p1,p2,p3) )
return true;
if( d4 == 0.0 && onSegment(p1,p2,p4) )
return true;
return false;
}
Line l[2010];
int main()
{
int n;
while( ~scanf("%d",&n) )
{
for(int i=0; i<n; i++)
scanf("%lf %lf %lf %lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y);
for(int i=0; i<n; i++)
for(int k=i+1; k<n; k++)
if( segIntersect(l[i].a,l[i].b,l[k].a,l[k].b))
{
printf("burned!\n");
goto end;
}
printf("ok!\n");
end:;
}
return 0;
}