计算几何基础 POJ 1269 Intersecting Lines 【直线相交判断,求交点】

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=1e5+50;
const double eps=1e-8;
typedef long long ll;
struct point
{
    double x,y;
    point(double _X=0,double _Y=0)
    {
        x=_X;y=_Y;
    }
    point operator - (const point &oth)const
    {
        return point(x-oth.x,y-oth.y);
    }
        point operator + (const point &oth)const
    {
        return point(x+oth.x,y+oth.y);
    }
        point operator * (const double oth)const
    {
        return point(x*oth,y*oth);
    }
        point operator / (const double oth)const
    {
        return point(x/oth,y/oth);
    }
};
int dcmp(double x)
{
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}
double cross(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
point glt(point a,point a1,point b,point b1)
{
    point v= a1-a;
    point w=b1-b,u = a-b;
    double t = cross(w,u)/cross(v,w);
    return a+v*t;
}
int main()
{
    //freopen("d://duipai//data.txt","r",stdin);
    //freopen("d://duipai//out1.txt","w",stdout);
    int T;
    scanf("%d",&T);
    printf("INTERSECTING LINES OUTPUT\n");
    while(T--)
    {
        double x1,y1,x2,y2,x3,y3,x4,y4;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
        point a,b,c,d;
        a=point(x1,y1);
        b=point(x2,y2);
        c=point(x3,y3);
        d=point(x4,y4);

        if(cross(b-a,d-c))
        {
            point t=glt(a,b,c,d);
            printf("POINT %.2lf %.2lf\n",t.x,t.y);

        }
        else
        {
            if(!cross(b-a,c-b)) puts("LINE");
            else puts("NONE");
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}
全部评论

相关推荐

11-29 11:21
门头沟学院 Java
点赞 评论 收藏
分享
昨天 17:22
已编辑
西安交通大学 Java
华为 昇腾 ai软件开发 薪资20k x (14-16),职级13A,5%公积金,c/cpp
BLOOMING7:闭眼滴滴,华子给的又少又累
点赞 评论 收藏
分享
10-10 17:54
点赞 评论 收藏
分享
11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务