http://poj.org/problem?id=1269
求直线是否相交
相交输出交点
共线输出LINE
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define eps 1e-8
using namespace std;
struct node
{
int x1,y1;
int x2,y2;
}L[10];
double x,y;
int max(int a,int b) {if(a>b) return a;return b;}
int min(int a,int b) {if(a<b) return a;return b; }
double multiply1(node a,node b)
{
return (a.x1-a.x2)*(b.y1-a.y1)-(a.y1-a.y2)*(b.x1-a.x1);
}
double multiply2(node a,node b)
{
return (a.x1-a.x2)*(b.y2-a.y1)-(a.y1-a.y2)*(b.x2-a.x1);
}
bool inter(node a,node b)
{
if(max(a.x1,a.x2)>=min(b.x1,b.x2)&&
max(b.x1,b.x2)>=min(a.x1,a.x2)&&
max(a.y1,a.y2)>=min(b.y1,b.y2)&&
max(b.y1,b.y2)>=min(a.y1,a.y2)&&
multiply1(a,b)*multiply2(a,b)<=0&&//判断线段b的两个端点是否在线段a的两侧
multiply1(b,a)*multiply2(b,a)<=0)//判断线段a的两个端点是否在线段b的两侧
return true;
else return false;
}
void GetInter(node a,node b)
{
int A1=a.y1-a.y2;
int B1=a.x2-a.x1;
int C1=A1*a.x1+B1*a.y1;
int A2=b.y1-b.y2;
int B2=b.x2-b.x1;
int C2=A2*b.x1+B2*b.y1;
y=(A2*C1-A1*C2)*1.0/(A2*B1-A1*B2);
x=(B2*C1-B1*C2)*1.0/(A1*B2-A2*B1);
}
bool Judge(node a,node b)
{
double p1=a.x1-a.x2;
double q1=a.y1-a.y2;
double p2=b.x1-b.x2;
double q2=b.y1-b.y2;
if(fabs(p1-0)<=eps && fabs(p2-0)<=eps)
return true;
if(fabs(p1-0)<=eps && fabs(p2-0)>=eps || fabs(p1-0)>=eps && fabs(p2-0)<=eps)
return false;
double k1=q1*1.0/p1;
double k2=q2*1.0/p2;
if(fabs(k1-k2)<=eps)
return true;
return false;
}
int main()
{
int n;
int i;
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
while(n--)
{
for(i=0;i<2;i++)
scanf("%d%d%d%d",&L[i].x1,&L[i].y1,&L[i].x2,&L[i].y2);
if(inter(L[0],L[1]) )
{
if(Judge(L[0],L[1]))
printf("LINE\n");
else
{
GetInter(L[0],L[1]);
printf("POINT %.2lf %.2lf\n",x,y);
}
}
else if(Judge(L[0],L[1]))
{
if(fabs(multiply1(L[0],L[1])-0)<eps)
printf("LINE\n");
else printf("NONE\n");
}
else
{
GetInter(L[0],L[1]);
printf("POINT %.2lf %.2lf\n",x,y);
}
}
printf("END OF OUTPUT\n");
return 0;
}