首页 > 代码库 > poj 1269 计算几何
poj 1269 计算几何
1 /** 2 判断直线位置关系 3 **/ 4 #include <iostream> 5 #include <cmath> 6 #include <cstdio> 7 using namespace std; 8 struct point { 9 double x,y; 10 point(double x=0,double y=0):x(x),y(y){} 11 }; 12 13 typedef point Vector; 14 15 Vector operator - (point A,point B){ 16 return Vector(A.x-B.x,A.y-B.y); 17 } 18 19 struct line { 20 point a,b; 21 }; 22 double length(Vector v){ 23 return sqrt(v.x*v.x+v.y*v.y); 24 } 25 26 double cross(Vector A,Vector B){ 27 return A.x*B.y-A.y*B.x; 28 } 29 30 double distoline(point P,point A,point B){ 31 Vector v1 =B-A,v2 = P-A; 32 return fabs(cross(v1,v2))/length(v1); 33 } 34 35 int main() 36 { 37 int n; 38 cin>>n; 39 line l1,l2; 40 cout<<"INTERSECTING LINES OUTPUT"<<endl; 41 while(n--){ 42 cin>>l1.a.x>>l1.a.y>>l1.b.x>>l1.b.y; 43 cin>>l2.a.x>>l2.a.y>>l2.b.x>>l2.b.y; 44 Vector tmp1,tmp2; 45 tmp1.x = l1.b.x-l1.a.x; 46 tmp1.y = l1.b.y-l1.a.y; 47 tmp2.x = l2.b.x-l2.a.x; 48 tmp2.y = l2.b.y-l2.a.y; 49 //cout<<tmp1.x<<" "<<tmp1.y<<endl; 50 //cout<<tmp2.x<<" "<<tmp2.y<<endl; 51 //cout<<cross(tmp1,tmp2)<<endl; 52 if(cross(tmp1,tmp2)==0){ 53 if(distoline(l1.a,l2.a,l2.b)==0){ 54 cout<<"LINE"<<endl; 55 }else 56 cout<<"NONE"<<endl; 57 }else{ 58 double x,y; 59 if(l1.a.x==l1.b.x&&l2.a.x!=l2.b.x){ 60 x = l1.a.x; 61 double k = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x); 62 double b = l2.a.y-k*l2.a.x;; 63 y = k*x+b; 64 }else if(l1.a.x!=l1.b.x&&l2.a.x==l2.b.x){ 65 x = l2.a.x; 66 double k = (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x); 67 double b = l1.a.y-k*l1.a.x; 68 y = k*x+b; 69 }else{ 70 double k1= (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x); 71 double b1=l1.a.y-k1*l1.a.x; 72 double k2 = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x); 73 double b2=l2.a.y-k2*l2.a.x; 74 x =(b2-b1)/(k1-k2); 75 y = k1*x+b1; 76 } 77 78 printf("POINT %.2lf %.2lf\n",x,y); 79 } 80 } 81 cout<<"END OF OUTPUT"<<endl; 82 return 0; 83 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。