poj 1269 Intersecting Lines

来源:互联网 发布:数控编程培训靠谱吗 编辑:程序博客网 时间:2024/06/17 06:27

题目链接:http://poj.org/problem?id=1269
题意:给你两条线段的两个端点,让你判断他们的情况,总共有三种情况,共线(NONE),平行(LINE),和相交(Point)
解析:共线和平行直接用斜率和叉乘来判断,剩下的就是求直线交点(推直线交点的时候心态别崩)

#include <cmath>#include <algorithm>#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <stack>using namespace std;const int maxn = 105+100;const double eps = 1e-8;struct point{    double x;    double y;    point() {}    point(double _x,double _y)    {        x = _x;        y = _y;    }};struct line{    point a;    point b;};double x_mul(point p0,point p1,point p2){    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}bool judge1(line l1,line l2){    //判断是否平行    double t1 = (l2.a.x-l2.b.x)*(l1.a.y-l1.b.y);    double t2 = (l2.a.y-l2.b.y)*(l1.a.x-l1.b.x);    return t1==t2;}//是否相交//bool judge2(line l1,line l2)//{//    bool flag1 = min(l1.a.x,l1.b.x)<=max(l2.a.x,l2.b.x)&&//                min(l1.a.y,l1.b.y)<=max(l2.a.y,l2.b.y)&&//                min(l2.a.x,l2.b.x)<=max(l1.a.x,l1.b.x)&&//                min(l2.a.y,l2.b.y)<=max(l1.a.y,l1.b.y);//    bool flag2 = (x_mul(l1.a,l2.a,l2.b)*x_mul(l1.b,l2.a,l2.b)<=0)&&//                (x_mul(l2.a,l1.a,l1.b)*x_mul(l2.b,l1.a,l1.b)<=0);//    return flag1&&flag2;//}point cross(line l1,line l2){    point res;    double t1 = x_mul(l1.a,l1.b,l2.a);    double t2 = x_mul(l1.b,l1.a,l2.b);    res.x = (l2.b.x*t1+l2.a.x*t2)/(t1+t2);    res.y = (l2.b.y*t1+l2.a.y*t2)/(t1+t2);    return res;}int main(void){    int n;    cin>>n;    puts("INTERSECTING LINES OUTPUT");    while(n--)    {        line l1,l2;        scanf("%lf %lf %lf %lf",&l1.a.x,&l1.a.y,&l1.b.x,&l1.b.y);        scanf("%lf %lf %lf %lf",&l2.a.x,&l2.a.y,&l2.b.x,&l2.b.y);        if(judge1(l1,l2))        {            if(x_mul(l1.a,l2.a,l2.b))                puts("NONE");            else                puts("LINE");        }        else        {            point ans = cross(l1,l2);            printf("POINT %.2f %.2f\n",ans.x,ans.y);        }    }    puts("END OF OUTPUT");    return 0;}
0 0