shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: 偏角
(src/geometry/angle.hpp)

2 次元ベクトル $(x, y)$ の偏角を表す Angle<T> です。x 軸正方向を 0 として反時計回りに測り、周回数 turn と合わせて $2\pi \cdot \mathrm{turn} + [0, 2\pi)$ の値として比べます。零ベクトルは x 軸正方向と同じ偏角として扱います。外積で比べるので、整数型なら誤差なく比べられます。

std::vector<Vector<long long>> ps(n);
std::sort(ps.begin(), ps.end(), [](const auto& a, const auto& b) { return Angle(a) < Angle(b); });

std::vector<std::array<long long, 2>> qs(n);  // std::array や std::pair からも作れる
std::sort(qs.begin(), qs.end(), [](const auto& a, const auto& b) { return Angle(a) < Angle(b); });

Angle a(1, 1), b(-1, 0);
bool c = a < b;          // true (π/4 < π)
double r = b.radian();   // π

// 偏角が [θ_i, θ_i + π) にある点の数 (円環をまたぐ範囲は 2π を足した写しをつなげて数える)
std::vector<Angle<long long>> as(n);
for (auto& p : as) std::cin >> p;  // x y を読む
std::sort(as.begin(), as.end());
for (int i = 0; i < n; i++) as.push_back(as[i].next_turn());
long long cnt = std::lower_bound(as.begin(), as.end(), as[i].opposite()) - as.begin() - i;  // i 番目の点について

Angle d = Angle(0, 1) - Angle(1, 1);  // π/4 から π/2 へ反時計回りに回す角 (π/4)

和と差は複素数の積で求めるので、結果の座標は 2 つの座標の積の大きさになります。座標の絶対値が $10^9$ なら long long に収まりますが、その結果どうしを比べるには 4 乗が収まる型 (__int128 など) が要ります。$\pi$ や $2\pi$ をずらすだけなら、座標が大きくならない opposite() と next_turn() を使います。

メンバ関数

関数 説明 計算量
Angle() / Angle(x, y, turn = 0) 零ベクトル (偏角 0) / ベクトル $(x, y)$ の偏角に $2\pi \cdot \mathrm{turn}$ を足したもの $O(1)$
Angle(p) p[0]、p[1] (Vector<T, 2>、std::array<T, 2> など) か p.first、p.second (std::pair<T, T>) から作る $O(1)$
is_zero() 零ベクトルか $O(1)$
half() 周回数を除いた偏角が $[0, \pi)$ なら 0、$[\pi, 2\pi)$ なら 1 $O(1)$
radian() 偏角をラジアンで返す (double、$2\pi \cdot \mathrm{turn} + [0, 2\pi)$) $O(1)$
next_turn() $2\pi$ を足した偏角 $O(1)$
opposite() $\pi$ を足した偏角 (逆向きのベクトル)。零ベクトルは assert で落ちる $O(1)$
-a 符号を反転した偏角 (x 軸について折り返したベクトル) $O(1)$
a + b、a - b、+=、-= 偏角の和・差。b - a は a から b へ反時計回りに回す角。零ベクトルは偏角 0 として扱う $O(1)$
<、<=、>、>=、==、!= 周回数を含めた偏角で比べる。== は偏角が等しいか (向きが同じなら長さが違っても等しい) $O(1)$
is >> a / os << a x y を読む (周回数は 0 にする) / 書く (周回数は書かない) $O(1)$

メンバ変数

変数 説明
x / y ベクトルの座標
turn 周回数 (既定は 0)

外積を計算するので、座標の絶対値の 2 乗が T に収まる必要があります。

関連

Verified with

Code

#pragma once

/**
 * @file angle.hpp
 * @brief 偏角
 */

#include <cassert>
#include <cmath>
#include <istream>
#include <ostream>
#include <type_traits>
#include <utility>

/**
 * @brief 2 次元ベクトル (x, y) の偏角
 *
 * x 軸正方向を 0 として反時計回りに測り、周回数 turn と合わせて 2π * turn + [0, 2π) の値として比べる。零ベクトルは x 軸正方向と同じ偏角として扱う
 * 外積で比べるので、整数型なら誤差なく比べられる (座標の絶対値の 2 乗が T に収まる必要がある)
 *
 * std::sort(ps.begin(), ps.end(), [](const auto& a, const auto& b) { return Angle(a) < Angle(b); });
 *
 * @tparam T 座標の型
 */
