poj 1269 Intersecting Lines

74 篇文章 0 订阅
33 篇文章 0 订阅

题目链接:http://poj.org/problem?id=1269
题意:给你两条线段的两个端点,让你判断他们的情况,总共有三种情况,共线(LINE),平行(NONE),和相交(Point)
解析:共线和平行直接用斜率和叉乘来判断,剩下的就是求直线交点(推直线交点的时候心态别崩)

#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <stack>
using namespace std;
const int maxn = 105+100;
const double eps = 1e-8;
struct point
{
    double x;
    double y;
    point() {}
    point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
};
struct line
{
    point a;
    point b;
};
double x_mul(point p0,point p1,point p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool judge1(line l1,line l2)
{
    //判断是否平行
    double t1 = (l2.a.x-l2.b.x)*(l1.a.y-l1.b.y);
    double t2 = (l2.a.y-l2.b.y)*(l1.a.x-l1.b.x);
    return t1==t2;
}
//是否相交
//bool judge2(line l1,line l2)
//{
//    bool flag1 = min(l1.a.x,l1.b.x)<=max(l2.a.x,l2.b.x)&&
//                min(l1.a.y,l1.b.y)<=max(l2.a.y,l2.b.y)&&
//                min(l2.a.x,l2.b.x)<=max(l1.a.x,l1.b.x)&&
//                min(l2.a.y,l2.b.y)<=max(l1.a.y,l1.b.y);
//    bool flag2 = (x_mul(l1.a,l2.a,l2.b)*x_mul(l1.b,l2.a,l2.b)<=0)&&
//                (x_mul(l2.a,l1.a,l1.b)*x_mul(l2.b,l1.a,l1.b)<=0);
//    return flag1&&flag2;
//}
point cross(line l1,line l2)
{
    point res;
    double t1 = x_mul(l1.a,l1.b,l2.a);
    double t2 = x_mul(l1.b,l1.a,l2.b);
    res.x = (l2.b.x*t1+l2.a.x*t2)/(t1+t2);
    res.y = (l2.b.y*t1+l2.a.y*t2)/(t1+t2);
    return res;
}
int main(void)
{
    int n;
    cin>>n;
    puts("INTERSECTING LINES OUTPUT");
    while(n--)
    {
        line l1,l2;
        scanf("%lf %lf %lf %lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y);
        scanf("%lf %lf %lf %lf",&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);

        if(judge1(l1,l2))
        {
            if(x_mul(l1.a,l2.a,l2.b))
                puts("NONE");
            else
                puts("LINE");
        }
        else
        {
            point ans = cross(l1,l2);
            printf("POINT %.2f %.2f\n",ans.x,ans.y);
        }
    }
    puts("END OF OUTPUT");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值