Intersecting Lines 【POJ - 1269】【直线的关系 相交、平行、重合】

题目链接

题目大意

给你两条直线,问你两个直线的位置,相交或者平行或者重合,相交的话输出相交的点

解题思路

求出两条直线的一般式
判断重合要用叉积判断

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
double eps=1e-8;

double cross(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double xx1=x2-x1,yy1=y2-y1;
    double xx2=x3-x1,yy2=y3-y1;
    return xx1*yy2-yy1*xx2;
}

int main()
{
    double x1,x2,y1,y2,x3,y3,x4,y4;
    int n;
    while(~scanf("%d",&n))
    {

    printf("INTERSECTING LINES OUTPUT\n");
    for(int i=1;i<=n;i++)
    {
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
        double A1=x2-x1,B1=-(y2-y1),C1=-y1*(x2-x1)+x1*(y2-y1);
        double A2=x4-x3,B2=-(y4-y3),C2=-y3*(x4-x3)+x3*(y4-y3);
        if((A1*B2-A2*B1)!=0)
        {
            printf("POINT ");
            double m=A1*B2-A2*B1;
            printf("%.2f %.2f\n",((C1*A2)-(C2*A1))/m,((C2*B1)-(C1*B2))/m);
        }
        else
        {
            if(fabs(cross(x3,y3,x1,y1,x2,y2))<eps&&fabs(cross(x4,y4,x1,y1,x2,y2))<eps)
                printf("LINE\n");
            else
                printf("NONE\n");
        }
    }
    printf("END OF OUTPUT\n");
    }
    return 0;
}

相关推荐
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页