标签:
http://poj.org/problem?id=1269
给出两条直线,判断它们是平行,重合,还是相交,如果相交,求出交点.
比较裸的一道题.学习了直线的写法(参数方程)
1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 const double eps=1e-8; 6 7 struct pt{ double x,y; pt(double x=0,double y=0):x(x),y(y){} }; 8 typedef pt vt; 9 int dcmp(double x){ if(fabs(x)<eps) return 0; return x>0?1:-1; } 10 vt operator + (vt a,vt b){ return vt(a.x+b.x,a.y+b.y); } 11 vt operator - (vt a,vt b){ return vt(a.x-b.x,a.y-b.y); } 12 vt operator * (vt a,double p){ return vt(a.x*p,a.y*p); } 13 double cross(vt a,vt b){ return a.x*b.y-a.y*b.x; } 14 struct line{ 15 pt p; vt v; 16 line(){} 17 line(pt a,pt b){ p=a; v=b-a; } 18 }; 19 int line_intersection(line A,line B){ 20 if(dcmp(cross(A.v,B.v)!=0)) return -1; 21 return dcmp(cross(A.v,A.p-B.p))==0; 22 } 23 pt get_line_intersection(line A,line B){ 24 vt v=A.v,w=B.v,u=A.p-B.p; 25 double t=cross(w,u)/cross(v,w); 26 return A.p+v*t; 27 } 28 int main(){ 29 int n; 30 scanf("%d",&n); 31 puts("INTERSECTING LINES OUTPUT"); 32 while(n--){ 33 pt p[4]; line l[2]; 34 for(int i=0;i<4;i++) scanf("%lf%lf",&p[i].x,&p[i].y); 35 l[0]=line(p[0],p[1]); 36 l[1]=line(p[2],p[3]); 37 int t=line_intersection(l[0],l[1]); 38 if(t==-1){ 39 pt x=get_line_intersection(l[0],l[1]); 40 printf("POINT %.2lf %.2lf\n",x.x,x.y); 41 } 42 else if(t==1) puts("LINE"); 43 else puts("NONE"); 44 } 45 puts("END OF OUTPUT"); 46 return 0; 47 }
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13622 | Accepted: 6060 |
Description
Input
Output
Sample Input
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
Sample Output
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
Source
POJ_1269_Intersecting_Lines_(计算几何基础)
标签:
原文地址:http://www.cnblogs.com/Sunnie69/p/5536416.html