您的位置:首页 > 其它

poj 1269 Intersecting Lines

2013-07-15 15:45 176 查看
这是很基础的计算几何的题目。

两直线判交,有交点就输出交点,没有交点共线或重合则输出相应的结果。

题目链接:http://poj.org/problem?id=1269

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
struct Point//
{
double x, y;
Point(){};
Point(double x, double y):x(x), y(y){};
}a[105], b[105];
//直线相交,平行返回0 重合返回1 相交返回2,有交点则存在p中
int jiaodian(Point a, Point b, Point c, Point d, Point &p)
{
double A1=b.y-a.y,A2=d.y-c.y,B1=a.x-b.x,B2=c.x-d.x;
double C1=b.y*(b.x-a.x)-b.x*(b.y-a.y),C2=d.y*(d.x-c.x)-d.x*(d.y-c.y);
if(A1*B2==B1*A2)//平行或重合
{
if(A2*C1==A1*C2&&B1*C2==B2*C1)
return 1;//重合
return 0;//平行
}
p.x=(B1*C2-B2*C1)/(B2*A1-B1*A2);
p.y=(A1*C2-A2*C1)/(A2*B1-A1*B2);
return 2;//相交
}

int main()
{
int Case;
Point a, b, c, d, p;
scanf("%d",&Case);
printf("INTERSECTING LINES OUTPUT\n");
while (Case--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y);
if (jiaodian(a, b, c, d, p) == 0)
printf("NONE\n");
else if (jiaodian(a, b, c, d, p) == 1)
printf("LINE\n");
else if (jiaodian(a, b, c, d, p) == 2)
printf("POINT %.2f %.2f\n", p.x, p.y);
}
printf("END OF OUTPUT\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: