题意:
判断直线间位置关系: 相交,平行,重合
1 include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct Point 5 { 6 int x , y; 7 Point(int a = 0, int b = 0) :x(a), y(b) {} 8 }; 9 struct Line 10 { 11 Point s, e; 12 int a, b, c;//a>=0 13 Line() {} 14 Line(Point s1,Point e1) : s(s1), e(e1) {} 15 void Coefficient()//get a,b,c 16 { 17 a = e.y - s.y; 18 b = s.x - e.x; 19 c = e.x * s.y - s.x * e.y; 20 if(a < 0) a = -a,b = -b,c = -c; 21 } 22 }; 23 int Line_Inter_Line(Line & l1,Line & l2, double &x,double &y)//直线 平行: 0, 相交:1, 重合: 2 24 { 25 l1.Coefficient(); 26 l2.Coefficient(); 27 if(l1.a * l2.b == l2.a * l1.b) 28 { 29 if(l1.b * l2.c != l1.c * l2.b || l1.a * l2.c != l1.c * l2.a) return 0; 30 else return 2; 31 } 32 x =(double) - (l1.c * l2.b - l2.c * l1.b) / (l1.a * l2.b - l2.a * l1.b);// (c1b2-c2b1)/(a1b2-a2b1) 33 y =(double) (l1.c * l2.a - l2.c * l1.a) / (l1.a * l2.b - l2.a * l1.b);// (c1a2-c2a1)/(a1b2-a2b1) 34 return 1; 35 } 36 int n; 37 int main() 38 { 39 puts("INTERSECTING LINES OUTPUT"); 40 scanf("%d", &n); 41 while (n--) 42 { 43 Point p1, p2, p3, p4; 44 double x, y; 45 scanf("%d%d%d%d%d%d%d%d",&p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y); 46 Line l1 = Line(p1, p2), l2 = Line(p3, p4); 47 int f = Line_Inter_Line(l1, l2, x, y); 48 if (f == 0) puts("NONE"); 49 else if (f == 2) puts("LINE"); 50 else printf("POINT %.2f %.2f\n", x, y); 51 } 52 puts("END OF OUTPUT"); 53 }
时间: 2024-10-23 20:34:13