template <typename T>
struct Angle {
    T x;                 //!< x 座標
    T y;                 //!< y 座標
    long long turn = 0;  //!< 周回数

    /**
     * @brief 偏角 0 (零ベクトル)
     */
    constexpr Angle() noexcept : x(), y() {}
    /**
     * @brief ベクトル (x, y) の偏角に 2π * turn を足したもの
     */
    constexpr Angle(T x, T y, long long turn = 0) noexcept : x(x), y(y), turn(turn) {}
    /**
     * @brief p[0], p[1] を x, y とする (Vector<T, 2> や std::array<T, 2> から作る)
     */
    template <typename P, typename = decltype(std::declval<const P&>()[0])>
    constexpr explicit Angle(const P& p) noexcept : x(p[0]), y(p[1]) {}
    /**
     * @brief p.first, p.second を x, y とする
     */
    constexpr explicit Angle(const std::pair<T, T>& p) noexcept : x(p.first), y(p.second) {}

    /**
     * @brief 零ベクトルか
     */
    constexpr bool is_zero() const noexcept { return x == 0 && y == 0; }
    /**
     * @brief 周回数を除いた偏角が [0, π) なら 0、[π, 2π) なら 1
     */
    constexpr int half() const noexcept { return y < 0 || (y == 0 && x < 0); }
    /**
     * @brief 偏角 (ラジアン、2π * turn + [0, 2π))
     */
    double radian() const {
        const double pi = std::acos(-1.0);
        double t = std::atan2(static_cast<double>(y), static_cast<double>(x));
        return (t < 0 ? t + 2 * pi : t) + 2 * pi * static_cast<double>(turn);
    }
    /**
     * @brief 2π を足した偏角
     */
    constexpr Angle next_turn() const noexcept { return Angle(x, y, turn + 1); }
    /**
     * @brief π を足した偏角 (逆向きのベクトル)。零ベクトルには使えない
     */
    constexpr Angle opposite() const noexcept {
        assert(!is_zero() && "Angle::opposite: zero vector");
        return Angle(-x, -y, turn + half());
    }

    /**
     * @brief 符号を反転した偏角 (x 軸について折り返したベクトル)
     */
    constexpr Angle operator-() const noexcept {
        // 周回数を除いた偏角が 0 なら -2π * turn、そうでなければ 2π を足して [0, 2π) に収める
        bool on_zero = y == 0 && x >= 0;
        return Angle(x, -y, -turn - !on_zero);
    }
    /**
     * @brief 偏角の和 (複素数としての積)
     *
     * 座標は a と b の座標の積の大きさになる。零ベクトルは偏角 0 として扱い、もう一方のベクトルを返す
     */
    friend constexpr Angle operator+(const Angle& a, const Angle& b) noexcept {
        if (a.is_zero()) return Angle(b.x, b.y, a.turn + b.turn);
        if (b.is_zero()) return Angle(a.x, a.y, a.turn + b.turn);
        Angle c(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x, a.turn + b.turn);
        // 周回数を除いた偏角の和が 2π 以上なら繰り上げる
        // 両方 [π, 2π) なら必ず、片方だけなら和は [π, 3π) なので結果が [0, π) のとき
        if (a.half() && b.half()) c.turn++;
        if (a.half() != b.half() && c.half() == 0) c.turn++;
        return c;
    }
    /**
     * @brief 偏角の差 (a から b へ反時計回りに回す角は b - a)
     */
    friend constexpr Angle operator-(const Angle& a, const Angle& b) noexcept { return a + (-b); }
    constexpr Angle& operator+=(const Angle& rhs) noexcept { return *this = *this + rhs; }
    constexpr Angle& operator-=(const Angle& rhs) noexcept { return *this = *this - rhs; }

