sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

最小包含円

Geometry/Minimum_Enclosing_Circle.cpp · ソース · 文書 現行ACあり Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

整数座標の点列をすべて含む、半径が最小の円を求めます。円周上の点も判定できます。

std::vector<sirkit::MinimumEnclosingCircle::Point> points{{0, 0}, {1, 0}, {2, 0}};
auto circle = sirkit::minimum_enclosing_circle(points);
assert(circle->on_boundary(points[0]));
assert(!circle->on_boundary(points[1]));

minimum_enclosing_circle

std::optional<MinimumEnclosingCircle> minimum_enclosing_circle(
    std::span<const MinimumEnclosingCircle::Point> points, std::uint64_t seed = 0);

MinimumEnclosingCircle::Point は std::pair<std::int32_t, std::int32_t> です。各座標の範囲は $[-10000,10000]$ で、同じ点も複数渡せます。空の点列の戻り値は std::nullopt です。通常版の構築と判定で範囲外の座標を渡すと std::out_of_range を送出します。

点の処理順を seed でシャッフルします。処理順を一様に選ぶと期待 $O(N)$ 時間、最悪 $O(N^3)$ 時間です。追加領域は $O(N)$ です。

center

std::pair<double, double> center() const noexcept;

円の中心を {x, y} の近似値で返します。$O(1)$ 時間です。

radius_squared

double radius_squared() const noexcept;

半径の二乗の近似値を返します。$O(1)$ 時間です。

contains

bool contains(Point point) const;

点が円内または円周上にあれば真を返します。構築時と同じ座標範囲で、整数演算で厳密に判定します。$O(1)$ 時間です。

on_boundary

bool on_boundary(Point point) const;

点が円周上にあれば真を返します。入力範囲と判定精度は contains と共通です。$O(1)$ 時間です。

使用例 / Verify

ソースのAPI注記
minimum_enclosing_circle(points,seed=0): 整数点列を含む最小円。空ならnullopt。
各座標は[-10000,10000]。重複可。入力を変えない。乱択順で期待O(N)、最悪O(N³)時間・O(N)領域。
MinimumEnclosingCircle::center()/radius_squared(): 中心・半径二乗のdouble近似。O(1)。
MinimumEnclosingCircle::contains(p)/on_boundary(p): 同じ範囲の整数点を整数演算で厳密に判定。O(1)。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Minimum Enclosing Circle minimum_enclosing_circle.cpp ✓ 現行AC AC

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/minimum_enclosing_circle.md
//
// API一覧
// minimum_enclosing_circle(points,seed=0): 整数点列を含む最小円。空ならnullopt。
// 各座標は[-10000,10000]。重複可。入力を変えない。乱択順で期待O(N)、最悪O(N³)時間・O(N)領域。
// MinimumEnclosingCircle::center()/radius_squared(): 中心・半径二乗のdouble近似。O(1)。
// MinimumEnclosingCircle::contains(p)/on_boundary(p): 同じ範囲の整数点を整数演算で厳密に判定。O(1)。

// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <random>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>

// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_MINIMUM_ENCLOSING_CIRCLE_DEFINED
#define SIRKIT_MINIMUM_ENCLOSING_CIRCLE_DEFINED

class MinimumEnclosingCircle
{
public:
    using Point = std::pair<std::int32_t, std::int32_t>;

    [[nodiscard]] std::pair<double, double> center() const noexcept
    {
        const auto [x, y] = offset();
        return {origin_.first + x, origin_.second + y};
    }

    [[nodiscard]] double radius_squared() const noexcept
    {
        const auto [x, y] = offset();
        return x * x + y * y;
    }

    [[nodiscard]] bool contains(Point point) const
    {
        check_coordinates(point);
        return power(point) <= 0;
    }

    [[nodiscard]] bool on_boundary(Point point) const
    {
        check_coordinates(point);
        return power(point) == 0;
    }

private:
    Point origin_;
    std::int64_t x_, y_, denominator_;

