题目大意:
给定n对直线,求判断直线间的关系:平行、重合、相交,如果相交则输出两条直线的交点
计算几何模板题,只要注意精度,判断平行和重合是不要用==0,要写一个dcmp,当绝对值小于等于1e-6时就可以判断是0了。
附上AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
class Point
{
public:
double x, y;
Point(double x, double y):x(x), y(y) { }
};
typedef Point Vector;
Vector operator + (const Vector &a, const Vector &b)
{
return Vector(a.x+b.x, a.y+b.y);
}
Vector operator - (const Point &a, const Point &b)
{
return Vector(a.x-b.x, a.y-b.y);
}
Vector operator * (const Vector &a, const double &b)
{
return Vector(a.x*b, a.y*b);
}
Vector operator / (const Vector &a, const double &b)
{
return Vector(a.x/b, a.y/b);
}
double Cross(const Vector &a, const Vector &b)
{
return a.x*b.y - a.y*b.x;
}
double Dot(const Vector &a, const Vector &b)
{
return a.x*b.x + a.y*b.y;
}
double Length(const Vector &a)
{
return sqrt(Dot(a, a));
}
int dcmp(double x)
{
if (fabs(x) <= eps)
return 0;
return x > 0 ? 1 : -1;
}
Point LineIntersection(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()
{
#ifndef ONLINE_JUDGE
freopen("inter.in", "r", stdin);
#endif
int t;
double a, b, c, d, e, f, g, h;
scanf("%d", &t);
printf("INTERSECTING LINES OUTPUT\n");
while (t--) {
scanf("%lf %lf %lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f, &g, &h);
Point p1(a, b), p2(c, d), p3(e, f), p4(g, h);
Vector v1 = p2-p1, v2 = p4-p3;
if (dcmp(Cross(v1, p3-p1)) == 0 && dcmp(Cross(p3-p1, p4-p1)) == 0) {
printf("LINE\n");
continue;
}
double tmp = Cross(v1, v2);
if (dcmp(tmp) == 0) {
printf("NONE\n");
continue;
}
Point ins = LineIntersection(p1, v1, p3, v2);
printf("POINT %.2f %.2f\n", ins.x, ins.y);
}
printf("END OF OUTPUT");
return 0;
}