题目链接:http://poj.org/problem?id=1269
题意:给两条直线,判断这两条直线是平行中的重合还是相交。
一切尽在不言中:
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const double eps = 1e-8; 23 24 typedef struct Point { 25 double x; 26 double y; 27 Point() {} 28 Point(double xx, double yy) : x(xx), y(yy) {} 29 Point operator +(const Point& b) const { return Point(x+b.x, y+b.y); } 30 Point operator -(const Point& b) const { return Point(x-b.x, y-b.y); } 31 double operator ^(const Point& b) const { return x * b.y - y * b.x; } 32 double operator *(const Point& b) const { return x * b.x + y * b.y; } 33 }Point; 34 35 typedef struct Line { 36 Point u; 37 Point v; 38 Line() {} 39 Line(Point uu, Point vv) : u(uu), v(vv) {} 40 }Line; 41 42 int sgn(double x) { 43 if(fabs(x) < eps) return 0; 44 if(x < 0) return -1; 45 return 1; 46 } 47 48 double xmulti(Point p0, Point p1, Point p2) { 49 return (p1 - p0) ^ (p2 - p0); 50 } 51 52 double dist(Point a, Point b) { 53 return sqrt((b-a)*(b-a)); 54 } 55 56 double intersect(Line l1, Line l2) { 57 return sgn(xmulti(l2.u, l1.u, l1.v))*sgn(xmulti(l2.v, l1.u, l1.v))<=0; 58 } 59 60 double ex1, ey1, ex2, ey2; 61 double ex3, ey3, ex4, ey4; 62 63 pair<Point, int> check(Line l1, Line l2) { 64 Point ans = l1.u; 65 if(sgn((l1.u-l1.v)^(l2.u-l2.v)) == 0) { 66 if(sgn((l2.u-l1.u)^(l2.v-l1.u)) == 0) { 67 return pair<Point, int>(ans, 0);//chonghe 68 } 69 else { 70 return pair<Point, int>(ans, 1);//pingxing 71 } 72 } 73 double t = ((l1.u - l2.u) ^ (l2.u - l2.v)) / ((l1.u - l1.v) ^ (l2.u - l2.v)); 74 ans.x += (l1.v.x - l1.u.x) * t; 75 ans.y += (l1.v.y - l1.u.y) * t; 76 return pair<Point, int>(ans, 2);//jiaodian ans 77 } 78 79 int main() { 80 // freopen("in", "r", stdin); 81 int T; 82 scanf("%d", &T); 83 printf("INTERSECTING LINES OUTPUT "); 84 while(T--) { 85 scanf("%lf %lf %lf %lf", &ex1, &ey1, &ex2, &ey2); 86 scanf("%lf %lf %lf %lf", &ex3, &ey3, &ex4, &ey4); 87 Line l1 = Line(Point(ex1, ey1), Point(ex2, ey2)); 88 Line l2 = Line(Point(ex3, ey3), Point(ex4, ey4)); 89 pair<Point, int> ans = check(l1, l2); 90 if(ans.second == 0) { 91 printf("LINE "); 92 } 93 else if(ans.second == 1) { 94 printf("NONE "); 95 } 96 else { 97 printf("POINT %.2f %.2f ", ans.first.x, ans.first.y); 98 } 99 } 100 printf("END OF OUTPUT "); 101 return 0; 102 }