競技プログラミング用 C++ ライブラリ
test/geometry_circle_polygon.test.cpp · GitHub で見る
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H"
#define ERROR "1e-7"
#include "geometry/circle.hpp"
#include <iomanip>
#include <iostream>
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n = 0;
geometry::Circle circle;
std::cin >> n >> circle.radius;
std::vector<geometry::IPoint> polygon(n);
for (auto &p : polygon) std::cin >> p;
std::cout << std::fixed << std::setprecision(15) << geometry::intersection_area(circle, polygon) << '\n';
}
#line 1 "test/geometry_circle_polygon.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H"
#define ERROR "1e-7"
#line 1 "geometry/circle.hpp"
#line 1 "geometry/linear.hpp"
#line 1 "geometry/point.hpp"
#line 1 "components/radix_sort.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <utility>
#include <vector>
template <int Bits, class T, class Key>
void stable_radix_sort(std::vector<T> &values, Key &&key) {
static_assert(1 <= Bits && Bits <= 16);
constexpr unsigned buckets = 1U << Bits, mask = buckets - 1;
using K = std::remove_cvref_t<decltype(key(std::declval<const T &>()))>;
static_assert(std::is_integral_v<K> && std::is_unsigned_v<K> && sizeof(K) <= sizeof(std::uint64_t));
if (values.size() < 2) return;
if (values.size() < 256) {
std::stable_sort(values.begin(), values.end(), [&](const T &a, const T &b) { return key(a) < key(b); });
return;
}
std::uint64_t first = key(values.front()), previous = first, varying = 0;
bool sorted = true;
for (std::size_t i = 1; i < values.size(); ++i) {
std::uint64_t current = key(values[i]);
varying |= first ^ current;
sorted &= previous <= current;
previous = current;
}
if (sorted) return;
std::vector<T> scratch(values.size());
using Counts = std::conditional_t<(Bits <= 11), std::array<std::size_t, buckets>, std::vector<std::size_t>>;
Counts count = [] {
if constexpr (Bits <= 11) return Counts{};
else return Counts(buckets);
}();
for (int shift = 0; shift < 64; shift += Bits) {
if (!((varying >> shift) & mask)) continue;
std::fill(count.begin(), count.end(), 0);
for (const auto &value : values) ++count[(static_cast<std::uint64_t>(key(value)) >> shift) & mask];
std::size_t sum = 0;
for (auto &x : count) {
auto n = x;
x = sum, sum += n;
}
for (auto &&value : values) scratch[count[(static_cast<std::uint64_t>(key(value)) >> shift) & mask]++] = std::move(value);
values.swap(scratch);
}
}
template <class T, class Key>
void stable_radix_sort(std::vector<T> &values, Key key) {
stable_radix_sort<8>(values, std::move(key));
}
namespace radix_sort_detail {
template <int Bits = 8, class T, class Key>
void sort_by_key(std::vector<T> &values, Key key) {
using K = std::remove_cvref_t<decltype(key(std::declval<const T &>()))>;
if constexpr (std::is_integral_v<K> && !std::is_same_v<K, bool> && sizeof(K) <= 8 && std::is_default_constructible_v<T>) {
using U = std::make_unsigned_t<K>;
constexpr U sign = std::is_signed_v<K> ? U(1) << (sizeof(K) * 8 - 1) : U(0);
stable_radix_sort<Bits>(values, [&](const T &value) { return U(U(key(value)) ^ sign); });
} else std::sort(values.begin(), values.end(), [&](const T &a, const T &b) { return key(a) < key(b); });
}
}
#line 5 "geometry/point.hpp"
#include <cmath>
#line 7 "geometry/point.hpp"
#include <istream>
#include <numbers>
#include <optional>
#include <ostream>
#line 12 "geometry/point.hpp"
namespace geometry {
using Real = long double;
inline constexpr Real epsilon = 1e-12L;
inline constexpr Real pi = std::numbers::pi_v<Real>;
enum class Location { outside = -1, boundary = 0, inside = 1 };
template<class T>
inline constexpr bool integer = std::is_integral_v<T> || std::is_same_v<T, __int128_t> || std::is_same_v<T, __uint128_t>;
template<class T>
using wide_t = std::conditional_t<integer<T>, __int128_t, Real>;
template<class T>
struct Point {
static_assert((std::is_arithmetic_v<T> && std::is_signed_v<T>) || std::is_same_v<T, __int128_t>);
T x{}, y{};
constexpr Point() = default;
constexpr Point(T x, T y) : x(x), y(y) {}
template<class U> explicit constexpr Point(const Point<U>& p) : x(static_cast<T>(p.x)), y(static_cast<T>(p.y)) {}
constexpr bool operator==(const Point&) const = default;
constexpr bool operator<(const Point& p) const { return x < p.x || (x == p.x && y < p.y); }
constexpr Point operator-() const { return {-x, -y}; }
constexpr Point& operator+=(const Point& p) { x += p.x; y += p.y; return *this; }
constexpr Point& operator-=(const Point& p) { x -= p.x; y -= p.y; return *this; }
constexpr Point& operator*=(T c) { x *= c; y *= c; return *this; }
constexpr Point& operator/=(T c) { x /= c; y /= c; return *this; }
friend constexpr Point operator+(Point p, const Point& q) { p += q; return p; }
friend constexpr Point operator-(Point p, const Point& q) { p -= q; return p; }
friend constexpr Point operator*(Point p, T c) { p *= c; return p; }
friend constexpr Point operator*(T c, Point p) { p *= c; return p; }
friend constexpr Point operator/(Point p, T c) { p /= c; return p; }
friend std::istream& operator>>(std::istream& in, Point& p) { return in >> p.x >> p.y; }
friend std::ostream& operator<<(std::ostream& out, const Point& p) { return out << p.x << ' ' << p.y; }
};
using IPoint = Point<std::int64_t>;
using FPoint = Point<Real>;
template<class T>
constexpr int sign(T value, Real eps = epsilon) {
if constexpr (integer<T>) return (value > 0) - (value < 0);
else return (value > eps) - (value < -eps);
}
template<class T>
constexpr wide_t<T> dot(Point<T> a, Point<T> b) {
return wide_t<T>(a.x) * b.x + wide_t<T>(a.y) * b.y;
}
template<class T>
constexpr wide_t<T> cross(Point<T> a, Point<T> b) {
return wide_t<T>(a.x) * b.y - wide_t<T>(a.y) * b.x;
}
template<class T>
constexpr wide_t<T> cross(Point<T> a, Point<T> b, Point<T> c) {
using W = wide_t<T>;
return (W(b.x) - a.x) * (W(c.y) - a.y) - (W(b.y) - a.y) * (W(c.x) - a.x);
}
template<class T>
constexpr wide_t<T> norm2(Point<T> p) { return dot(p, p); }
template<class T>
constexpr wide_t<T> distance_squared(Point<T> a, Point<T> b) {
using W = wide_t<T>;
W x = W(a.x) - b.x, y = W(a.y) - b.y;
return x * x + y * y;
}
template<class T>
constexpr int orientation(Point<T> a, Point<T> b, Point<T> c, Real eps = epsilon) { return sign(cross(a, b, c), eps); }
template<class T>
constexpr int ccw(Point<T> a, Point<T> b, Point<T> c, Real eps = epsilon) {
if (int turn = orientation(a, b, c, eps)) return turn;
using W = wide_t<T>;
W x = W(b.x) - a.x, y = W(b.y) - a.y, u = W(c.x) - a.x, v = W(c.y) - a.y;
if (sign(x * u + y * v, eps) < 0) return 2;
if (sign(u * u + v * v - x * x - y * y, eps) > 0) return -2;
return 0;
}
template<class T>
constexpr FPoint to_real(Point<T> p) { return FPoint(p); }
template<class T>
Real length(Point<T> p) { return std::hypot(Real(p.x), Real(p.y)); }
template<class T>
Real distance(Point<T> a, Point<T> b) {
return std::hypot(Real(wide_t<T>(a.x) - b.x), Real(wide_t<T>(a.y) - b.y));
}
template<class T>
constexpr bool almost_equal(Point<T> a, Point<T> b, Real eps = epsilon) {
if constexpr (integer<T>) return a == b;
else return std::abs(Real(a.x) - b.x) <= eps && std::abs(Real(a.y) - b.y) <= eps;
}
template<class T>
constexpr Point<T> rotate90(Point<T> p) { return {-p.y, p.x}; }
template<class T>
Real argument(Point<T> p) { return std::atan2(Real(p.y), Real(p.x)); }
template<class T>
Real angle(Point<T> a, Point<T> b) { return std::atan2(Real(cross(a, b)), Real(dot(a, b))); }
template<class T>
FPoint rotate(Point<T> p, Real theta) {
Real c = std::cos(theta), s = std::sin(theta);
return {Real(p.x) * c - Real(p.y) * s, Real(p.x) * s + Real(p.y) * c};
}
template<class T>
std::optional<FPoint> unit(Point<T> p) {
Real n = length(p);
if (!n) return std::nullopt;
return to_real(p) / n;
}
template<class T>
struct PolarLess {
bool operator()(Point<T> a, Point<T> b) const {
if (a == Point<T>{} || b == Point<T>{}) return a == Point<T>{} && b != Point<T>{};
auto half = [](Point<T> p) { return p.y < 0 || (p.y == 0 && p.x < 0); };
if (half(a) != half(b)) return half(a) < half(b);
if constexpr (integer<T> && sizeof(T) <= 4) {
auto c = std::int64_t(a.x) * b.y - std::int64_t(a.y) * b.x;
if (c) return c > 0;
auto norm = [](Point<T> p) { return std::uint64_t(std::int64_t(p.x) * p.x) + std::uint64_t(std::int64_t(p.y) * p.y); };
return norm(a) < norm(b);
} else {
if (auto c = cross(a, b); c != 0) return c > 0;
return norm2(a) < norm2(b);
}
}
};
template<class T>
void polar_sort(std::vector<Point<T>> &points) {
if constexpr (integer<T> && sizeof(T) <= 4) {
if (points.size() >= 256) {
struct Entry { std::uint64_t key; Point<T> point; };
std::vector<Entry> order;
order.reserve(points.size());
constexpr std::uint64_t scale = (std::uint64_t(1) << 61) - 1;
for (auto p : points) {
std::int64_t x = p.x, y = p.y;
std::uint64_t a = x < 0 ? -x : x, b = y < 0 ? -y : y;
unsigned quadrant = y < 0 ? (x < 0 ? 2 : 3) : (x < 0 ? 1 : 0);
unsigned octant = quadrant * 2 + ((a < b) ^ (quadrant & 1));
std::uint64_t ratio = a || b ? std::min(scale, std::uint64_t(double(std::min(a, b)) / double(std::max(a, b)) * double(scale))) : 0;
if (octant & 1) ratio = scale - ratio;
order.push_back({(std::uint64_t(octant) << 61) | ratio, p});
}
stable_radix_sort<11>(order, [](const Entry &entry) { return entry.key; });
for (std::size_t first = 0; first < order.size();) {
std::size_t last = first + 1;
while (last < order.size() && order[last].key == order[first].key) ++last;
auto less = [](const Entry &a, const Entry &b) { return PolarLess<T>{}(a.point, b.point); };
if (last - first <= 8 || !std::is_sorted(order.begin() + first, order.begin() + last, less))
std::sort(order.begin() + first, order.begin() + last, less);
first = last;
}
for (std::size_t i = 0; i < points.size(); ++i) points[i] = order[i].point;
return;
}
}
std::sort(points.begin(), points.end(), PolarLess<T>{});
}
}
#line 8 "geometry/linear.hpp"
namespace geometry {
template<class T> struct Line { Point<T> a, b; };
template<class T> struct Segment { Point<T> a, b; };
template<class T>
constexpr bool parallel(Line<T> l, Line<T> m, Real eps = epsilon) {
using W = wide_t<T>;
return sign((W(l.b.x) - l.a.x) * (W(m.b.y) - m.a.y) - (W(l.b.y) - l.a.y) * (W(m.b.x) - m.a.x), eps) == 0;
}
template<class T>
constexpr bool orthogonal(Line<T> l, Line<T> m, Real eps = epsilon) {
using W = wide_t<T>;
return sign((W(l.b.x) - l.a.x) * (W(m.b.x) - m.a.x) + (W(l.b.y) - l.a.y) * (W(m.b.y) - m.a.y), eps) == 0;
}
template<class T>
constexpr bool on_line(Line<T> l, Point<T> p, Real eps = epsilon) {
return l.a == l.b ? almost_equal(l.a, p, eps) : orientation(l.a, l.b, p, eps) == 0;
}
template<class T>
constexpr bool on_segment(Segment<T> s, Point<T> p, Real eps = epsilon) {
if (orientation(s.a, s.b, p, eps)) return false;
if constexpr (integer<T>)
return std::min(s.a.x, s.b.x) <= p.x && p.x <= std::max(s.a.x, s.b.x)
&& std::min(s.a.y, s.b.y) <= p.y && p.y <= std::max(s.a.y, s.b.y);
else return std::min(s.a.x, s.b.x) - eps <= p.x && p.x <= std::max(s.a.x, s.b.x) + eps
&& std::min(s.a.y, s.b.y) - eps <= p.y && p.y <= std::max(s.a.y, s.b.y) + eps;
}
template<class T>
constexpr bool intersects(Segment<T> s, Segment<T> t, Real eps = epsilon) {
int a = orientation(s.a, s.b, t.a, eps), b = orientation(s.a, s.b, t.b, eps);
int c = orientation(t.a, t.b, s.a, eps), d = orientation(t.a, t.b, s.b, eps);
return (!a && on_segment(s, t.a, eps)) || (!b && on_segment(s, t.b, eps))
|| (!c && on_segment(t, s.a, eps)) || (!d && on_segment(t, s.b, eps)) || (a * b < 0 && c * d < 0);
}
template<class T>
constexpr bool intersects(Line<T> l, Segment<T> s, Real eps = epsilon) {
if (l.a == l.b) return on_segment(s, l.a, eps);
return orientation(l.a, l.b, s.a, eps) * orientation(l.a, l.b, s.b, eps) <= 0;
}
template<class T>
constexpr bool intersects(Segment<T> s, Line<T> l, Real eps = epsilon) { return intersects(l, s, eps); }
template<class T>
constexpr bool intersects(Line<T> l, Line<T> m, Real eps = epsilon) {
if (l.a == l.b) return on_line(m, l.a, eps);
if (m.a == m.b) return on_line(l, m.a, eps);
return !parallel(l, m, eps) || on_line(l, m.a, eps);
}
template<class T>
FPoint projection(Line<T> l, Point<T> p) {
using W = wide_t<T>;
W x = W(l.b.x) - l.a.x, y = W(l.b.y) - l.a.y, n = x * x + y * y;
if (!n) return to_real(l.a);
Real t = Real(x * (W(p.x) - l.a.x) + y * (W(p.y) - l.a.y)) / Real(n);
return {Real(l.a.x) + Real(x) * t, Real(l.a.y) + Real(y) * t};
}
template<class T>
FPoint reflection(Line<T> l, Point<T> p) { return projection(l, p) * Real(2) - to_real(p); }
template<class T>
std::optional<FPoint> intersection(Line<T> l, Line<T> m, Real eps = epsilon) {
if (l.a == l.b) return on_line(m, l.a, eps) ? std::optional(to_real(l.a)) : std::nullopt;
if (m.a == m.b) return on_line(l, m.a, eps) ? std::optional(to_real(m.a)) : std::nullopt;
using W = wide_t<T>;
W x = W(l.b.x) - l.a.x, y = W(l.b.y) - l.a.y, u = W(m.b.x) - m.a.x, v = W(m.b.y) - m.a.y;
W d = x * v - y * u;
if (!sign(d, eps)) return std::nullopt;
Real t = Real((W(m.a.x) - l.a.x) * v - (W(m.a.y) - l.a.y) * u) / Real(d);
return FPoint{Real(l.a.x) + Real(x) * t, Real(l.a.y) + Real(y) * t};
}
template<class T>
std::vector<FPoint> intersection(Segment<T> s, Segment<T> t, Real eps = epsilon) {
if (!intersects(s, t, eps)) return {};
if (auto p = intersection(Line<T>{s.a, s.b}, Line<T>{t.a, t.b}, eps)) return {*p};
std::array<Point<T>, 4> candidates{s.a, s.b, t.a, t.b};
std::sort(candidates.begin(), candidates.end());
std::vector<FPoint> result;
std::optional<Point<T>> first, last;
for (auto p : candidates) if (on_segment(s, p, eps) && on_segment(t, p, eps)) {
if (!first) first = p;
last = p;
}
if (first) {
result.push_back(to_real(*first));
if (!almost_equal(*first, *last, eps)) result.push_back(to_real(*last));
}
return result;
}
template<class T>
std::vector<FPoint> intersection(Line<T> l, Segment<T> s, Real eps = epsilon) {
if (!intersects(l, s, eps)) return {};
if (auto p = intersection(l, Line<T>{s.a, s.b}, eps)) return {*p};
if (almost_equal(s.a, s.b, eps)) return {to_real(s.a)};
return {to_real(s.a), to_real(s.b)};
}
template<class T>
std::vector<FPoint> intersection(Segment<T> s, Line<T> l, Real eps = epsilon) { return intersection(l, s, eps); }
template<class T>
Real distance_squared(Line<T> l, Point<T> p) {
if (l.a == l.b) return Real(distance_squared(l.a, p));
Real c = Real(cross(l.a, l.b, p));
return c * c / Real(distance_squared(l.a, l.b));
}
template<class T>
Real distance_squared(Segment<T> s, Point<T> p) {
using W = wide_t<T>;
W x = W(s.b.x) - s.a.x, y = W(s.b.y) - s.a.y;
W n = x * x + y * y, d = (W(p.x) - s.a.x) * x + (W(p.y) - s.a.y) * y;
if (d <= 0) return Real(distance_squared(s.a, p));
if (d >= n) return Real(distance_squared(s.b, p));
Real c = Real(cross(s.a, s.b, p));
return c * c / Real(n);
}
template<class T> Real distance(Line<T> l, Point<T> p) { return std::sqrt(distance_squared(l, p)); }
template<class T> Real distance(Point<T> p, Line<T> l) { return distance(l, p); }
template<class T> Real distance(Segment<T> s, Point<T> p) { return std::sqrt(distance_squared(s, p)); }
template<class T> Real distance(Point<T> p, Segment<T> s) { return distance(s, p); }
template<class T> Real distance_squared(Point<T> p, Line<T> l) { return distance_squared(l, p); }
template<class T> Real distance_squared(Point<T> p, Segment<T> s) { return distance_squared(s, p); }
template<class T>
Real distance_squared(Line<T> l, Line<T> m, Real eps = epsilon) {
if (intersects(l, m, eps)) return 0;
return l.a == l.b ? distance_squared(m, l.a) : distance_squared(l, m.a);
}
template<class T>
Real distance_squared(Line<T> l, Segment<T> s, Real eps = epsilon) {
if (intersects(l, s, eps)) return 0;
return l.a == l.b ? distance_squared(s, l.a) : std::min(distance_squared(l, s.a), distance_squared(l, s.b));
}
template<class T>
Real distance_squared(Segment<T> s, Line<T> l, Real eps = epsilon) { return distance_squared(l, s, eps); }
template<class T>
Real distance_squared(Segment<T> s, Segment<T> t, Real eps = epsilon) {
if (intersects(s, t, eps)) return 0;
return std::min({distance_squared(s, t.a), distance_squared(s, t.b), distance_squared(t, s.a), distance_squared(t, s.b)});
}
template<class T> Real distance(Line<T> l, Line<T> m, Real eps = epsilon) { return std::sqrt(distance_squared(l, m, eps)); }
template<class T> Real distance(Line<T> l, Segment<T> s, Real eps = epsilon) { return std::sqrt(distance_squared(l, s, eps)); }
template<class T> Real distance(Segment<T> s, Line<T> l, Real eps = epsilon) { return distance(l, s, eps); }
template<class T> Real distance(Segment<T> s, Segment<T> t, Real eps = epsilon) { return std::sqrt(distance_squared(s, t, eps)); }
template<class T>
std::optional<Line<Real>> perpendicular_bisector(Point<T> a, Point<T> b) {
if (a == b) return std::nullopt;
using W = wide_t<T>;
FPoint mid{Real(a.x) / 2 + Real(b.x) / 2, Real(a.y) / 2 + Real(b.y) / 2};
FPoint d{Real(W(b.y) - a.y), Real(W(a.x) - b.x)};
return Line<Real>{mid, mid + d};
}
}
#line 7 "geometry/circle.hpp"
#include <cassert>
#line 12 "geometry/circle.hpp"
#include <random>
#line 14 "geometry/circle.hpp"
namespace geometry {
struct Circle { FPoint center{}; Real radius = 0; };
enum class CircleRelation { separate, external_tangent, intersecting, internal_tangent, contained, coincident };
namespace circle_detail {
inline void check([[maybe_unused]] const Circle &c, [[maybe_unused]] Real eps = 0) { assert(c.radius >= 0 && eps >= 0); }
inline FPoint perpendicular(FPoint p) { return {-p.y, p.x}; }
inline std::vector<FPoint> ordered(FPoint a, FPoint b) {
if (b < a) std::swap(a, b);
return {a, b};
}
inline Circle diameter(FPoint a, FPoint b) {
FPoint center = a + (b - a) / Real(2);
return {center, distance(center, a)};
}
inline Real segment_area(Real radius, Real along, Real height) {
Real angle = std::atan2(height, along), square = angle * angle;
Real value = angle < 0.001L ? angle * square * (Real(2) / 3 + square * (-Real(2) / 15 + square * (Real(4) / 315 - square * Real(2) / 2835)))
: angle - std::sin(2 * angle) / 2;
return radius * radius * value;
}
inline Real chord_position(Real radius, Real other, Real d) {
if (std::abs(d - other) < std::abs(radius - other)) return ((d - other) * (d + other) + radius * radius) / (2 * d);
return (d + (radius - other) * (radius + other) / d) / 2;
}
}
inline Real area(const Circle &c) { circle_detail::check(c); return std::numbers::pi_v<Real> * c.radius * c.radius; }
inline Real circumference(const Circle &c) { circle_detail::check(c); return 2 * std::numbers::pi_v<Real> * c.radius; }
template <class T>
Location contains(const Circle &c, Point<T> point, Real eps = epsilon) {
circle_detail::check(c, eps);
Real d2 = norm2(to_real(point) - c.center), outer = c.radius + eps;
if (d2 > outer * outer) return Location::outside;
Real inner = c.radius - eps;
return inner > 0 && d2 < inner * inner ? Location::inside : Location::boundary;
}
inline CircleRelation relation(const Circle &a, const Circle &b, Real eps = epsilon) {
circle_detail::check(a, eps);
circle_detail::check(b);
Real d = distance(a.center, b.center), small = std::min(a.radius, b.radius), large = std::max(a.radius, b.radius);
if (d <= eps && large - small <= eps) return CircleRelation::coincident;
Real gap = d - large;
if (gap > small + eps) return CircleRelation::separate;
if (gap < -small - eps) return CircleRelation::contained;
if (std::abs(gap - small) <= eps) return CircleRelation::external_tangent;
if (std::abs(gap + small) <= eps) return CircleRelation::internal_tangent;
return CircleRelation::intersecting;
}
template <class T>
std::vector<FPoint> intersection(const Circle &c, const Line<T> &line, Real eps = epsilon) {
circle_detail::check(c, eps);
FPoint a = to_real(line.a), d = to_real(line.b) - a;
Real length2 = norm2(d);
if (!length2) return contains(c, a, eps) == Location::boundary ? std::vector<FPoint>{a} : std::vector<FPoint>{};
FPoint middle = a + d * (dot(c.center - a, d) / length2);
Real height = distance(c.center, middle);
if (height > c.radius + eps) return {};
if (std::abs(height - c.radius) <= eps) return {middle};
FPoint offset = d * std::sqrt(std::max(Real(0), (c.radius - height) * (c.radius + height) / length2));
return circle_detail::ordered(middle - offset, middle + offset);
}
template <class T>
std::vector<FPoint> intersection(const Circle &c, const Segment<T> &segment, Real eps = epsilon) {
auto points = intersection(c, Line<T>{segment.a, segment.b}, eps);
FPoint a = to_real(segment.a), d = to_real(segment.b) - a;
Real length2 = norm2(d);
if (!length2) return points;
Real tolerance = eps / std::sqrt(length2);
std::erase_if(points, [&](FPoint p) {
Real t = dot(p - a, d) / length2;
return t < -tolerance || t > 1 + tolerance;
});
return points;
}
inline std::optional<std::vector<FPoint>> intersection(const Circle &a, const Circle &b, Real eps = epsilon) {
CircleRelation type = relation(a, b, eps);
if (type == CircleRelation::coincident) {
if (a.radius == 0 && b.radius == 0) return std::vector<FPoint>{a.center + (b.center - a.center) / Real(2)};
return std::nullopt;
}
if (type == CircleRelation::separate || type == CircleRelation::contained) return std::vector<FPoint>{};
const Circle &first = a.radius <= b.radius ? a : b, &second = a.radius <= b.radius ? b : a;
FPoint v = second.center - first.center;
Real d = std::sqrt(norm2(v));
Real along = circle_detail::chord_position(first.radius, second.radius, d);
FPoint middle = first.center + v * (along / d);
if (type != CircleRelation::intersecting) return std::vector<FPoint>{middle};
Real height2 = std::max(Real(0), (first.radius - along) * (first.radius + along));
if (!height2) return std::vector<FPoint>{middle};
FPoint offset = circle_detail::perpendicular(v) * (std::sqrt(height2) / d);
return circle_detail::ordered(middle - offset, middle + offset);
}
template <class T>
std::vector<FPoint> tangent_points(const Circle &c, Point<T> point, Real eps = epsilon) {
circle_detail::check(c, eps);
FPoint p = to_real(point), v = p - c.center;
Real d2 = norm2(v), d = std::sqrt(d2);
if (!d) return c.radius == 0 ? std::vector<FPoint>{c.center} : std::vector<FPoint>{};
if (d < c.radius - eps) return {};
if (c.radius == 0) return {c.center};
if (std::abs(d - c.radius) <= eps) return {c.center + v * (c.radius / d)};
FPoint middle = c.center + v * (c.radius * c.radius / d2);
FPoint offset = circle_detail::perpendicular(v) * (c.radius * std::sqrt(std::max(Real(0), (d - c.radius) * (d + c.radius))) / d2);
return circle_detail::ordered(middle - offset, middle + offset);
}
inline std::optional<std::vector<Line<Real>>> common_tangents(const Circle &a, const Circle &b, Real eps = epsilon) {
circle_detail::check(a, eps);
circle_detail::check(b);
FPoint v = b.center - a.center;
Real d2 = norm2(v), d = std::sqrt(d2);
if (d <= eps) {
if (std::abs(a.radius - b.radius) <= eps) return std::nullopt;
return std::vector<Line<Real>>{};
}
std::vector<Line<Real>> result;
result.reserve(4);
Real scale = std::max({Real(1), d, a.radius, b.radius});
if (!a.radius && !b.radius) return std::vector<Line<Real>>{Line<Real>{a.center, b.center}};
if (!a.radius || !b.radius) {
const Circle &c = a.radius ? a : b;
FPoint p = a.radius ? b.center : a.center;
for (FPoint q : tangent_points(c, p, eps))
result.push_back({q, q + circle_detail::perpendicular((q - c.center) / c.radius) * scale});
return result;
}
for (int side : {1, -1}) {
Real difference = a.radius - side * b.radius, absolute = std::abs(difference);
if (d < absolute - eps) continue;
Real height = std::abs(d - absolute) <= eps ? 0 : std::sqrt(std::max(Real(0), (d - absolute) * (d + absolute)));
for (int direction : {1, -1}) {
FPoint normal = (v * difference + circle_detail::perpendicular(v) * (height * direction)) / d2;
FPoint p = a.center + normal * a.radius;
result.push_back({p, p + circle_detail::perpendicular(normal) * scale});
if (!height) break;
}
}
return result;
}
template <class T>
std::optional<Circle> circumcircle(Point<T> pa, Point<T> pb, Point<T> pc, Real eps = epsilon) {
assert(eps >= 0);
FPoint a = to_real(pa), u = to_real(pb) - a, v = to_real(pc) - a;
Real determinant = cross(u, v), u2 = norm2(u), v2 = norm2(v);
if (std::abs(determinant) <= eps * std::sqrt(std::max(u2, v2))) return std::nullopt;
FPoint offset{(u2 * v.y - v2 * u.y) / (2 * determinant), (u.x * v2 - v.x * u2) / (2 * determinant)};
return Circle{a + offset, std::sqrt(norm2(offset))};
}
template <class T>
std::optional<Circle> incircle(Point<T> pa, Point<T> pb, Point<T> pc, Real eps = epsilon) {
assert(eps >= 0);
FPoint a = to_real(pa), b = to_real(pb), c = to_real(pc);
Real x = distance(b, c), y = distance(c, a), z = distance(a, b), total = x + y + z;
Real area2 = std::abs(cross(b - a, c - a));
if (!total || area2 <= eps * std::max({x, y, z})) return std::nullopt;
return Circle{a + ((b - a) * y + (c - a) * z) / total, area2 / total};
}
inline Real overlap_area(const Circle &a, const Circle &b) {
circle_detail::check(a);
circle_detail::check(b);
Real d = distance(a.center, b.center), r = std::min(a.radius, b.radius), large = std::max(a.radius, b.radius), gap = d - large;
if (!r || gap >= r) return 0;
if (gap <= -r) return std::numbers::pi_v<Real> * r * r;
Real x = circle_detail::chord_position(r, large, d);
Real height = std::sqrt(std::max(Real(0), (r - x) * (r + x)));
return std::clamp(circle_detail::segment_area(r, x, height) + circle_detail::segment_area(large, d - x, height), Real(0), std::numbers::pi_v<Real> * r * r);
}
template <class T>
Real intersection_area(const Circle &c, const std::vector<Point<T>> &polygon) {
circle_detail::check(c);
if (!c.radius || polygon.size() < 3) return 0;
Real result = 0, radius2 = c.radius * c.radius;
for (std::size_t i = 0; i < polygon.size(); ++i) {
FPoint p = to_real(polygon[i]) - c.center, q = to_real(polygon[(i + 1) % polygon.size()]) - c.center, d = q - p;
Real a = norm2(d);
if (!a) continue;
if (norm2(p) <= radius2 && norm2(q) <= radius2) { result += cross(p, q); continue; }
Real b = dot(p, d), perpendicular = cross(p, d), determinant = a * radius2 - perpendicular * perpendicular;
std::array<Real, 4> split{0};
int count = 1;
if (determinant > 0) {
Real root = std::sqrt(determinant);
for (Real t : {(-b - root) / a, (-b + root) / a}) if (0 < t && t < 1) split[count++] = t;
}
split[count++] = 1;
for (int j = 1; j < count; ++j) {
FPoint u = p + d * split[j - 1], v = p + d * split[j];
if (norm2(p + d * ((split[j - 1] + split[j]) / 2)) < radius2) result += cross(u, v);
else result += radius2 * std::atan2(cross(u, v), dot(u, v));
}
}
return std::abs(result) / 2;
}
template <class T>
std::optional<Circle> minimum_enclosing_circle(const std::vector<Point<T>> &points, std::uint64_t seed = 712367821, Real eps = epsilon) {
assert(eps >= 0);
if (points.empty()) return std::nullopt;
std::vector<FPoint> p;
p.reserve(points.size());
for (auto point : points) p.push_back(to_real(point));
std::mt19937_64 random(seed);
std::shuffle(p.begin(), p.end(), random);
Circle result{p[0], 0};
for (std::size_t i = 1; i < p.size(); ++i) if (contains(result, p[i], eps) == Location::outside) {
result = {p[i], 0};
for (std::size_t j = 0; j < i; ++j) if (contains(result, p[j], eps) == Location::outside) {
result = circle_detail::diameter(p[i], p[j]);
for (std::size_t k = 0; k < j; ++k) if (contains(result, p[k], eps) == Location::outside) {
auto c = circumcircle(p[i], p[j], p[k], Real(0));
if (c) result = *c;
else {
std::array<Circle, 3> candidates{circle_detail::diameter(p[i], p[j]), circle_detail::diameter(p[i], p[k]), circle_detail::diameter(p[j], p[k])};
result = *std::max_element(candidates.begin(), candidates.end(), [](const Circle &a, const Circle &b) { return a.radius < b.radius; });
}
}
}
}
Real radius2 = result.radius * result.radius;
for (FPoint point : p) radius2 = std::max(radius2, norm2(point - result.center));
result.radius = std::sqrt(radius2);
return result;
}
}
#line 4 "test/geometry_circle_polygon.test.cpp"
#include <iomanip>
#include <iostream>
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n = 0;
geometry::Circle circle;
std::cin >> n >> circle.radius;
std::vector<geometry::IPoint> polygon(n);
for (auto &p : polygon) std::cin >> p;
std::cout << std::fixed << std::setprecision(15) << geometry::intersection_area(circle, polygon) << '\n';
}