This documentation is automatically generated by online-judge-tools/verification-helper
#include "geo/convex_polygon_union_area.hpp"#include "geo/base.hpp"
// それぞれが ccw order の strict convex polygon
// O(N^2logN)
// 6角形784個: 840ms
// https://ac.nowcoder.com/acm/contest/133876/K
template <typename Re>
Re convex_polygon_union_area(vvc<Point<ll>> dat) {
using P = Point<ll>;
/*
線分の寄与に分解
境界に現れるところを足す
各辺について、他の多角形の内部にあるところを消すという感じ
タイブレイクを慎重にやる
同じ辺:小さい番号の辺を優先することにする
*/
// strict ?
for (auto &X : dat) {
int n = len(X);
if (n <= 2) continue;
FOR(i, n) {
P A = X[i], B = X[(i + 1) % n], C = X[(i + 2) % n];
assert(ccw(A, B, C) == 1);
}
}
ll N = len(dat);
Re ANS = 0.0;
FOR(i, N) {
FOR(k, len(dat[i])) {
P A = dat[i][k];
P B = dat[i][(k + 1) % len(dat[i])];
vc<pair<Re, Re>> ng;
FOR(j, N) {
if (j == i) continue;
Re L = 0.0, R = 1.0;
FOR(kk, len(dat[j])) {
P C = dat[j][kk];
P D = dat[j][(kk + 1) % len(dat[j])];
P norm = (D - C).rot90(true);
ll d = norm.dot(C);
// left of CD: norm v > d
if ((A - B).det(C - D) == 0) {
// 平行です
ll sgn = A.dot(norm) - d;
if (sgn > 0) {
continue;
}
elif (sgn == 0) {
if ((B - A).dot(D - C) < 0) {
// 逆向き
continue;
}
// 同じ向き
// 小さい番号からくる部分を消す
if (j < i) {
Re c = Re((C - A).dot(B - A)) / Re((B - A).dot(B - A));
Re d = Re((D - A).dot(B - A)) / Re((B - A).dot(B - A));
chmax(L, c), chmin(R, d);
// [c,d)]
} else {
// 一切消さないようにする
L = 1.0, R = 0.0;
}
}
elif (sgn < 0) {
// 内部にならない
L = 1.0, R = 0.0;
break;
}
else {
assert(0);
}
} else {
// 平行ではない
// 0 以上になっている部分が切り取られる
ll a = A.dot(norm) - d;
ll b = B.dot(norm) - d;
Re t = Re(0 - a) / Re(b - a);
if (a < b) {
chmax(L, t);
}
elif (a > b) { chmin(R, t); }
else {
assert(0);
}
}
}
if (L < R) ng.eb(L, R);
}
vc<pair<Re, int>> event;
for (auto &[a, b] : ng) {
if (a >= b) continue;
event.eb(a, 1);
event.eb(b, -1);
}
Re prv = 0.0;
int cnt = 0;
sort(all(event));
Re ans = 0.0;
for (auto &[x, t] : event) {
Re dx = x - prv;
prv = x;
if (cnt == 0) ans += dx;
cnt += t;
}
Re dx = 1.0 - prv;
ans += dx;
ll det = A.det(B);
ANS += det * ans;
}
}
ANS /= 2;
return ANS;
}#line 2 "geo/base.hpp"
template <typename T>
struct Point {
T x, y;
Point() : x(0), y(0) {}
template <typename A, typename B>
Point(A x, B y) : x(x), y(y) {}
template <typename A, typename B>
Point(pair<A, B> p) : x(p.fi), y(p.se) {}
template <typename U>
Point(Point<U> p) : x(p.x), y(p.y) {}
Point operator+=(const Point p) {
x += p.x, y += p.y;
return *this;
}
Point operator-=(const Point p) {
x -= p.x, y -= p.y;
return *this;
}
Point operator+(Point p) const { return {x + p.x, y + p.y}; }
Point operator-(Point p) const { return {x - p.x, y - p.y}; }
bool operator==(Point p) const { return x == p.x && y == p.y; }
bool operator!=(Point p) const { return x != p.x || y != p.y; }
Point operator-() const { return {-x, -y}; }
Point operator*(T t) const { return {x * t, y * t}; }
Point operator/(T t) const { return {x / t, y / t}; }
bool operator<(Point p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
T dot(const Point& other) const { return x * other.x + y * other.y; }
T det(const Point& other) const { return x * other.y - y * other.x; }
double norm() { return sqrtl(x * x + y * y); }
double angle() { return atan2(y, x); }
Point rotate(double theta) {
static_assert(!is_integral<T>::value);
double c = cos(theta), s = sin(theta);
return Point{c * x - s * y, s * x + c * y};
}
Point rot90(bool ccw) { return (ccw ? Point{-y, x} : Point{y, -x}); }
};
#ifdef FASTIO
template <typename T>
void rd(Point<T>& p) {
fastio::rd(p.x), fastio::rd(p.y);
}
template <typename T>
void wt(Point<T>& p) {
fastio::wt(p.x);
fastio::wt(' ');
fastio::wt(p.y);
}
#endif
// A -> B -> C と進むときに、左に曲がるならば +1、右に曲がるならば -1
template <typename T>
int ccw(Point<T> A, Point<T> B, Point<T> C) {
T x = (B - A).det(C - A);
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
template <typename REAL, typename T, typename U>
REAL dist(Point<T> A, Point<U> B) {
REAL dx = REAL(A.x) - REAL(B.x);
REAL dy = REAL(A.y) - REAL(B.y);
return sqrt(dx * dx + dy * dy);
}
// ax+by+c
template <typename T>
struct Line {
T a, b, c;
Line(T a, T b, T c) : a(a), b(b), c(c) {}
Line(Point<T> A, Point<T> B) {
a = A.y - B.y, b = B.x - A.x, c = A.x * B.y - A.y * B.x;
}
Line(T x1, T y1, T x2, T y2) : Line(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <typename U>
U eval(Point<U> P) {
return U(a) * P.x + U(b) * P.y + U(c);
}
template <typename U>
T eval(U x, U y) {
return a * x + b * y + c;
}
// 同じ直線が同じ a,b,c で表現されるようにする
void normalize() {
static_assert(is_same_v<T, int> || is_same_v<T, long long>);
T g = gcd(gcd(abs(a), abs(b)), abs(c));
a /= g, b /= g, c /= g;
if (b < 0) {
a = -a, b = -b, c = -c;
}
if (b == 0 && a < 0) {
a = -a, b = -b, c = -c;
}
}
bool is_parallel(Line other) { return a * other.b - b * other.a == 0; }
bool is_orthogonal(Line other) { return a * other.a + b * other.b == 0; }
bool is_same(Line other) {
if (a * other.b != b * other.a) return 0;
if (a * other.c != c * other.a) return 0;
if (b * other.c != c * other.b) return 0;
return 1;
}
};
template <typename T>
struct Segment {
Point<T> A, B;
Segment(Point<T> A, Point<T> B) : A(A), B(B) {}
Segment(T x1, T y1, T x2, T y2)
: Segment(Point<T>(x1, y1), Point<T>(x2, y2)) {}
bool contain(Point<T> C) {
T det = (C - A).det(B - A);
if (det != 0) return 0;
return (C - A).dot(B - A) >= 0 && (C - B).dot(A - B) >= 0;
}
Line<T> to_Line() { return Line(A, B); }
};
template <typename REAL>
struct Circle {
Point<REAL> O;
REAL r;
Circle() {}
Circle(Point<REAL> O, REAL r) : O(O), r(r) {}
Circle(REAL x, REAL y, REAL r) : O(x, y), r(r) {}
template <typename T>
bool contain(Point<T> p) {
REAL dx = p.x - O.x, dy = p.y - O.y;
return dx * dx + dy * dy <= r * r;
}
};
#line 2 "geo/convex_polygon_union_area.hpp"
// それぞれが ccw order の strict convex polygon
// O(N^2logN)
// 6角形784個: 840ms
// https://ac.nowcoder.com/acm/contest/133876/K
template <typename Re>
Re convex_polygon_union_area(vvc<Point<ll>> dat) {
using P = Point<ll>;
/*
線分の寄与に分解
境界に現れるところを足す
各辺について、他の多角形の内部にあるところを消すという感じ
タイブレイクを慎重にやる
同じ辺:小さい番号の辺を優先することにする
*/
// strict ?
for (auto &X : dat) {
int n = len(X);
if (n <= 2) continue;
FOR(i, n) {
P A = X[i], B = X[(i + 1) % n], C = X[(i + 2) % n];
assert(ccw(A, B, C) == 1);
}
}
ll N = len(dat);
Re ANS = 0.0;
FOR(i, N) {
FOR(k, len(dat[i])) {
P A = dat[i][k];
P B = dat[i][(k + 1) % len(dat[i])];
vc<pair<Re, Re>> ng;
FOR(j, N) {
if (j == i) continue;
Re L = 0.0, R = 1.0;
FOR(kk, len(dat[j])) {
P C = dat[j][kk];
P D = dat[j][(kk + 1) % len(dat[j])];
P norm = (D - C).rot90(true);
ll d = norm.dot(C);
// left of CD: norm v > d
if ((A - B).det(C - D) == 0) {
// 平行です
ll sgn = A.dot(norm) - d;
if (sgn > 0) {
continue;
}
elif (sgn == 0) {
if ((B - A).dot(D - C) < 0) {
// 逆向き
continue;
}
// 同じ向き
// 小さい番号からくる部分を消す
if (j < i) {
Re c = Re((C - A).dot(B - A)) / Re((B - A).dot(B - A));
Re d = Re((D - A).dot(B - A)) / Re((B - A).dot(B - A));
chmax(L, c), chmin(R, d);
// [c,d)]
} else {
// 一切消さないようにする
L = 1.0, R = 0.0;
}
}
elif (sgn < 0) {
// 内部にならない
L = 1.0, R = 0.0;
break;
}
else {
assert(0);
}
} else {
// 平行ではない
// 0 以上になっている部分が切り取られる
ll a = A.dot(norm) - d;
ll b = B.dot(norm) - d;
Re t = Re(0 - a) / Re(b - a);
if (a < b) {
chmax(L, t);
}
elif (a > b) { chmin(R, t); }
else {
assert(0);
}
}
}
if (L < R) ng.eb(L, R);
}
vc<pair<Re, int>> event;
for (auto &[a, b] : ng) {
if (a >= b) continue;
event.eb(a, 1);
event.eb(b, -1);
}
Re prv = 0.0;
int cnt = 0;
sort(all(event));
Re ans = 0.0;
for (auto &[x, t] : event) {
Re dx = x - prv;
prv = x;
if (cnt == 0) ans += dx;
cnt += t;
}
Re dx = 1.0 - prv;
ans += dx;
ll det = A.det(B);
ANS += det * ans;
}
}
ANS /= 2;
return ANS;
}