競技プログラミング用 C++ ライブラリ
geometry/polygon.hpp · GitHub で見る
共通ページ
#include "lib/geometry/polygon.hpp"
using namespace geometry;
std::vector<IPoint> polygon{ {0, 0}, {4, 0}, {4, 3}, {0, 3} };
auto twice = signed_area2(polygon); // 24
auto where = contains(polygon, IPoint(2, 1)); // Location::inside
auto center = centroid(polygon); // (2, 1.5)
auto left = convex_cut(polygon, Line<std::int64_t>{ {2, -1}, {2, 4} });
auto left_area = area(left); // 6
多角形は境界に沿った頂点列 vector<Point<T>> で渡します。末尾に始点を重複させる必要はありません。
通常は反時計回りを使いますが、面積・周長・重心・一般の内外判定は時計回りでも使えます。
T は符号付き整数または浮動小数点数とし、整数なら IPoint、実数なら FPoint が使えます。
| 操作 | 内容・返り値 | 計算量 |
|---|---|---|
signed_area2(polygon) |
符号付き面積の2倍を wide_t<T> で返す。反時計回りが正 |
\(O(n)\) |
area(polygon) |
面積の絶対値を Real で返す |
\(O(n)\) |
perimeter(polygon) |
最後から最初への辺を含む周長を Real で返す |
\(O(n)\) |
is_convex(polygon,strict = false,eps = epsilon) |
単純多角形が凸か判定。strict = true では同一直線上の連続する3頂点も認めない |
\(O(n)\) |
contains(polygon,p,eps = epsilon) |
一般の単純多角形に対する Location::outside・boundary・inside
|
\(O(n)\) |
centroid(polygon,eps = epsilon) |
面積についての重心を optional<FPoint> で返す。面積0は nullopt
|
\(O(n)\) |
convex_cut(polygon,line,eps = epsilon) |
凸多角形を有向直線 line.a -> line.b の左側で切り、残る頂点列を vector<FPoint> で返す。直線上も残す |
\(O(n)\) |
is_convex は入力が単純であることを前提にします。自己交差の検査はしません。0・1・2頂点は、strict = false なら凸、true なら凸でないものとします。
contains は頂点・辺上を boundary と区別します。空の多角形は常に outside、単点・線分には内部がなく、そこに乗る点だけが boundary です。
空・単点の面積と周長は0です。2点の周長は線分長の2倍です。
centroid は頂点の単純平均ではなく、囲まれた領域の重心です。
convex_cut には凸な境界順の頂点列と、異なる2点からなる直線を渡します。切断後が空・単点・線分になることもあります。
整数の符号判定と signed_area2 は浮動小数点に変換せず計算します。計算途中の積・和は符号付き128 bitに収めてください。
area・perimeter・centroid・切断後の座標は実数です。浮動小数点の eps は絶対誤差のしきい値なので、座標の大きさに合わせて変更してください。整数では eps を使いません。centroid の面積と退化判定も、整数入力なら128 bitで計算してから実数に変換します。
頂点数を \(n\) とすると、各操作は \(O(n)\) 時間です。convex_cut は返り値を含め \(O(n)\) 空間、それ以外は返り値を除き \(O(1)\) 空間です。
#ifndef GEOMETRY_POLYGON_HPP
#define GEOMETRY_POLYGON_HPP
#include "linear.hpp"
#include <cassert>
#include <cmath>
#include <optional>
#include <vector>
namespace geometry {
template <class T>
wide_t<T> signed_area2(const std::vector<Point<T>> &polygon) {
wide_t<T> result = 0;
for (std::size_t i = 2; i < polygon.size(); ++i) result += cross(polygon[0], polygon[i - 1], polygon[i]);
return result;
}
template <class T>
Real area(const std::vector<Point<T>> &polygon) { return std::abs(Real(signed_area2(polygon))) / 2; }
template <class T>
Real perimeter(const std::vector<Point<T>> &polygon) {
Real result = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) result += distance(polygon[i], polygon[(i + 1) % polygon.size()]);
return result;
}
template <class T>
bool is_convex(const std::vector<Point<T>> &polygon, bool strict = false, Real eps = epsilon) {
if (polygon.size() < 3) return !strict;
int direction = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) {
int turn = orientation(polygon[i], polygon[(i + 1) % polygon.size()], polygon[(i + 2) % polygon.size()], eps);
if (!turn) { if (strict) return false; }
else if (direction && turn != direction) return false;
else direction = turn;
}
return true;
}
template <class T>
Location contains(const std::vector<Point<T>> &polygon, Point<T> p, Real eps = epsilon) {
int winding = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) {
const auto &a = polygon[i], &b = polygon[(i + 1) % polygon.size()];
if (on_segment(Segment<T>{a, b}, p, eps)) return Location::boundary;
int ay = sign(wide_t<T>(a.y) - p.y, eps), by = sign(wide_t<T>(b.y) - p.y, eps);
int turn = orientation(a, b, p, eps);
if (ay <= 0 && by > 0 && turn > 0) ++winding;
if (ay > 0 && by <= 0 && turn < 0) --winding;
}
return winding ? Location::inside : Location::outside;
}
template <class T>
std::optional<FPoint> centroid(const std::vector<Point<T>> &polygon, Real eps = epsilon) {
if (polygon.size() < 3) return std::nullopt;
using W = wide_t<T>;
FPoint origin = to_real(polygon[0]), sum{};
W total = 0;
for (std::size_t i = 2; i < polygon.size(); ++i) {
FPoint a{Real(W(polygon[i - 1].x) - polygon[0].x), Real(W(polygon[i - 1].y) - polygon[0].y)};
FPoint b{Real(W(polygon[i].x) - polygon[0].x), Real(W(polygon[i].y) - polygon[0].y)};
W weight = cross(polygon[0], polygon[i - 1], polygon[i]);
sum += (a + b) * Real(weight);
total += weight;
}
if (!sign(total, eps)) return std::nullopt;
return origin + sum / (3 * Real(total));
}
template <class T>
std::vector<FPoint> convex_cut(const std::vector<Point<T>> &polygon, Line<T> line, Real eps = epsilon) {
assert(line.a != line.b);
std::vector<FPoint> result;
result.reserve(polygon.size() + 1);
auto append = [&](FPoint p) { if (result.empty() || result.back() != p) result.push_back(p); };
for (std::size_t i = 0; i < polygon.size(); ++i) {
const auto &a = polygon[i], &b = polygon[(i + 1) % polygon.size()];
auto ca = cross(line.a, line.b, a), cb = cross(line.a, line.b, b);
int sa = sign(ca, eps), sb = sign(cb, eps);
if (sa >= 0) append(to_real(a));
if (sa * sb < 0) append(to_real(a) + (to_real(b) - to_real(a)) * (Real(ca) / (Real(ca) - Real(cb))));
}
if (result.size() > 1 && result.front() == result.back()) result.pop_back();
return result;
}
}
#endif
#line 1 "geometry/polygon.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 5 "geometry/polygon.hpp"
#include <cassert>
#line 9 "geometry/polygon.hpp"
namespace geometry {
template <class T>
wide_t<T> signed_area2(const std::vector<Point<T>> &polygon) {
wide_t<T> result = 0;
for (std::size_t i = 2; i < polygon.size(); ++i) result += cross(polygon[0], polygon[i - 1], polygon[i]);
return result;
}
template <class T>
Real area(const std::vector<Point<T>> &polygon) { return std::abs(Real(signed_area2(polygon))) / 2; }
template <class T>
Real perimeter(const std::vector<Point<T>> &polygon) {
Real result = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) result += distance(polygon[i], polygon[(i + 1) % polygon.size()]);
return result;
}
template <class T>
bool is_convex(const std::vector<Point<T>> &polygon, bool strict = false, Real eps = epsilon) {
if (polygon.size() < 3) return !strict;
int direction = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) {
int turn = orientation(polygon[i], polygon[(i + 1) % polygon.size()], polygon[(i + 2) % polygon.size()], eps);
if (!turn) { if (strict) return false; }
else if (direction && turn != direction) return false;
else direction = turn;
}
return true;
}
template <class T>
Location contains(const std::vector<Point<T>> &polygon, Point<T> p, Real eps = epsilon) {
int winding = 0;
for (std::size_t i = 0; i < polygon.size(); ++i) {
const auto &a = polygon[i], &b = polygon[(i + 1) % polygon.size()];
if (on_segment(Segment<T>{a, b}, p, eps)) return Location::boundary;
int ay = sign(wide_t<T>(a.y) - p.y, eps), by = sign(wide_t<T>(b.y) - p.y, eps);
int turn = orientation(a, b, p, eps);
if (ay <= 0 && by > 0 && turn > 0) ++winding;
if (ay > 0 && by <= 0 && turn < 0) --winding;
}
return winding ? Location::inside : Location::outside;
}
template <class T>
std::optional<FPoint> centroid(const std::vector<Point<T>> &polygon, Real eps = epsilon) {
if (polygon.size() < 3) return std::nullopt;
using W = wide_t<T>;
FPoint origin = to_real(polygon[0]), sum{};
W total = 0;
for (std::size_t i = 2; i < polygon.size(); ++i) {
FPoint a{Real(W(polygon[i - 1].x) - polygon[0].x), Real(W(polygon[i - 1].y) - polygon[0].y)};
FPoint b{Real(W(polygon[i].x) - polygon[0].x), Real(W(polygon[i].y) - polygon[0].y)};
W weight = cross(polygon[0], polygon[i - 1], polygon[i]);
sum += (a + b) * Real(weight);
total += weight;
}
if (!sign(total, eps)) return std::nullopt;
return origin + sum / (3 * Real(total));
}
template <class T>
std::vector<FPoint> convex_cut(const std::vector<Point<T>> &polygon, Line<T> line, Real eps = epsilon) {
assert(line.a != line.b);
std::vector<FPoint> result;
result.reserve(polygon.size() + 1);
auto append = [&](FPoint p) { if (result.empty() || result.back() != p) result.push_back(p); };
for (std::size_t i = 0; i < polygon.size(); ++i) {
const auto &a = polygon[i], &b = polygon[(i + 1) % polygon.size()];
auto ca = cross(line.a, line.b, a), cb = cross(line.a, line.b, b);
int sa = sign(ca, eps), sb = sign(cb, eps);
if (sa >= 0) append(to_real(a));
if (sa * sb < 0) append(to_real(a) + (to_real(b) - to_real(a)) * (Real(ca) / (Real(ca) - Real(cb))));
}
if (result.size() > 1 && result.front() == result.back()) result.pop_back();
return result;
}
}