zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    站立会议(二)
    站立会议(一)
    买书优惠问题
    软件的NABCD----安装部分
    你的灯亮着吗读书笔记(一)
    软件工程概论---环状二维数组最大子数组和
    梦断代码读书笔记(三)
    梦断代码读书笔记(二)
    课程作业3.10
    软件工程作业提交3.06
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4789702.html
Copyright © 2011-2022 走看看