poj 1269 Intersecting Lines
程序员文章站
2022-04-02 17:20:44
...
Intersecting Lines
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 16275 | Accepted: 7023 |
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.
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.先判断两条直线是否重合,方法是两次从四个点选三个点做叉乘看结果是否等于0。
2.判断是否平行,判断斜率是否相等即可,不能做除法,除数不能为0
3.计算交点坐标,没什么好方法,在草稿纸上算,= =
这里给出最终化简后的方法,A,B,C,D表示四个点
double a=B.x-A.x;
double b=B.y-A.y;
double c=D.x-C.x;
double d=D.y-C.y;
double e=A.x*B.y-B.x*A.y;
double f=C.x*D.y-D.x*C.y;
double x=(a*f-c*e)/(a*d-b*c);
double y=(b*f-d*e)/(a*d-b*c);
化简过程较复杂,算了一页草稿纸,这里就不贴出来了,自己可以去试一下知道这些代码就简单了
#pragma comment (linker,"/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
#define pi acos(-1.0)
#define eps 1e-8
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define epppp tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define mod 1000000007
const int inf=0x3f3f3f3f;
int t;
struct Point
{
double x,y;
}A,B,C,D;
double cross(Point a,Point b,Point c)//计算叉乘
{
return (a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
}
void solve()
{
if(fabs(cross(A,B,C))<=eps&&fabs(cross(A,B,D))<=eps)puts("LINE");//先判断重合再判断平行,顺序不能搞反
else if((B.y-A.y)*(D.x-C.x)==(D.y-C.y)*(B.x-A.x))puts("NONE");//判断平行,将除转换成乘法
else
{
double a=B.x-A.x;
double b=B.y-A.y;
double c=D.x-C.x;
double d=D.y-C.y;
double e=A.x*B.y-B.x*A.y;
double f=C.x*D.y-D.x*C.y;
double x=(a*f-c*e)/(a*d-b*c);
double y=(b*f-d*e)/(a*d-b*c);
pf("POINT %.2f %.2f\n",x,y);
}
}
int main()
{
sf("%d",&t);
puts("INTERSECTING LINES OUTPUT");
for1(i,t)
{
sf("%lf%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&D.x,&D.y);
solve();
}
puts("END OF OUTPUT");
return 0;
}