#include <bits/stdc++.h> using namespace std; const int maxn = 1500 + 10; const double eps = 1e-9; const double PI = acos(-1); int dcmp(double x) { if (fabs(x) < eps) return 0; return x > 0 ? 1 : -1; } struct Point { double x, y; Point (double a = 0, double b = 0): x(a), y(b) {} }; typedef Point Vector; typedef vector<Point> Polygon; Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); } Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); } Vector operator * (const Vector& a, double& b) { return Vector(a.x * b, a.y * b); } Vector operator / (const Vector& a, double& b) { return Vector(a.x / b, a.y / b); } bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); } bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } 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)); } double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; } double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); } struct Line { Point p; Vector v; double ang; Line() {} Line(Point a, Vector b): p(a), v(b) { ang = atan2(b.y, b.x); } bool operator < (const Line& L) const { return ang < L.ang; } Point point(double a) { return p + v * a; } }; double PolygonArea(vector<Point>& res, int m) { double area = 0; for (int i = 1; i < m - 1; i++) area += Cross(res[i] - res[0], res[i + 1] - res[0]); return area / 2; } //点p在有向直线L的左边(线上不算) bool OnLeft(Line L, Point P) { return Cross(L.v, P - L.p) > 0; } //求两直线的交点,前提交点一定存在 Point GetIntersection(Line a, Line b) { Vector u = a.p - b.p; double t = Cross(b.v, u) / Cross(a.v, b.v); return a.p + a.v * t; } //求半面交 int HalfplaneIntersection(vector<Line>& L, vector<Point>& poly) { int n = L.size(); sort(L.begin(), L.end()); int first = 0, rear = 0; vector<Point> p(n); vector<Line> q(n); q[first] = L[0]; for (int i = 1; i < n; i++) { while (first < rear && !OnLeft(L[i], p[rear - 1])) rear--; while (first < rear && !OnLeft(L[i], p[first])) first++; q[++rear] = L[i]; if (fabs(Cross(q[rear].v, q[rear - 1].v)) < eps) { rear--; if (OnLeft(q[rear], L[i].p)) q[rear] = L[i]; } if (first < rear) p[rear - 1] = GetIntersection(q[rear - 1], q[rear]); } while (first < rear && !OnLeft(q[first], p[rear - 1])) rear--; if (rear - first <= 1) return 0; p[rear] = GetIntersection(q[rear], q[first]); for (int i = first; i <= rear; i++) poly.push_back(p[i]); return poly.size(); } int n, T; Point P[maxn]; vector<Line>L; int main(int argc, char const *argv[]) { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = n - 1; i >= 0; i--) scanf("%lf%lf", &P[i].x, &P[i].y); L.clear(); for (int i = 0; i < n; i++) L.push_back(Line(P[i], P[(i + 1) % n] - P[i])); Polygon poly; int cnt = HalfplaneIntersection(L, poly); printf("%.2lf\n", PolygonArea(poly, cnt)); } return 0; }
纯净的半平面交模板题。