描述
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.
输入
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).输出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".
样例输入
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
样例输出
INTERSECTING LINES OUTPUT POINT 2.00 2.00 NONE LINE POINT 2.00 5.00 POINT 1.07 2.20 END OF OUTPUT
来源
Mid-Atlantic 1996
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <set> 11 #include <vector> 12 #include <fstream> 13 #define maxn 10005 14 #define inf 999999 15 #define cha 127 16 #define eps 1e-6 17 using namespace std; 18 19 struct Vector { 20 double x, y; 21 Vector(double a, double b) { 22 x = a, y = b; 23 } 24 Vector() {} 25 }; 26 #define point Vector 27 point operator -(point a, point b) { 28 return point(a.x - b.x, a.y - b.y); 29 } 30 double cross(Vector v1, Vector v2) { 31 return v1.x*v2.y - v1.y*v2.x; 32 } 33 double area(Vector p1, Vector p2) { 34 return cross(p1, p2) / 2; 35 } 36 struct seg { 37 point a, b; 38 seg(point aa, point bb) :a(aa), b(bb) {} 39 seg() {} 40 }; 41 point p1, p2, p3, p4; 42 seg s1, s2; 43 44 void getcross() { 45 double a1 = area(p3 - p1, p4 - p1), a2 = area(p4 - p2, p3 - p2); 46 if (cross(p2 - p1, p3 - p4) == 0) { 47 if (cross(p2-p1,p3-p1)==0) { 48 printf("LINE\n"); 49 return; 50 } 51 printf("NONE\n"); 52 return; 53 } 54 double x = p1.x + (p2.x - p1.x)*(a1/ (a1 + a2)); 55 double y = p1.y + (p2.y - p1.y)*(a1 / (a1 + a2)); 56 printf("POINT %.2f %.2f\n", x, y); 57 } 58 59 void init() { 60 scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y); 61 s1 = seg(p1, p2), s2 = seg(p3, p4); 62 getcross(); 63 } 64 65 int main() { 66 int kase; 67 scanf("%d", &kase); 68 printf("INTERSECTING LINES OUTPUT\n"); 69 while (kase--) 70 init(); 71 printf("END OF OUTPUT\n"); 72 return 0; 73 }
是否共线可用叉乘做。
原文地址:https://www.cnblogs.com/yalphait/p/10134736.html