POJ 1269 两线的关系

Intersecting Lines

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10967   Accepted: 4930

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".

Sample Input

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

Sample Output

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

题意:

给两条线,求他们之间的关系。关系有3种:相交、共线、平行。

思路:

模拟手算就行了,注意直线可能没有斜率,用G++提交时用%.2f而不能用%.2lf,否则判错。

代码:

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <stdio.h>
 5 #include <math.h>
 6 using namespace std;
 7 #define pi acos(-1)
 8
 9
10 main()
11 {
12      int t, t1;
13      double x1, y1, x2, y2, x3, y3, x4, y4;
14      double k1, b1, k2, b2, x, y;
15      cin>>t;
16      t1=t;
17      while(t--){
18          if(t1==t+1){
19              printf("INTERSECTING LINES OUTPUT\n");
20          }
21          scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
22          if(x1==x2&&x3==x4){              //当两条线都没有斜率时
23              if(x1==x3) printf("LINE\n");
24              else printf("NONE\n");
25          }
26          else if(x1==x2){             //第一条线没斜率
27              k1=(y4-y3)/(x4-x3);
28              b1=y4-k1*x4;
29              printf("POINT %.2f %.2f\n",x1,k1*x1+b1);
30          }
31          else if(x3==x4){              //第二条线没斜率
32              k1=(y2-y1)/(x2-x1);
33              b1=y2-k1*x1;
34              printf("POINT %.2f %.2f\n",x3,k1*x3+b1);
35          }
36          else{                        //都有斜率
37              k1=(y2-y1)/(x2-x1);
38              k2=(y4-y3)/(x4-x3);
39              b1=y2-k1*x2;
40              b2=y4-k2*x4;
41              if(k1==k2&&b1==b2){
42                  printf("LINE\n");
43              }
44              else if(k1==k2){
45                  printf("NONE\n");
46              }
47              else{
48                  x=(b1-b2)/(k2-k1);
49                  y=k1*x+b1;
50                  printf("POINT %.2f %.2f\n",x,y);
51              }
52          }
53          if(t==0) printf("END OF OUTPUT\n");
54      }
55 }

POJ 1269 两线的关系

时间: 2024-10-10 22:26:56

POJ 1269 两线的关系的相关文章

判断两条直线的位置关系 POJ 1269 Intersecting Lines

两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, p2, p3, p4,直线L1,L2分别穿过前两个和后两个点.来判断直线L1和L2的关系 这三种关系一个一个来看: 1. 共线. 如果两条直线共线的话,那么另外一条直线上的点一定在这一条直线上.所以p3在p1p2上,所以用get_direction(p1, p2, p3)来判断p3相对于p1p2的关

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

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——叉积求直线交点坐标

题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么是叉积:https://blog.csdn.net/sunbobosun56801/article/details/78980467        其二维:https://blog.csdn.net/qq_38182397/article/details/80508303计算交点:    方法1:面

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 计算几何

1 /** 2 判断直线位置关系 3 **/ 4 #include <iostream> 5 #include <cmath> 6 #include <cstdio> 7 using namespace std; 8 struct point { 9 double x,y; 10 point(double x=0,double y=0):x(x),y(y){} 11 }; 12 13 typedef point Vector; 14 15 Vector operator

poj 1182 食物链 (带关系的并查集)

  食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类.

poj 1269 线段与线段相交

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13605   Accepted: 6049 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 3608 两凸包最近距离 旋转卡壳

Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2364   Special Judge Description Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory