您的位置:首页 > 其它

POJ 2826 An easy problem?! 几何基础

2016-03-13 22:08 363 查看
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int T;
struct Point
{
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
//typedef vector<Point> Polygon;
Point read_point()
{
double X, Y;
scanf("%lf%lf", &X, &Y);
return Point(X, Y);
}
Vector operator +(Vector A, Vector B)//
{
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point 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);
}
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;
}
double Angle(Vector A, Vector B)//
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
double TriArea(Point A, Point B, Point C) //
{
return fabs(Cross(B - A, C - A)) / 2;
}
bool SegmentIntersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - a1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return
max(a1.x, a2.x) >= min(b1.x, b2.x) &&
max(b1.x, b2.x) >= min(a1.x, a2.x) &&
max(a1.y, a2.y) >= min(b1.y, b2.y) &&
max(b1.y, b2.y) >= min(a1.y, a2.y) &&
dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 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(int argc, char const *argv[])
{
scanf("%d", &T);
while (T--)
{
Point A1 = read_point(), A2 = read_point(), B1 = read_point(), B2 = read_point();
if (dcmp(A1.y - A2.y) < 0) swap(A1, A2); if (dcmp(B1.y - B2.y) < 0) swap(B1, B2);
if (dcmp(A1.y - A2.y) == 0 || dcmp(B1.y - B2.y) == 0 || SegmentIntersection(A1, A2, B1, B2) == false || SegmentIntersection(A1, Point(A1.x, 100000), B1, B2)  || SegmentIntersection(B1, Point(B1.x, 100000), A1, A2)) printf("0.00\n");
else
{
Point P = GetLineIntersection(A1, A2 - A1, B1, B2 - B1);
Point P1 = GetLineIntersection(A1, A2 - A1, Point(1000000, B1.y), B1 - Point(1000000, B1.y));
Point P2 = GetLineIntersection(B1, B2 - B1, Point(1000000, A1.y), A1 - Point(1000000, A1.y));
printf("%.2lf\n", min(TriArea(B1, P, P1), TriArea(A1, P, P2)) + eps);
}
}
return 0;
}


感觉就是一个字:妙!

写完就交,秒过,就一个字,爽~~~~!

虽然交了两次ce,还有两次输出了中间结果。。。。。

这是一道非常重要的几何基础题,考验对精度控制的理解(dcmp,EPS),对模板的熟悉程度。

还是建议采用刘汝佳的模板,他的模板才是经得起考验的,值得信赖的,虽然少了一些函数,自己修改就好啦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: