POJ 1269 Intersecting Lines(直线求交点)

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect. 
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000. 

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".
 
题目大意:以两点式给出直线,求交点。
思路:继续做模板,虽然不知道它什么原理。
 
代码(0MS):
  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 }
View Code

 

你可能感兴趣的:(intersect)