    MinimumEnclosingCircle(Point origin, std::int64_t x, std::int64_t y, std::int64_t denominator) noexcept
        : origin_(origin), x_(x), y_(y), denominator_(denominator)
    {
    }

    static void check_coordinates([[maybe_unused]] Point point)
    {
#ifndef SKIP_RUNTIME_CHECKS
        constexpr std::int32_t kLimit = 10000;
        if (point.first < -kLimit || point.first > kLimit || point.second < -kLimit || point.second > kLimit)
        {
            throw std::out_of_range("minimum_enclosing_circle: coordinate out of range");
        }
#endif
    }

    [[nodiscard]] std::pair<double, double> offset() const noexcept
    {
        const auto denominator = static_cast<double>(denominator_);
        return {static_cast<double>(x_) / denominator, static_cast<double>(y_) / denominator};
    }

    [[nodiscard]] std::int64_t power(Point point) const noexcept
    {
        const auto dx = static_cast<std::int64_t>(point.first) - origin_.first;
        const auto dy = static_cast<std::int64_t>(point.second) - origin_.second;
        // origin_ は円周上。円の外・上・内を示す符号を、分母を掛けて求める。
        // 座標差は20000以下なので、全体の絶対値は24×20000^4以下。
        return denominator_ * (dx * dx + dy * dy) - 2 * (x_ * dx + y_ * dy);
    }

    static MinimumEnclosingCircle with_diameter(Point a, Point b) noexcept
    {
        return {a, static_cast<std::int64_t>(b.first) - a.first,
                static_cast<std::int64_t>(b.second) - a.second, 2};
    }

    static MinimumEnclosingCircle through_three(Point a, Point b, Point c) noexcept
    {
        const auto bx = static_cast<std::int64_t>(b.first) - a.first;
        const auto by = static_cast<std::int64_t>(b.second) - a.second;
        const auto cx = static_cast<std::int64_t>(c.first) - a.first;
        const auto cy = static_cast<std::int64_t>(c.second) - a.second;
        const auto b2 = bx * bx + by * by;
        const auto c2 = cx * cx + cy * cy;
        auto denominator = 2 * (bx * cy - by * cx);
        auto x = b2 * cy - c2 * by;
        auto y = bx * c2 - cx * b2;
        if (denominator < 0)
        {
            denominator = -denominator;
            x = -x;
            y = -y;
        }
        return {a, x, y, denominator};
    }

    friend std::optional<MinimumEnclosingCircle> minimum_enclosing_circle(std::span<const Point> points,
                                                                          std::uint64_t seed);
};

inline std::optional<MinimumEnclosingCircle> minimum_enclosing_circle(
    std::span<const MinimumEnclosingCircle::Point> points, std::uint64_t seed = 0)
{
    if (points.empty())
    {
        return std::nullopt;
    }
    using Circle = MinimumEnclosingCircle;
    std::vector<Circle::Point> shuffled;
    shuffled.reserve(points.size());
    for (const auto &point : points)
    {
        Circle::check_coordinates(point);
        shuffled.push_back(point);
    }
    std::mt19937_64 random(seed);
    std::ranges::shuffle(shuffled, random);

    auto circle = Circle::with_diameter(shuffled.front(), shuffled.front());
    for (std::size_t i = 1; i < shuffled.size(); ++i)
    {
        const auto a = shuffled[i];
        if (circle.power(a) <= 0)
        {
            continue;
        }
        circle = Circle::with_diameter(a, a);
        for (std::size_t j = 0; j < i; ++j)
        {
            const auto b = shuffled[j];
            if (circle.power(b) <= 0)
            {
                continue;
            }
            circle = Circle::with_diameter(a, b);
            for (std::size_t k = 0; k < j; ++k)
            {
                const auto c = shuffled[k];
                if (circle.power(c) > 0)
                {
                    // a,b を円周に置く制約には解がある。同一直線上で既存の円の外に
                    // ある点は、その制約のもとでは含められないので、この分岐に来ない。
                    circle = Circle::through_three(a, b, c);
                }
            }
        }
    }
    return circle;
}

#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page