18.12.17 POJ 1269 Intersecting Lines

描述

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

时间: 2024-11-08 20:46:21

18.12.17 POJ 1269 Intersecting Lines的相关文章

poj 1269 Intersecting Lines(判相交交点与平行)

http://poj.org/problem?id=1269 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10379   Accepted: 4651 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

POJ 1269 Intersecting Lines(线段相交,水题)

Intersecting Lines 大意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:线段相交判断.求交点的水题,没什么好说的. struct Point{ double x, y; } ; struct Line{ Point a, b; } A, B; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x); } bool

POJ 1269 Intersecting Lines(判断直线相交)

题目地址:POJ 1269 直接套模板就可以了...实在不想自己写模板了...写的又臭又长....不过这题需要注意的是要先判断是否有直线垂直X轴的情况. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h>

POJ 1269 - Intersecting Lines 直线与直线相交

题意:    判断直线间位置关系: 相交,平行,重合 1 include <iostream> 2 #include <cstdio> 3 using namespace std; 4 struct Point 5 { 6 int x , y; 7 Point(int a = 0, int b = 0) :x(a), y(b) {} 8 }; 9 struct Line 10 { 11 Point s, e; 12 int a, b, c;//a>=0 13 Line() {

poj 1269 Intersecting Lines (计算几何)

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12863   Accepted: 5712 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

poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12421   Accepted: 5548 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

POJ 1269 Intersecting Lines 直线相交判断

D - Intersecting Lines Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1269 Appoint description:  System Crawler  (2016-05-08) Description We all know that a pair of distinct points on a plane d

POJ 1269 - Intersecting Lines - [平面几何模板题]

题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K 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 b

POJ 1269 Intersecting Lines (判断直线位置关系)

题目链接:POJ 1269 Problem 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 becau