水题,以前总结的模板还是很好用的。
1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 5 const double eps = 1e-8; 6 7 int dcmp(double x) 8 { 9 if(fabs(x) < eps) return 0; 10 return x < 0 ? -1 : 1; 11 } 12 13 struct Point 14 { 15 double x, y; 16 Point(double x=0, double y=0):x(x), y(y) {} 17 }; 18 typedef Point Vector; 19 20 Point read_point() 21 { 22 double x, y; 23 scanf("%lf%lf", &x, &y); 24 return Point(x, y); 25 } 26 27 Point operator + (const Point& A, const Point& B) 28 { return Point(A.x+B.x, A.y+B.y); } 29 30 Point operator - (const Point& A, const Point& B) 31 { return Point(A.x-B.x, A.y-B.y); } 32 33 Point operator * (const Point& A, double p) 34 { return Point(A.x*p, A.y*p); } 35 36 double Cross(const Point& A, const Point& B) 37 { return A.x*B.y - A.y*B.x; } 38 39 double Length(const Vector v) 40 { return sqrt(v.x*v.x + v.y*v.y); } 41 42 double DistanceToLine(Point P, Point A, Point B) 43 { 44 Vector v1 = B - A, v2 = P - A; 45 return fabs(Cross(v1, v2)) / Length(v1); 46 } 47 48 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) 49 { 50 Vector u = P - Q; 51 double t = Cross(w, u) / Cross(v, w); 52 return P + v*t; 53 } 54 55 int main() 56 { 57 //freopen("in.txt", "r", stdin); 58 59 int n; 60 scanf("%d", &n); 61 puts("INTERSECTING LINES OUTPUT"); 62 Point A, B, C, D; 63 while(n--) 64 { 65 A = read_point(); B = read_point(); 66 C = read_point(); D = read_point(); 67 Vector v1 = B - A, v2 = D - C; 68 if(dcmp(Cross(v1, v2)) == 0) 69 { 70 if(dcmp(DistanceToLine(C, A, B)) == 0) puts("LINE"); 71 else puts("NONE"); 72 73 } 74 else 75 { 76 Point ans = GetLineIntersection(A, v1, C, v2); 77 printf("POINT %.2f %.2f\n", ans.x, ans.y); 78 } 79 } 80 puts("END OF OUTPUT"); 81 82 return 0; 83 }
代码君
时间: 2024-11-13 10:41:21