poj 1269 Intersecting Lines 线段相交 平行 重合

利用斜率;
斜率相同 在判断是否等同的 如果不是则平行;
斜率不同 求交点;

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps = 1e-9;//注意精度;
const double inf = 1e10;

double x1, y, x2, y2, x3, y3, x4, y4;

bool dx(double x1, double x2)
{
    return abs(x1-x2) < eps;//两点相同;
}

double xl(double x1, double y1, double x2, double y2)//求斜率;
{
    if(dx(x1, x2)) return inf;//平行y轴 无斜率;
    return (y1-y2)/(x1-x2);
}

int main()
{
    int t;
    cout << "INTERSECTING LINES OUTPUT" << endl;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1, &y, &x2, &y2, &x3, &y3, &x4, &y4);
        double k1 = xl(x1, y, x2, y2), k2 = xl(x3, y3, x4, y4);
        if(dx(k1, k2))
        {
            if((dx(x1, x2) && dx(x1, x3)) ||(dx((y-k1*x1), (y3-k2*x3)))) cout << "LINE" << endl;
            else cout << "NONE" << endl;
        }
        else
        {
            double b1 = y-k1*x1, b2 = y3-k2*x3;
            printf("POINT %.2lf %.2lf\n", (b1-b2)/(k2-k1), k1*(b1-b2)/(k2-k1)+b1);
        }
    }
    cout << "END OF OUTPUT" << endl;
    return 0;
}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页