Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)

时间:2021-08-01 13:59:03

http://poj.org/problem?id=1269

我今天才知道原来标准的浮点输出用%.2f   并不是%.2lf  所以wa了好几次

题目大意:   就给你两个线段 然后求这两个线段所在的直线的关系  有共线  平行  和相交

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define N 200
const double ESP = 1e-;
struct Point
{
double x, y; Point(double x=,double y=):x(x),y(y) {}
Point operator + (const Point &temp)const{
return Point(x+temp.x, y+temp.y);
}
Point operator - (const Point &temp)const{
return Point(x-temp.x, y-temp.y);
}
bool operator == (const Point &temp)const{
return (fabs(x-temp.x) < ESP && fabs(y-temp.y) < ESP);
}
int operator * (const Point &temp)const{
double t=(x*temp.y)-(y*temp.x);
if(t > ESP)
return ;
if(fabs(t) < ESP)
return ;
return -;
}
}; struct node
{
Point A,B;
node(Point A=,Point B=):A(A),B(B){} }; Point line(Point u1,Point u2,Point v1,Point v2)///求交点模板
{
Point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t; return ret;
} int main()
{
int n;
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
while(n--)
{
Point p[];
node a[];
double x1,x2,x3,x4,y1,y2,y3,y4;
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
p[]=Point(x1,y1);
p[]=Point(x2,y2);
p[]=Point(x3,y3);
p[]=Point(x4,y4);
a[]=node(p[],p[]);
a[]=node(p[],p[]);
if(fabs((a[].A-a[].A)*(a[].B-a[].A))== && fabs((a[].B-a[].A)*(a[].B-a[].A))==)///判断共线 如果a[1]的两个点都在直线a[2]上 就说明共线
printf("LINE\n");
else
{
if(fabs((y2-y1)*(x4-x3)-(y4-y3)*(x2-x1))<ESP)///如果不共线 并且斜率相等的话 就说明是平行
printf("NONE\n");
else///求交点
{
Point d;
d=line(p[],p[],p[],p[]);
printf("POINT %.2f %.2f\n",d.x,d.y);
}
}
}
printf("END OF OUTPUT\n");
return ;
}