    friend constexpr bool operator<(const Angle& a, const Angle& b) noexcept {
        if (a.turn != b.turn) return a.turn < b.turn;
        // 零ベクトルは外積で比べられないので、偏角 0 (x 軸正方向) として先に判定する
        if (b.is_zero()) return false;                   // 偏角 0 より小さいものはない
        if (a.is_zero()) return !(b.y == 0 && b.x > 0);  // b の偏角が 0 より大きいか
        if (a.half() != b.half()) return a.half() < b.half();
        return b.x * a.y < a.x * b.y;
    }
    friend constexpr bool operator>(const Angle& a, const Angle& b) noexcept { return b < a; }
    friend constexpr bool operator<=(const Angle& a, const Angle& b) noexcept { return !(b < a); }
    friend constexpr bool operator>=(const Angle& a, const Angle& b) noexcept { return !(a < b); }
    /**
     * @brief 偏角が等しいか (ベクトルとして等しいかではない)
     */
    friend constexpr bool operator==(const Angle& a, const Angle& b) noexcept { return !(a < b) && !(b < a); }
    friend constexpr bool operator!=(const Angle& a, const Angle& b) noexcept { return !(a == b); }

    /**
     * @brief x, y を読む (周回数は 0 にする)
     */
    template <class CharT, class Traits>
    friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, Angle& rhs) {
        rhs.turn = 0;
        return is >> rhs.x >> rhs.y;
    }
    /**
     * @brief x, y を空白区切りで書く (周回数は書かない)
     */
    template <class CharT, class Traits>
    friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const Angle& rhs) {
        return os << rhs.x << CharT(' ') << rhs.y;
    }
};

template <typename P, typename = decltype(std::declval<const P&>()[0])>
Angle(const P&) -> Angle<std::remove_cv_t<std::remove_reference_t<decltype(std::declval<const P&>()[0])>>>;
template <typename T>
Angle(const std::pair<T, T>&) -> Angle<T>;
#line 2 "src/geometry/angle.hpp"

/**
 * @file angle.hpp
 * @brief 偏角
 */

#include <cassert>
#include <cmath>
#include <istream>
#include <ostream>
#include <type_traits>
#include <utility>

/**
 * @brief 2 次元ベクトル (x, y) の偏角
 *
 * x 軸正方向を 0 として反時計回りに測り、周回数 turn と合わせて 2π * turn + [0, 2π) の値として比べる。零ベクトルは x 軸正方向と同じ偏角として扱う
 * 外積で比べるので、整数型なら誤差なく比べられる (座標の絶対値の 2 乗が T に収まる必要がある)
 *
 * std::sort(ps.begin(), ps.end(), [](const auto& a, const auto& b) { return Angle(a) < Angle(b); });
 *
 * @tparam T 座標の型
 */
template <typename T>
struct Angle {
    T x;                 //!< x 座標
    T y;                 //!< y 座標
    long long turn = 0;  //!< 周回数

    /**
     * @brief 偏角 0 (零ベクトル)
     */
    constexpr Angle() noexcept : x(), y() {}
    /**
     * @brief ベクトル (x, y) の偏角に 2π * turn を足したもの
     */
    constexpr Angle(T x, T y, long long turn = 0) noexcept : x(x), y(y), turn(turn) {}
    /**
     * @brief p[0], p[1] を x, y とする (Vector<T, 2> や std::array<T, 2> から作る)
     */
    template <typename P, typename = decltype(std::declval<const P&>()[0])>
    constexpr explicit Angle(const P& p) noexcept : x(p[0]), y(p[1]) {}
    /**
     * @brief p.first, p.second を x, y とする
     */
    constexpr explicit Angle(const std::pair<T, T>& p) noexcept : x(p.first), y(p.second) {}

    /**
     * @brief 零ベクトルか
     */
    constexpr bool is_zero() const noexcept { return x == 0 && y == 0; }
    /**
     * @brief 周回数を除いた偏角が [0, π) なら 0、[π, 2π) なら 1
     */
    constexpr int half() const noexcept { return y < 0 || (y == 0 && x < 0); }
    /**
     * @brief 偏角 (ラジアン、2π * turn + [0, 2π))
     */
    double radian() const {
        const double pi = std::acos(-1.0);
        double t = std::atan2(static_cast<double>(y), static_cast<double>(x));
        return (t < 0 ? t + 2 * pi : t) + 2 * pi * static_cast<double>(turn);
    }
    /**
     * @brief 2π を足した偏角
     */
    constexpr Angle next_turn() const noexcept { return Angle(x, y, turn + 1); }
    /**
     * @brief π を足した偏角 (逆向きのベクトル)。零ベクトルには使えない
     */
    constexpr Angle opposite() const noexcept {
        assert(!is_zero() && "Angle::opposite: zero vector");
        return Angle(-x, -y, turn + half());
    }

