水题,以前总结的模板还是很好用的。
#include <cstdio>
#include <cmath>
using namespace std; const double eps = 1e-; int dcmp(double x)
{
if(fabs(x) < eps) return ;
return x < ? - : ;
} struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
};
typedef Point Vector; Point read_point()
{
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
} Point operator + (const Point& A, const Point& B)
{ return Point(A.x+B.x, A.y+B.y); } Point operator - (const Point& A, const Point& B)
{ return Point(A.x-B.x, A.y-B.y); } Point operator * (const Point& A, double p)
{ return Point(A.x*p, A.y*p); } double Cross(const Point& A, const Point& B)
{ return A.x*B.y - A.y*B.x; } double Length(const Vector v)
{ return sqrt(v.x*v.x + v.y*v.y); } double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
} 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()
{
//freopen("in.txt", "r", stdin); int n;
scanf("%d", &n);
puts("INTERSECTING LINES OUTPUT");
Point A, B, C, D;
while(n--)
{
A = read_point(); B = read_point();
C = read_point(); D = read_point();
Vector v1 = B - A, v2 = D - C;
if(dcmp(Cross(v1, v2)) == )
{
if(dcmp(DistanceToLine(C, A, B)) == ) puts("LINE");
else puts("NONE"); }
else
{
Point ans = GetLineIntersection(A, v1, C, v2);
printf("POINT %.2f %.2f\n", ans.x, ans.y);
}
}
puts("END OF OUTPUT"); return ;
}
代码君