poj1269---直线位置关系
题目大意:给你8个点,也就是两条直线,让你判断他们的位置关系
代码如下:
#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define EPS 1e-8
struct Point
{
double x,y;
}a,b,c,d;
double xmult(Point p1,Point p2,Point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
Point intersection(Point A, Point B, Point C, Point D)
{
//a1*x+b1*y=c1;a2*x+b2*y=c2;
Point ans;
double a1 = A.y-B.y;
double b1 = B.x-A.x;
double c1 = A.x*B.y-B.x*A.y;
double a2 = C.y-D.y;
double b2 = D.x-C.x;
double c2 = C.x*D.y-D.x*C.y;
double x = (b1*c2-b2*c1)/(a1*b2-a2*b1);
double y = (a2*c1-a1*c2)/(a1*b2-a2*b1);
ans.x=x,ans.y=y;
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int T;
cin>>T;
printf("INTERSECTING LINES OUTPUT\n");
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
if(fabs(xmult(a,b,c))<=EPS&&fabs(xmult(a,b,d)<=EPS))
printf("LINE\n");//直线重合
else if((a.y-b.y)*(d.x-c.x)==(b.x-a.x)*(c.y-d.y))
printf("NONE\n");//直线平行
else
{
Point ans = intersection(a,b,c,d);
printf("POINT %.2f %.2f\n",ans.x,ans.y);
}
}
printf("END OF OUTPUT\n");
return 0;
}
浙公网安备 33010602011771号