    /**
     * @brief 符号を反転した偏角 (x 軸について折り返したベクトル)
     */
    constexpr Angle operator-() const noexcept {
        // 周回数を除いた偏角が 0 なら -2π * turn、そうでなければ 2π を足して [0, 2π) に収める
        bool on_zero = y == 0 && x >= 0;
        return Angle(x, -y, -turn - !on_zero);
    }
    /**
     * @brief 偏角の和 (複素数としての積)
     *
     * 座標は a と b の座標の積の大きさになる。零ベクトルは偏角 0 として扱い、もう一方のベクトルを返す
     */
    friend constexpr Angle operator+(const Angle& a, const Angle& b) noexcept {
        if (a.is_zero()) return Angle(b.x, b.y, a.turn + b.turn);
        if (b.is_zero()) return Angle(a.x, a.y, a.turn + b.turn);
        Angle c(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x, a.turn + b.turn);
        // 周回数を除いた偏角の和が 2π 以上なら繰り上げる
        // 両方 [π, 2π) なら必ず、片方だけなら和は [π, 3π) なので結果が [0, π) のとき
        if (a.half() && b.half()) c.turn++;
        if (a.half() != b.half() && c.half() == 0) c.turn++;
        return c;
    }
    /**
     * @brief 偏角の差 (a から b へ反時計回りに回す角は b - a)
     */
    friend constexpr Angle operator-(const Angle& a, const Angle& b) noexcept { return a + (-b); }
    constexpr Angle& operator+=(const Angle& rhs) noexcept { return *this = *this + rhs; }
    constexpr Angle& operator-=(const Angle& rhs) noexcept { return *this = *this - rhs; }

    friend constexpr bool operator<(const Angle& a, const Angle& b) noexcept {
        if (a.turn != b.turn) return a.turn < b.turn;
        // 零ベクトルは外積で比べられないので、偏角 0 (x 軸正方向) として先に判定する
        if (b.is_zero()) return false;                   // 偏角 0 より小さいものはない
        if (a.is_zero()) return !(b.y == 0 && b.x > 0);  // b の偏角が 0 より大きいか
        if (a.half() != b.half()) return a.half() < b.half();
        return b.x * a.y < a.x * b.y;
    }
    friend constexpr bool operator>(const Angle& a, const Angle& b) noexcept { return b < a; }
    friend constexpr bool operator<=(const Angle& a, const Angle& b) noexcept { return !(b < a); }
    friend constexpr bool operator>=(const Angle& a, const Angle& b) noexcept { return !(a < b); }
    /**
     * @brief 偏角が等しいか (ベクトルとして等しいかではない)
     */
    friend constexpr bool operator==(const Angle& a, const Angle& b) noexcept { return !(a < b) && !(b < a); }
    friend constexpr bool operator!=(const Angle& a, const Angle& b) noexcept { return !(a == b); }

    /**
     * @brief x, y を読む (周回数は 0 にする)
     */
    template <class CharT, class Traits>
    friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, Angle& rhs) {
        rhs.turn = 0;
        return is >> rhs.x >> rhs.y;
    }
    /**
     * @brief x, y を空白区切りで書く (周回数は書かない)
     */
    template <class CharT, class Traits>
    friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const Angle& rhs) {
        return os << rhs.x << CharT(' ') << rhs.y;
    }
};

template <typename P, typename = decltype(std::declval<const P&>()[0])>
Angle(const P&) -> Angle<std::remove_cv_t<std::remove_reference_t<decltype(std::declval<const P&>()[0])>>>;
template <typename T>
Angle(const std::pair<T, T>&) -> Angle<T>;
Back to top page