给定两直线,判定相交,重合,或求出交点
验模板的题目
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int t;
struct Point {
double x, y;
Point() {}
Point(double x, double y) {
this->x = x;
this->y = y;
}
void read() {
scanf("%lf%lf", &x, &y);
}
};
typedef Point Vector;
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);
}
Vector operator / (Vector A, double p) {
return Vector(A.x / p, A.y / p);
}
const double eps = 1e-8;
int dcmp(double x) {
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
bool LineCoincide(Point p1, Point p2, Point p3) {
return dcmp(Cross(p2 - p1, p3 - p1)) == 0;
}
bool LineParallel(Point P, Vector v, Point Q, Vector w) {
return dcmp(v.x * w.y - v.y * w.x) == 0;
}
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() {
scanf("%d", &t);
printf("INTERSECTING LINES OUTPUT\n");
while (t--) {
Point p1, p2, p3, p4;
p1.read(); p2.read();
p3.read(); p4.read();
if (LineCoincide(p1, p2, p3) && LineCoincide(p1, p2, p4))
printf("LINE\n");
else if (LineParallel(p1, p2 - p1, p3, p4 - p3))
printf("NONE\n");
else {
Point ans = GetLineIntersection(p1, p2 - p1, p3, p4 - p3);
printf("POINT %.2f %.2f\n", ans.x, ans.y);
}
}
printf("END OF OUTPUT\n");
return 0;
}