This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "geometry/common_area_cp.hpp"#pragma once
#include <algorithm>
#include <complex>
#include "base.hpp"
#include "cross_point_cl.hpp"
#include "distance_sp.hpp"
#include "is_intersect_cs.hpp"
#include "point.hpp"
#include "polygon.hpp"
namespace geometry {
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H
Real ca_cp_impl(const Circle& c, const Point& a, const Point& b) {
auto va = c.p - a, vb = c.p - b;
Real f = cross(va, vb), ret = 0;
if (sign(f) == 0) return ret;
if (sign(std::max(std::abs(va), std::abs(vb)) - c.r) <= 0) return f;
if (sign(distance_sp(Segment(a, b), c.p) - c.r) >= 0)
return std::norm(c.r) * std::arg(vb * std::conj(va));
auto tot = cross_point_cl(c, Line(a, b));
if (is_intersect_cs(c, Segment(a, b)) != 2 and
dot(a - tot[0], b - tot[0]) < 0) {
std::swap(tot[0], tot[1]);
}
tot.emplace(tot.begin(), a);
tot.emplace_back(b);
for (int i = 1; i < (int)tot.size(); i++) {
ret += ca_cp_impl(c, tot[i - 1], tot[i]);
}
return ret;
}
Real common_area_cp(const Circle& c, const Polygon& p) {
if (p.size() < 3) return 0;
Real A = 0;
for (int i = 0; i < p.size(); i++) {
A += ca_cp_impl(c, p[i], p[(i + 1) % p.size()]);
}
return A * 0.5;
}
} // namespace geometry
#line 2 "geometry/common_area_cp.hpp"
#include <algorithm>
#include <complex>
#line 2 "geometry/base.hpp"
#include <cmath>
namespace geometry {
using Real = double;
const Real EPS = 1e-8;
const Real PI = std::acos(static_cast<Real>(-1));
enum { OUT, ON, IN };
inline int sign(const Real& r) { return r <= -EPS ? -1 : r >= EPS ? 1 : 0; }
inline bool equals(const Real& a, const Real& b) { return sign(a - b) == 0; }
} // namespace geometry
#line 2 "geometry/cross_point_cl.hpp"
#line 4 "geometry/cross_point_cl.hpp"
#line 2 "geometry/circle.hpp"
#include <vector>
#line 2 "geometry/point.hpp"
#line 5 "geometry/point.hpp"
#include <iostream>
#line 7 "geometry/point.hpp"
#line 9 "geometry/point.hpp"
namespace geometry {
using Point = std::complex<Real>;
std::istream& operator>>(std::istream& is, Point& p) {
Real a, b;
is >> a >> b;
p = Point(a, b);
return is;
}
std::ostream& operator<<(std::ostream& os, const Point& p) {
return os << std::real(p) << " " << std::imag(p);
}
Point operator*(const Point& p, const Real& d) {
return Point(std::real(p) * d, std::imag(p) * d);
}
// rotate point p counterclockwise by theta rad
Point rotate(Real theta, const Point& p) {
return Point(std::cos(theta) * std::real(p) - std::sin(theta) * std::imag(p),
std::sin(theta) * std::real(p) + std::cos(theta) * std::imag(p));
}
Real cross(const Point& a, const Point& b) {
return std::real(a) * std::imag(b) - std::imag(a) * std::real(b);
}
Real dot(const Point& a, const Point& b) {
return std::real(a) * std::real(b) + std::imag(a) * std::imag(b);
}
bool compare_x(const Point& a, const Point& b) {
return equals(std::real(a), std::real(b)) ? std::imag(a) < std::imag(b)
: std::real(a) < std::real(b);
}
bool compare_y(const Point& a, const Point& b) {
return equals(std::imag(a), std::imag(b)) ? std::real(a) < std::real(b)
: std::imag(a) < std::imag(b);
}
using Points = std::vector<Point>;
} // namespace geometry
#line 6 "geometry/circle.hpp"
namespace geometry {
struct Circle {
Point p;
Real r{};
Circle() = default;
Circle(const Point& p, const Real& r) : p(p), r(r) {}
};
using Circles = std::vector<Circle>;
} // namespace geometry
#line 2 "geometry/line.hpp"
#include <cassert>
#line 6 "geometry/line.hpp"
#line 8 "geometry/line.hpp"
namespace geometry {
struct Line {
Point a, b;
Line() = default;
Line(const Point& a, const Point& b) : a(a), b(b) {}
Line(const Real& A, const Real& B, const Real& C) { // Ax+By=C
if (equals(A, 0)) {
assert(!equals(B, 0));
a = Point(0, C / B);
b = Point(1, C / B);
} else if (equals(B, 0)) {
a = Point(C / A, 0);
b = Point(C / A, 1);
} else if (equals(C, 0)) {
a = Point(0, C / B);
b = Point(1, (C - A) / B);
} else {
a = Point(0, C / B);
b = Point(C / A, 0);
}
}
friend std::ostream& operator<<(std::ostream& os, Line& l) {
return os << l.a << " to " << l.b;
}
friend std::istream& operator>>(std::istream& is, Line& l) {
return is >> l.a >> l.b;
}
};
using Lines = std::vector<Line>;
} // namespace geometry
#line 2 "geometry/projection.hpp"
#line 4 "geometry/projection.hpp"
#line 7 "geometry/projection.hpp"
namespace geometry {
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A
Point projection(const Line& l, const Point& p) {
auto t = dot(p - l.a, l.a - l.b) / std::norm(l.a - l.b);
return l.a + (l.a - l.b) * t;
}
} // namespace geometry
#line 10 "geometry/cross_point_cl.hpp"
namespace geometry {
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D
Points cross_point_cl(const Circle& c, const Line& l) {
Point pr = projection(l, c.p);
if (equals(std::abs(pr - c.p), c.r)) return {pr};
Point e = (l.b - l.a) / std::abs(l.b - l.a);
auto k = std::sqrt(std::norm(c.r) - std::norm(pr - c.p));
return {pr - e * k, pr + e * k};
}
} // namespace geometry
#line 2 "geometry/distance_sp.hpp"
#line 4 "geometry/distance_sp.hpp"
#line 2 "geometry/is_intersect_sp.hpp"
#line 2 "geometry/ccw.hpp"
#line 4 "geometry/ccw.hpp"
#line 6 "geometry/ccw.hpp"
namespace geometry {
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C
constexpr int COUNTER_CLOCKWISE = +1;
constexpr int CLOCKWISE = -1;
constexpr int ONLINE_BACK = +2; // c-a-b
constexpr int ONLINE_FRONT = -2; // a-b-c
constexpr int ON_SEGMENT = 0; // a-c-b
int ccw(const Point& a, Point b, Point c) {
b = b - a, c = c - a;
if (sign(cross(b, c)) == +1) return COUNTER_CLOCKWISE;
if (sign(cross(b, c)) == -1) return CLOCKWISE;
if (sign(dot(b, c)) == -1) return ONLINE_BACK;
if (std::norm(b) < std::norm(c)) return ONLINE_FRONT;
return ON_SEGMENT;
}
} // namespace geometry
#line 2 "geometry/segment.hpp"
#line 4 "geometry/segment.hpp"
#line 6 "geometry/segment.hpp"
namespace geometry {
struct Segment : Line {
Segment() = default;
using Line::Line;
};
using Segments = std::vector<Segment>;
} // namespace geometry
#line 6 "geometry/is_intersect_sp.hpp"
namespace geometry {
bool is_intersect_sp(const Segment& s, const Point& p) {
return ccw(s.a, s.b, p) == ON_SEGMENT;
}
} // namespace geometry
#line 9 "geometry/distance_sp.hpp"
namespace geometry {
Real distance_sp(const Segment& s, const Point& p) {
Point r = projection(s, p);
if (is_intersect_sp(s, r)) return std::abs(r - p);
return std::min(std::abs(s.a - p), std::abs(s.b - p));
}
} // namespace geometry
#line 2 "geometry/is_intersect_cs.hpp"
#line 4 "geometry/is_intersect_cs.hpp"
#line 10 "geometry/is_intersect_cs.hpp"
namespace geometry {
int is_intersect_cs(const Circle& c, const Segment& l) {
Point h = projection(l, c.p);
if (sign(std::norm(h - c.p) - std::norm(c.r)) > 0) return 0;
auto d1 = std::abs(c.p - l.a), d2 = std::abs(c.p - l.b);
if (sign(c.r - d1) >= 0 && sign(c.r - d2) >= 0) return 0;
if (sign(c.r - d1) < 0 && sign(d2 - c.r) > 0 ||
sign(d1 - c.r) > 0 && sign(c.r - d2) < 0)
return 1;
if (sign(dot(l.a - h, l.b - h)) < 0) return 2;
return 0;
}
} // namespace geometry
#line 2 "geometry/polygon.hpp"
#line 4 "geometry/polygon.hpp"
#line 6 "geometry/polygon.hpp"
namespace geometry {
using Polygon = std::vector<Point>;
using Polygons = std::vector<Polygon>;
} // namespace geometry
#line 12 "geometry/common_area_cp.hpp"
namespace geometry {
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H
Real ca_cp_impl(const Circle& c, const Point& a, const Point& b) {
auto va = c.p - a, vb = c.p - b;
Real f = cross(va, vb), ret = 0;
if (sign(f) == 0) return ret;
if (sign(std::max(std::abs(va), std::abs(vb)) - c.r) <= 0) return f;
if (sign(distance_sp(Segment(a, b), c.p) - c.r) >= 0)
return std::norm(c.r) * std::arg(vb * std::conj(va));
auto tot = cross_point_cl(c, Line(a, b));
if (is_intersect_cs(c, Segment(a, b)) != 2 and
dot(a - tot[0], b - tot[0]) < 0) {
std::swap(tot[0], tot[1]);
}
tot.emplace(tot.begin(), a);
tot.emplace_back(b);
for (int i = 1; i < (int)tot.size(); i++) {
ret += ca_cp_impl(c, tot[i - 1], tot[i]);
}
return ret;
}
Real common_area_cp(const Circle& c, const Polygon& p) {
if (p.size() < 3) return 0;
Real A = 0;
for (int i = 0; i < p.size(); i++) {
A += ca_cp_impl(c, p[i], p[(i + 1) % p.size()]);
}
return A * 0.5;
}
} // namespace geometry