poj 1269 Intersecting Lines(直线相交)

本文介绍了一个用于确定平面上两条线是否相交、相交方式及相交位置的算法。该算法能够处理平行、重合及点交情况,并通过具体实例展示了如何使用C++实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8637 Accepted: 3915

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

Source

 
 
 
 
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 using namespace std;
 5 #define eps 1e-8
 6 #define oo 100000000
 7 #define pi acos(-1) 
 8 struct point
 9 {
10     double x,y;
11      point(double _x = 0.0,double _y = 0.0)
12     {
13         x =_x;
14         y =_y;
15     }
16     point operator -(const point &b)const
17     {
18         return point(x - b.x, y - b.y);
19     }
20     point operator +(const point &b)const
21     {
22         return point(x +b.x, y + b.y);
23     }
24     double operator ^(const point &b)const
25     {
26         return x*b.y - y*b.x;
27     }
28     double operator *(const point &b)const
29     {
30         return x*b.x + y*b.y;
31     }
32 }p[10];
33 
34 double dis(point a,point b)//两点之间的距离 
35 {
36      return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
37 }
38 
39 int dcmp(double a)//判断一个double型的符号 
40 {
41     if(fabs(a)<eps)return 0;
42     if(a>0)return 1;
43     else return -1; 
44 }
45 
46 int isxiangjiao(point a,point b,point c,point d)//判断直线相交,重合,平行!!! 
47 {
48     point aa,bb,cc,dd;
49     aa=b-a;
50     bb=d-c;
51     if(dcmp(aa^bb)!=0)return 1;//相交 
52     else
53     {
54         aa=a-d;
55         bb=b-c;
56         cc=a-c;
57         dd=b-d; 
58         if(dcmp(aa^bb)!=0||dcmp(cc^dd)!=0)return 2;//平行 
59         else return 3;//重合 
60     }
61 }
62 
63 point getjiaodian(point p,point v,point q,point w)//参数方程,v,w都为方向向量,p,q,为两直线上的点,求交点 
64 {
65     point u;
66     u=p-q;
67     double t=(w^u)/(v^w);
68     v.x=t*v.x;v.y=t*v.y;
69     return p+v;
70 }
71 
72 int main()
73 {
74     int T,i,j;
75     scanf("%d",&T);
76     printf("INTERSECTING LINES OUTPUT\n");
77     while(T--)
78     {
79         for(i=1;i<=4;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
80     
81         if(isxiangjiao(p[1],p[2],p[3],p[4])==1)
82         {
83             point ans,v,w,q;
84             v=p[2]-p[1];
85             w=p[4]-p[3];
86              ans=getjiaodian(p[1],v,p[3],w);
87              printf("POINT %.2f %.2f\n",ans.x,ans.y);
88         }
89         
90         if(isxiangjiao(p[1],p[2],p[3],p[4])==2)printf("NONE\n");//平行 
91         
92         if(isxiangjiao(p[1],p[2],p[3],p[4])==3)printf("LINE\n");//重合    
93     }
94      printf("END OF OUTPUT\n");
95     return 0;
96 }
View Code

 

转载于:https://www.cnblogs.com/skykill/p/3235064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值