题目链接:http://poj.org/problem?id=1269
题面:
Description
Input
Output
Sample Input
5 0 0 4 4 0 4 4 0 5 0 7 6 1 0 2 3 5 0 7 6 3 -6 4 -3 2 0 2 27 1 5 18 5 0 3 4 0 1 2 2 5
Sample Output
#include
#include
#include
using namespace std;
struct Point {
double x, y;
Point (double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector;
int n;
Point A, B, C, D;
Vector operator + (Vector A, Vector B) {
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Vector A, Vector 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);
}
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x) {
if(fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B) {
return A.x * B.x + A.y * B.y;
}
double Length(Vector A) {
return sqrt(Dot(A, A));
}
double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
}
//求单位方向向量
Vector Unit_direction_vector(Vector w) {
return Vector(w.x / Length(w), w.y / Length(w));
}
//判断两直线是否不相交
bool isIntersection(Vector A, Vector B) {
return Unit_direction_vector(A) == Unit_direction_vector(B) || Unit_direction_vector(Vector(- A.x, - A.y)) == Unit_direction_vector(B);
}
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;
}
//判断两直线是否重合只要判断是否有公共点即可
bool OnLine(Point p, Point a1, Point a2) {
return dcmp(Cross(a1 - p, a2 - p)) == 0;
}
int main() {
while(~scanf("%d", &n)) {
printf("INTERSECTING LINES OUTPUT\n");
while(n--) {
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(isIntersection(A - B, C - D)) {
if(OnLine(A, C, D)) {
printf("LINE\n");
} else {
printf("NONE\n");
}
} else {
Point P = GetLineIntersection(A, A - B, C, C - D);
printf("POINT %.2f %.2f\n", P.x, P.y);
}
}
printf("END OF OUTPUT\n");
}
}