Description
判断两直线的位置关系,如果相交则输出交点。
Solution
原来 \(poj\) 输出用 \(\text{%}lf\) 会错……
叉积求交点……哪个好人给我讲讲啊……QQ:1837479092
Code
#include <cstdio>
#include <cmath>
const double eps = 1e-8;
struct Point {
double x, y;
} a, b, c, d;
int dcmp(double x) {
return fabs(x) < eps ? 0 : x > 0 ? 1 : -1;
}
void clear(double &x) {
if (fabs(x) < eps) x = 0;
}
double cross(double x1, double y1, double x2, double y2) {
return x1 * y2 - x2 * y1;
}
Point cpoint() {
Point res;
double a1 = b.x - a.x, b1 = a.y - b.y, c1 = cross(a.x, a.y, b.x, b.y);
double a2 = d.x - c.x, b2 = c.y - d.y, c2 = cross(c.x, c.y, d.x, d.y);
res.x = cross(c1, c2, a1, a2) / cross(a1, a2, b1, b2);
res.y = cross(c1, c2, b1, b2) / cross(b1, b2, a1, a2);
clear(res.x), clear(res.y);
return res;
}
int main() {
puts("INTERSECTING LINES OUTPUT");
int n; scanf("%d", &n);
while (n--) {
scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
scanf("%lf%lf%lf%lf", &c.x, &c.y, &d.x, &d.y);
if (dcmp(cross(a.x - b.x, a.y - b.y, c.x - d.x, c.y - d.y)) == 0) {
if (dcmp(cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y)) == 0) puts("LINE");
else puts("NONE");
} else {
Point ans = cpoint();
printf("POINT %.2f %.2f\n", ans.x, ans.y);
}
}
puts("END OF OUTPUT");
return 0;
}