• Intersecting Lines


    分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了....
     
    代码如下:
    ======================================================================================================================================
    #include<math.h>
    #include<algorithm>
    #include<stdio.h>
    using namespace std;
    
    const int MAXN = 107;
    const double EPS = 1e-8;
    
    struct point
    {///定义点
        double x, y;
    
        point(double x=0, double y=0):x(x),y(y){}
        point operator - (const point &t) const{
            return point(x-t.x, y-t.y);
        }
        double operator * (const point &t) const{
            return x * t.y - y * t.x;
        }
    };
    struct segment
    {
        point A, B;
        double a, b, c;
    
        void InIt(point t1, point t2)
        {
            A = t1, B = t2;
            a = B.y - A.y;
            b = A.x - B.x;
            c =  A.x*B.y-B.x*A.y;
        }
    };
    
    bool Parallel(segment t1, segment t2)
    {///是否平行
        return fabs((t1.A-t1.B)*(t2.A-t2.B)) < EPS;
    }
    bool Collineation(segment t1, segment t2)
    {///是否共线
    
        return fabs((t1.A-t1.B)*(t2.A-t1.B)) < EPS  && fabs((t1.A-t1.B)*(t2.B-t1.B)) < EPS;
    }
    point CrossPoint(segment t1, segment t2)
    {
        point t;
    
        t.x = (t1.c*t2.b-t2.c*t1.b) / (t1.a*t2.b-t2.a*t1.b);
        t.y = (t1.c*t2.a-t2.c*t1.a) / (t1.b*t2.a-t2.b*t1.a);
    
        return t;
    }
    
    int main()
    {
        int N;
    
        while(scanf("%d", &N) != EOF)
        {
            segment p1, p2;
            point A, B;
    
            printf("INTERSECTING LINES OUTPUT
    ");
            while(N--)
            {
                scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);p1.InIt(A, B);
                scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);p2.InIt(A, B);
    
                if(Collineation(p1, p2) == true)
                    printf("LINE
    ");
                else if(Parallel(p1, p2) == true)
                    printf("NONE
    ");
                else
                {
                    point ans = CrossPoint(p1, p2);
                    printf("POINT %.2f %.2f
    ", ans.x, ans.y);
                }
            }
            printf("END OF OUTPUT
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    html笔记
    Git入门学习总结
    使用OpenSSH远程管理Linux服务器
    Linux 网卡驱动的安装
    vi的使用
    Linux下常用的数据恢复工具
    网络文件系统(NFS)的使用
    文件系统管理
    磁盘存储管理
    用户权限管理
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4789702.html
Copyright © 2020-2023  润新知