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;
}


發佈了121 篇原創文章 · 獲贊 10 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章