题目链接: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;
}