标签:from define 图片 ber 分享 lse lines src desc
1 #include <iostream> 2 #include <cstdlib> 3 #include <cmath> 4 using namespace std; 5 #define CPoint CVector 6 #define PI acos(-1) 7 #define INF 1e20 8 #define EPS 1e-12 9 10 struct CVector 11 { 12 double x, y; 13 CVector(double _x, double _y) : 14 x(_x), y(_y) {} 15 CVector() :x(INF), y(INF) {} 16 }; 17 18 struct CLine 19 { 20 CPoint a, b; 21 CLine(CPoint _a, CPoint _b) { 22 a = _a; 23 b = _b; 24 } 25 }; 26 27 bool isZero(double x) { 28 return -EPS < x && x < EPS; 29 } 30 31 CVector operator + (CVector p, CVector q) { 32 return CVector(p.x + q.x, p.y + q.y); 33 } 34 35 CVector operator - (CVector p, CVector q){ 36 return CVector(p.x - q.x, p.y - q.y); 37 } 38 39 double operator ^(CVector p, CVector q) { 40 return p.x * q.y - p.y * q.x; 41 } 42 43 double area(CVector p, CVector q) { 44 return (p ^ q) / 2; 45 } 46 47 double operator *(CVector p, CVector q) { 48 return p.x*q.x + p.y*q.y; 49 } 50 51 CVector operator *(double k, CVector p) { 52 return CVector(k*p.x, k*p.y); 53 } 54 55 double length(CVector p) { 56 return sqrt(p * p); 57 } 58 59 double dist(CPoint p, CLine l) { 60 return fabs((p - l.a) ^ (l.b - l.a)) / length(l.b - l.a); 61 } 62 63 CPoint interset(CLine l, CLine m, string &msg) { 64 double x = area(m.a - l.a, l.b - l.a); 65 double y = area(l.b - l.a, m.b - l.a); 66 if (isZero(x + y)) { 67 if (isZero(dist(l.a, m))) 68 msg = "LINE"; 69 else msg = "NONE"; 70 return CPoint(); 71 } 72 msg = "POINT"; 73 return m.a + (x / (x + y)) * (m.b - m.a); 74 } 75 76 int main() 77 { 78 int N = 0; 79 cin >> N; 80 printf("INTERSECTING LINES OUTPUT\n"); 81 while (N--) { 82 int x1, y1, x2, y2, x3, y3, x4, y4; 83 string msg; 84 cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; 85 CLine l = CLine(CPoint(x1, y1), CPoint(x2, y2)); 86 CLine m = CLine(CPoint(x3, y3), CPoint(x4, y4)); 87 CPoint p = interset(l, m, msg); 88 if (p.x == INF) printf("%s\n", msg.c_str()); 89 else printf("%s %.2f %.2f\n", msg.c_str(), p.x, p.y); 90 } 91 printf("END OF OUTPUT\n"); 92 //system("pause"); 93 return 0; 94 }
标签:from define 图片 ber 分享 lse lines src desc
原文地址:https://www.cnblogs.com/Jeffrey-Y/p/10166662.html