poj 1269 计算几何

时间:2023-03-08 15:45:59
 /**
判断直线位置关系
**/
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
struct point {
double x,y;
point(double x=,double y=):x(x),y(y){}
}; typedef point Vector; Vector operator - (point A,point B){
return Vector(A.x-B.x,A.y-B.y);
} struct line {
point a,b;
};
double length(Vector v){
return sqrt(v.x*v.x+v.y*v.y);
} double cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
} double distoline(point P,point A,point B){
Vector v1 =B-A,v2 = P-A;
return fabs(cross(v1,v2))/length(v1);
} int main()
{
int n;
cin>>n;
line l1,l2;
cout<<"INTERSECTING LINES OUTPUT"<<endl;
while(n--){
cin>>l1.a.x>>l1.a.y>>l1.b.x>>l1.b.y;
cin>>l2.a.x>>l2.a.y>>l2.b.x>>l2.b.y;
Vector tmp1,tmp2;
tmp1.x = l1.b.x-l1.a.x;
tmp1.y = l1.b.y-l1.a.y;
tmp2.x = l2.b.x-l2.a.x;
tmp2.y = l2.b.y-l2.a.y;
//cout<<tmp1.x<<" "<<tmp1.y<<endl;
//cout<<tmp2.x<<" "<<tmp2.y<<endl;
//cout<<cross(tmp1,tmp2)<<endl;
if(cross(tmp1,tmp2)==){
if(distoline(l1.a,l2.a,l2.b)==){
cout<<"LINE"<<endl;
}else
cout<<"NONE"<<endl;
}else{
double x,y;
if(l1.a.x==l1.b.x&&l2.a.x!=l2.b.x){
x = l1.a.x;
double k = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x);
double b = l2.a.y-k*l2.a.x;;
y = k*x+b;
}else if(l1.a.x!=l1.b.x&&l2.a.x==l2.b.x){
x = l2.a.x;
double k = (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x);
double b = l1.a.y-k*l1.a.x;
y = k*x+b;
}else{
double k1= (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x);
double b1=l1.a.y-k1*l1.a.x;
double k2 = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x);
double b2=l2.a.y-k2*l2.a.x;
x =(b2-b1)/(k1-k2);
y = k1*x+b1;
} printf("POINT %.2lf %.2lf\n",x,y);
}
}
cout<<"END OF OUTPUT"<<endl;
return ;
}