Interecting Lines
题目来源
OpenJ_Bailian 1269
POJ 1269
UVA 378
UVAlive 5425
ZOJ 1280
题面
描述
我们都知道平面上的一对由线上两点定义的直线有三种关系:
1. 平行无交点
2. 两条线重叠
3. 在一个点上相交
在这个问题中你需要使用你的代数知识编写一个程序决定两条线的怎样的关系、在何处相交。(啊啊啊翻译苦手)
你的程序将要重复地读入定义在xy平面上定义两条直线的四个点,并决定他们怎样 在何处相交。保证题目所给的数据合理并在-1000到1000之间。
输入
第一行包括整数N(在1 ~ 10之间)表明将给出多少对直线。接下来的
N行每一行包括8个整数,表示四个点在平面上的坐标x1 y1 x2 y2 x3 y3 x4 y4,表示穿过(x1,y1)、(x2,y2)的直线和穿过(x3,y3)、(x4,y4)的直线。保证(x1,y1)和(x2,y2)是不同的点,(x3,y3)和(x4,y4)同理。
输出
输出N+2行,第一行为“INTERSECTING LINES OUTPUT”。接下来每行输出输入中所给的两条直线的相交情况:none,line,还是point。如果存在交点那么再输出交点的坐标。最后一行输出“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
???
非常和蔼的模板题啊ww
敲的很开心2333
就是UVA的多组数据wa了几发_(:з」∠)_
(你个智障
代码
#include <cmath>
#include <cstdio>
#include <cstring>
const double eps=1e-10;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){};
};
typedef Point Vector;
int n;
Point P;
int dcmp(double x)
{
if (fabs(x)<eps)
return 0;
return x>0 ? 1:-1;
}
Vector operator - (Vector A,Vector B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double p)
{
return Vector (A.x*p,A.y*p);
}
Vector operator + (Vector A,Vector B)
{
return Vector(A.x+B.x,A.y+B.y);
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
Point interection(Point P,Vector v,Point Q,Vector w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
bool online(Vector P,Vector A,Vector B)
{
return dcmp(Cross(P-A,P-B))==0;
}
int interect(Point A1,Point A2,Point B1,Point B2)
{
Vector v1=A1-A2,v2=B1-B2;
if (dcmp(Cross(v1,v2))==0)
{
if (online(A1,B1,B2))
return -1;
return 0;
}
P=interection(A1,v1,B1,v2);
return 1;
}
int main()
{
while (scanf("%d",&n)==1)
{
printf("INTERSECTING LINES OUTPUT\n");
for (int i=1;i<=n;i++)
{
double x1,x2,x3,x4,y1,y2,y3,y4;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
int k=interect(Point(x1,y1),Point(x2,y2),Point(x3,y3),Point(x4,y4));
if (k==0)
printf("NONE\n");
if (k==-1)
printf("LINE\n");
if (k==1)
printf("POINT %.2lf %.2lf\n",P.x,P.y);
}
printf("END OF OUTPUT\n");
}
}
本文介绍了一道关于判断二维平面上两条直线相交情况的经典算法题。通过代数方法确定两条直线之间的关系——平行无交点、重叠或在一点相交,并提供了完整的C++实现代码。
2505

被折叠的 条评论
为什么被折叠?



