POJ 1269 直线相交(水题请绕行~~~~~)

题目链接http://poj.org/problem?id=1269


题解:贴上来 只是想说 水题也是有尊严的0.0  好吧我下次不贴了……


#include<cstdio>
#include<cmath>
//定义点
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名

//向量+向量=向量    点+向量=点
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x, A.y+B.y);}

//点-点=向量
Vector operator - (Point A, Point B) {return Vector(A.x-B.x, A.y-B.y);}

//向量*数=向量
Vector operator * (Vector A, double p) {return Vector(A.x*p, A.y*p);}

//向量/数=向量
Vector operator / (Vector A, double p) {return Vector(A.x/p, A.y/p);}

//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B)
{
	return A.x*B.y - A.y*B.x;
}

//两直线交点
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
{
	Vector u = P - Q;
	double t = Cross(w, u) / Cross(v, w);
	return P+v*t;
}


int main ()
{
    printf("INTERSECTING LINES OUTPUT\n");
    int T;
    scanf("%d", &T);
    while(T--)
    {

        Point A, B, C, D;
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y);

        if(A.x == B.x && C.x == D.x)  //无斜率 垂直于X轴
        {
            if(A.x == C.x)   //同一条直线
                printf("LINE\n");

            else
                printf("NONE\n");
            continue;
        }

        double k1 = (B.y-A.y)/(B.x-A.x), k2 = (D.y-C.y)/(D.x-C.x);

        if(k1 == k2) //斜率相同
        {
            if(D.y == k1*D.x + A.y - k1*A.x) //在同一条直线上
               printf("LINE\n");
            else
               printf("NONE\n");
            continue;
        }

        Point ans = GetLineIntersection(A, A-B, C, C-D);
        printf("POINT %.2lf %.2lf\n", ans.x, ans.y);

    }
     printf("END OF OUTPUT\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值