Description
Input
Output
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 const double EPS = 1e-8; 9 const double PI = acos(-1.0);//3.14159265358979323846 10 11 inline int sgn(double x) { 12 return (x > EPS) - (x < -EPS); 13 } 14 15 struct Point { 16 double x, y; 17 Point() {} 18 Point(double x, double y): x(x), y(y) {} 19 void read() { 20 scanf("%lf%lf", &x, &y); 21 } 22 bool operator < (const Point &rhs) const { 23 if(y != rhs.y) return y < rhs.y; 24 return x < rhs.x; 25 } 26 Point operator + (const Point &rhs) const { 27 return Point(x + rhs.x, y + rhs.y); 28 } 29 Point operator - (const Point &rhs) const { 30 return Point(x - rhs.x, y - rhs.y); 31 } 32 Point operator * (const int &b) const { 33 return Point(x * b, y * b); 34 } 35 Point operator / (const int &b) const { 36 return Point(x / b, y / b); 37 } 38 double length() const { 39 return sqrt(x * x + y * y); 40 } 41 Point unit() const { 42 return *this / length(); 43 } 44 }; 45 typedef Point Vector; 46 47 double dist(const Point &a, const Point &b) { 48 return (a - b).length(); 49 } 50 51 double cross(const Point &a, const Point &b) { 52 return a.x * b.y - a.y * b.x; 53 } 54 //ret >= 0 means turn left 55 double cross(const Point &sp, const Point &ed, const Point &op) { 56 return sgn(cross(sp - op, ed - op)); 57 } 58 59 struct Seg { 60 Point st, ed; 61 Seg() {} 62 Seg(Point st, Point ed): st(st), ed(ed) {} 63 void read() { 64 st.read(); ed.read(); 65 } 66 }; 67 typedef Seg Line; 68 69 bool isIntersected(const Point &s1, const Point &e1, const Point &s2, const Point &e2) { 70 return (max(s1.x, e1.x) >= min(s2.x, e2.x)) && 71 (max(s2.x, e2.x) >= min(s1.x, e1.x)) && 72 (max(s1.y, e1.y) >= min(s2.y, e2.y)) && 73 (max(s2.y, e2.y) >= min(s1.y, e1.y)) && 74 (cross(s2, e1, s1) * cross(e1, e2, s1) >= 0) && 75 (cross(s1, e2, s2) * cross(e2, e1, s2) >= 0); 76 } 77 78 bool isIntersected(const Seg &a, const Seg &b) { 79 return isIntersected(a.st, a.ed, b.st, b.ed); 80 } 81 82 bool isParallel(const Seg &a, const Seg &b) { 83 return sgn(cross(a.ed - a.st, b.ed - b.st)) == 0; 84 } 85 86 //return Ax + By + C =0 's A, B, C 87 void Coefficient(const Line &L, double &A, double &B, double &C) { 88 A = L.ed.y - L.st.y; 89 B = L.st.x - L.ed.x; 90 C = L.ed.x * L.st.y - L.st.x * L.ed.y; 91 } 92 93 Point intersection(const Line &a, const Line &b) { 94 double A1, B1, C1; 95 double A2, B2, C2; 96 Coefficient(a, A1, B1, C1); 97 Coefficient(b, A2, B2, C2); 98 Point I; 99 I.x = - (B2 * C1 - B1 * C2) / (A1 * B2 - A2 * B1); 100 I.y = (A2 * C1 - A1 * C2) / (A1 * B2 - A2 * B1); 101 return I; 102 } 103 104 bool isEqual(const Line &a, const Line &b) { 105 double A1, B1, C1; 106 double A2, B2, C2; 107 Coefficient(a, A1, B1, C1); 108 Coefficient(b, A2, B2, C2); 109 return sgn(A1 * B2 - A2 * B1) == 0 && sgn(A1 * C2 - A2 * C1) == 0 && sgn(B1 * C2 - B2 * C1) == 0; 110 } 111 112 /*******************************************************************************************/ 113 114 115 Line a, b; 116 int n; 117 118 int main() { 119 scanf("%d", &n); 120 puts("INTERSECTING LINES OUTPUT"); 121 for(int i = 0; i < n; ++i) { 122 a.read(), b.read(); 123 if(isParallel(a, b)) { 124 if(isEqual(a, b)) puts("LINE"); 125 else puts("NONE"); 126 } 127 else { 128 Point ans = intersection(a, b); 129 printf("POINT %.2f %.2f\n", ans.x, ans.y); 130 } 131 } 132 puts("END OF OUTPUT"); 133 }