大意:给你两条直线的坐标,判断两条直线是否共线、平行、相交,若相交,求出交点。
思路:线段相交判断、求交点的水题,没什么好说的。
1 struct Point{ 2 double x, y; 3 } ; 4 struct Line{ 5 Point a, b; 6 } A, B; 7 8 double xmult(Point p1, Point p2, Point p) 9 { 10 return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x); 11 } 12 13 bool parallel(Line u, Line v) 14 { 15 return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y)); 16 } 17 18 Point intersection(Line u, Line v) 19 { 20 Point ret = u.a; 21 double t = ((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x)); 22 ret.x += (u.b.x-u.a.x)*t, ret.y += (u.b.y-u.a.y)*t; 23 return ret; 24 } 25 26 int T; 27 28 void Solve() 29 { 30 scanf("%d", &T); 31 printf("INTERSECTING LINES OUTPUT "); 32 while(T--) 33 { 34 scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &A.a.x, &A.a.y, &A.b.x, &A.b.y, &B.a.x, &B.a.y, &B.b.x, &B.b.y); 35 if(parallel(A, B) && zero(xmult(A.a, B.a, B.b))) 36 { 37 printf("LINE "); 38 } 39 else if(parallel(A, B)) 40 { 41 printf("NONE "); 42 } 43 else 44 { 45 Point t = intersection(A, B); 46 printf("POINT %.2f %.2f ", t.x, t.y); 47 } 48 } 49 printf("END OF OUTPUT "); 50 }