競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// geometry/angle.hpp の Angle を、atan2 で求めた偏角と比べて確かめる
// 問題の入力は使わず、すべて成り立ったら Hello World を出力する
#include "../../../src/geometry/angle.hpp"
#include <array>
#include <cmath>
#include <iostream>
#include <sstream>
#include <utility>
#include <vector>
#include "../../../src/geometry/vector.hpp"
int main() {
bool ok = true;
const double pi = std::acos(-1.0);
// 偏角を [0, 2π) で返す。零ベクトルは 0
auto arg = [&](int x, int y) {
double t = std::atan2(static_cast<double>(y), static_cast<double>(x));
return t < 0 ? t + 2 * pi : t;
};
std::vector<std::pair<int, int>> ps;
for (int x = -4; x <= 4; x++) {
for (int y = -4; y <= 4; y++) ps.emplace_back(x, y);
}
for (auto [ax, ay] : ps) {
Angle<long long> a(ax, ay);
ok = ok && std::abs(a.radian() - arg(ax, ay)) < 1e-12;
ok = ok && a.is_zero() == (ax == 0 && ay == 0);
ok = ok && a.half() == (arg(ax, ay) >= pi - 1e-12);
for (auto [bx, by] : ps) {
Angle<long long> b(bx, by);
double ta = arg(ax, ay), tb = arg(bx, by);
bool lt = ta < tb - 1e-12, eq = std::abs(ta - tb) < 1e-12;
ok = ok && (a < b) == lt && (b > a) == lt;
ok = ok && (a == b) == eq && (a != b) == !eq;
ok = ok && (a <= b) == (lt || eq) && (b >= a) == (lt || eq);
}
}
// 周回数を付けたときの比較、和・差・符号反転・opposite・next_turn を radian と比べる
for (auto [ax, ay] : ps) {
for (int at = -1; at <= 1; at++) {
Angle<long long> a(ax, ay, at);
double ta = a.radian();
ok = ok && std::abs(ta - (arg(ax, ay) + 2 * pi * at)) < 1e-9;
ok = ok && std::abs(a.next_turn().radian() - (ta + 2 * pi)) < 1e-9;
ok = ok && std::abs((-a).radian() + ta) < 1e-9;
if (!a.is_zero()) ok = ok && std::abs(a.opposite().radian() - (ta + pi)) < 1e-9;
for (auto [bx, by] : ps) {
for (int bt = -1; bt <= 1; bt++) {
Angle<long long> b(bx, by, bt);
double tb = b.radian();
bool lt = ta < tb - 1e-9, eq = std::abs(ta - tb) < 1e-9;
ok = ok && (a < b) == lt && (a == b) == eq;
ok = ok && std::abs((a + b).radian() - (ta + tb)) < 1e-9;
ok = ok && std::abs((a - b).radian() - (ta - tb)) < 1e-9;
Angle<long long> c = a;
c += b;
ok = ok && c == a + b;
c -= b;
ok = ok && c == a;
}
}
}
}
// [θ, θ + π) の判定は a <= b && b < a.opposite() でも b - a < π でも書ける
{
Angle<long long> a(1, 1), on(2, 2), in(-1, 1), out(-1, -1), back(-3, -3);
ok = ok && a <= on && on < a.opposite() && a <= in && in < a.opposite();
ok = ok && !(back < a.opposite()) && !(out < a.opposite());
ok = ok && on - a < Angle<long long>(-1, 0) && in - a < Angle<long long>(-1, 0);
ok = ok && !(back - a < Angle<long long>(-1, 0)) && !(out - a < Angle<long long>(-1, 0));
}
// 入出力は x y (周回数は読み書きしない)
{
std::istringstream is("3 -4 -5 6");
Angle<long long> a(0, 0, 5), b;
is >> a >> b;
ok = ok && a.x == 3 && a.y == -4 && a.turn == 0 && b.x == -5 && b.y == 6;
std::ostringstream os;
os << Angle<long long>(-1, 2, 3);
ok = ok && os.str() == "-1 2";
}
// Vector、std::array、std::pair から作れる
Vector<long long> v(-3, 2);
std::array<long long, 2> w{-3, 2};
std::pair<long long, long long> p(-3, 2);
Angle av(v), aw(w), ap(p);
ok = ok && av.x == -3 && av.y == 2 && aw.x == -3 && aw.y == 2 && ap.x == -3 && ap.y == 2;
ok = ok && av == Angle<long long>(-6, 4);
// 浮動小数点型でも使える
ok = ok && Angle(1.0, 1.0) < Angle(-1.0, 0.0) && !(Angle(0.0, 0.0) < Angle(2.0, 0.0));
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}#line 1 "test/unittest/geometry/angle.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// geometry/angle.hpp の Angle を、atan2 で求めた偏角と比べて確かめる
// 問題の入力は使わず、すべて成り立ったら Hello World を出力する
#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>;
#line 7 "test/unittest/geometry/angle.test.cpp"
#include <array>
#line 10 "test/unittest/geometry/angle.test.cpp"
#include <iostream>
#include <sstream>
#line 13 "test/unittest/geometry/angle.test.cpp"
#include <vector>
#line 2 "src/geometry/vector.hpp"
/**
* @file vector.hpp
* @brief 幾何ベクトル
*/
#line 15 "src/geometry/vector.hpp"
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#include <cstddef>
#include <functional>
#include <iterator>
#include <set>
#line 14 "src/utility/traits.hpp"
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 17 "src/geometry/vector.hpp"
/**
* @brief 幾何学としてのベクトル
*
* @tparam T 型 省略したらdouble
* @tparam Dim 次元 省略したら2
*/
template <typename T = double, int Dim = 2, ENABLE_IF_T(std::is_scalar_v<T> && (Dim > 0))>
struct Vector {
std::array<T, Dim> v; //!< ベクトルの成分表示
/**
* @brief デフォルトコンストラクタ
* ゼロベクトル
*/
constexpr Vector() noexcept : v() {}
/**
* @brief 可変長引数によるコンストラクタ
*
* 各成分を T に変換して入れる (Vector(1, 2.5) のように型が混ざっても縮小変換の警告を出さない)
* @tparam Args
*/
template <typename... Args>
constexpr Vector(Args... args) noexcept : v{static_cast<T>(args)...} {}
/**
* @brief i番目の成分を返す
*
* @param i
* @return T&
*/
constexpr T& operator[](long long i) noexcept {
return v[i];
}
/**
* @brief i番目の成分を返す
*
* @param i
* @return T&
*/
constexpr const T& operator[](long long i) const noexcept {
return v[i];
}
/**
* @brief 加算代入演算子
*
* @param rhs
* @return Vector&
*/
constexpr Vector& operator+=(const Vector& rhs) noexcept {
for (int i = 0; i < Dim; i++) v[i] += rhs[i];
return *this;
}
/**
* @brief 減算代入演算子
*
* @param rhs
* @return Vector&
*/
constexpr Vector& operator-=(const Vector& rhs) noexcept {
for (int i = 0; i < Dim; i++) v[i] -= rhs.v[i];
return *this;
}
/**
* @brief スカラー倍代入演算子
*
* @param coef
* @return Vector&
*/
constexpr Vector& operator*=(const T& coef) noexcept {
for (int i = 0; i < Dim; i++) v[i] *= coef;
return *this;
}
/**
* @brief スカラーの逆数倍代入演算子
*
* @param coef
* @return Vector&
*/
constexpr Vector& operator/=(const T& coef) {
for (int i = 0; i < Dim; i++) {
if constexpr (std::is_integral_v<T>) {
assert(v[i] % coef == 0 && "Vector::operator/= : coef must be a divisor of all elements");
}
v[i] /= coef;
}
return *this;
}
/**
* @brief 単項プラス演算子
*
* @return Vector
*/
[[nodiscard]]
constexpr Vector operator+() const noexcept {
return *this;
}
/**
* @brief 単項マイナス演算子
*
* @return Vector
*/
[[nodiscard]]
constexpr Vector operator-() const noexcept {
return *this * (-1);
}
/**
* @brief 加算演算子
*
* @param lhs
* @param rhs
* @return Vector
*/
[[nodiscard]]
friend constexpr Vector operator+(const Vector& lhs, const Vector& rhs) noexcept {
return std::move(Vector(lhs) += rhs);
}
/**
* @brief 減算演算子
*
* @param lhs
* @param rhs
* @return Vector
*/
[[nodiscard]]
friend constexpr Vector operator-(const Vector& lhs, const Vector& rhs) noexcept {
return std::move(Vector(lhs) -= rhs);
}
/**
* @brief スカラー倍演算子
*
* @param lhs
* @param coef
* @return Vector
*/
[[nodiscard]]
friend constexpr Vector operator*(const Vector& lhs, const T& coef) noexcept {
return std::move(Vector(lhs) *= coef);
}
/**
* @brief スカラー倍演算子
*
* @param coef
* @param rhs
* @return Vector
*/
[[nodiscard]]
friend constexpr Vector operator*(const T& coef, const Vector& rhs) noexcept {
return std::move(Vector(rhs) *= coef);
}
/**
* @brief スカラーの逆数倍演算子
*
* @param lhs
* @param coef
* @return Vector
*/
[[nodiscard]]
friend constexpr Vector operator/(const Vector& lhs, const T& coef) {
return std::move(Vector(lhs) /= coef);
}
/**
* @brief 等価演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator==(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v == rhs.v;
}
/**
* @brief 非等価演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator!=(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v != rhs.v;
}
/**
* @brief 小なり演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator<(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v < rhs.v;
}
/**
* @brief 大なり演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator>(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v > rhs.v;
}
/**
* @brief 小なりイコール演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator<=(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v <= rhs.v;
}
/**
* @brief 大なりイコール演算子
*
* @param lhs
* @param rhs
* @return bool
*/
[[nodiscard]]
friend constexpr bool operator>=(const Vector& lhs, const Vector& rhs) noexcept {
return lhs.v >= rhs.v;
}
/**
* @brief 入力ストリームからの読み込み
*
* @tparam CharT 入力ストリームの文字型
* @tparam Traits 入力ストリームの文字型特性
* @param is
* @param rhs
* @return std::basic_istream<CharT, Traits>&
*/
template <class CharT, class Traits>
friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, Vector& rhs) {
for (int i = 0; i < Dim; i++) is >> rhs.v[i];
return is;
}
/**
* @brief 出力ストリームへの書き込み
*
* @tparam CharT 出力ストリームの文字型
* @tparam Traits 出力ストリームの文字型特性
* @param is
* @param rhs
* @return std::basic_ostream<CharT, Traits>&
*/
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const Vector& rhs) {
for (int i = 0; i < Dim; i++) {
if (i != 0) os << CharT(' ');
os << rhs.v[i];
}
return os;
}
/**
* @brief 内積
*
* @param lhs
* @param rhs
* @return T
*/
friend constexpr T dot(const Vector& lhs, const Vector& rhs) noexcept {
T ret = 0;
for (int i = 0; i < Dim; i++) ret += lhs[i] * rhs[i];
return ret;
}
/**
* @brief 絶対値の2乗
*
* @return T
*/
constexpr T squared_norm() const noexcept {
return dot(*this, *this);
}
/**
* @brief 絶対値
*
* @return double
*/
constexpr double abs() const noexcept {
return std::sqrt(squared_norm());
}
};
/**
* @brief 成分から作るときの推論ガイド。型は成分の共通の型、次元は成分の個数にする
*
* Vector(2LL, 3LL) は Vector<long long, 2>、Vector(1, 2, 3) は Vector<int, 3>、Vector(0.0, 0.0) は Vector<double, 2>
* (これがないと、テンプレート引数の省略時の値から Vector<double, 2> になってしまう)
*/
template <typename T, typename... U, ENABLE_IF_T(std::is_scalar_v<T> && (std::is_scalar_v<U> && ...))>
Vector(T, U...) -> Vector<std::common_type_t<T, U...>, 1 + sizeof...(U)>;
/**
* @brief 二次元ベクトルどうしの外積(平行四辺形の符号付き面積)
*
* @param lhs
* @param rhs
* @tparam T 座標の型
* @return T
*/
template <typename T>
constexpr T cross(const Vector<T, 2>& lhs, const Vector<T, 2>& rhs) noexcept {
return lhs[0] * rhs[1] - lhs[1] * rhs[0];
}
/**
* @brief 三次元ベクトルどうしの外積
*
* @param lhs
* @param rhs
* @tparam T 座標の型
* @return Vector<T, 3>
*/
template <typename T>
constexpr Vector<T, 3> cross(const Vector<T, 3>& lhs, const Vector<T, 3>& rhs) noexcept {
Vector<T, 3> ret;
ret[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
ret[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
ret[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
return ret;
}
/**
* @brief 3点の位置関係 (counter-clockwise)
*
* 整数型でも使える。浮動小数点型では誤差を考えずに比較する
*
* @tparam T 座標の型
* @return int 1: a→b→c が反時計回り, -1: 時計回り, 2: c, a, b の順に一直線上, -2: a, b, c の順に一直線上, 0: c が線分 ab 上
*/
template <typename T>
constexpr int ccw(const Vector<T, 2>& a, const Vector<T, 2>& b, const Vector<T, 2>& c) {
Vector<T, 2> u = b - a, v = c - a;
T cr = cross(u, v);
if (cr > 0) return 1;
if (cr < 0) return -1;
if (dot(u, v) < 0) return 2;
if (u.squared_norm() < v.squared_norm()) return -2;
return 0;
}
#line 16 "test/unittest/geometry/angle.test.cpp"
int main() {
bool ok = true;
const double pi = std::acos(-1.0);
// 偏角を [0, 2π) で返す。零ベクトルは 0
auto arg = [&](int x, int y) {
double t = std::atan2(static_cast<double>(y), static_cast<double>(x));
return t < 0 ? t + 2 * pi : t;
};
std::vector<std::pair<int, int>> ps;
for (int x = -4; x <= 4; x++) {
for (int y = -4; y <= 4; y++) ps.emplace_back(x, y);
}
for (auto [ax, ay] : ps) {
Angle<long long> a(ax, ay);
ok = ok && std::abs(a.radian() - arg(ax, ay)) < 1e-12;
ok = ok && a.is_zero() == (ax == 0 && ay == 0);
ok = ok && a.half() == (arg(ax, ay) >= pi - 1e-12);
for (auto [bx, by] : ps) {
Angle<long long> b(bx, by);
double ta = arg(ax, ay), tb = arg(bx, by);
bool lt = ta < tb - 1e-12, eq = std::abs(ta - tb) < 1e-12;
ok = ok && (a < b) == lt && (b > a) == lt;
ok = ok && (a == b) == eq && (a != b) == !eq;
ok = ok && (a <= b) == (lt || eq) && (b >= a) == (lt || eq);
}
}
// 周回数を付けたときの比較、和・差・符号反転・opposite・next_turn を radian と比べる
for (auto [ax, ay] : ps) {
for (int at = -1; at <= 1; at++) {
Angle<long long> a(ax, ay, at);
double ta = a.radian();
ok = ok && std::abs(ta - (arg(ax, ay) + 2 * pi * at)) < 1e-9;
ok = ok && std::abs(a.next_turn().radian() - (ta + 2 * pi)) < 1e-9;
ok = ok && std::abs((-a).radian() + ta) < 1e-9;
if (!a.is_zero()) ok = ok && std::abs(a.opposite().radian() - (ta + pi)) < 1e-9;
for (auto [bx, by] : ps) {
for (int bt = -1; bt <= 1; bt++) {
Angle<long long> b(bx, by, bt);
double tb = b.radian();
bool lt = ta < tb - 1e-9, eq = std::abs(ta - tb) < 1e-9;
ok = ok && (a < b) == lt && (a == b) == eq;
ok = ok && std::abs((a + b).radian() - (ta + tb)) < 1e-9;
ok = ok && std::abs((a - b).radian() - (ta - tb)) < 1e-9;
Angle<long long> c = a;
c += b;
ok = ok && c == a + b;
c -= b;
ok = ok && c == a;
}
}
}
}
// [θ, θ + π) の判定は a <= b && b < a.opposite() でも b - a < π でも書ける
{
Angle<long long> a(1, 1), on(2, 2), in(-1, 1), out(-1, -1), back(-3, -3);
ok = ok && a <= on && on < a.opposite() && a <= in && in < a.opposite();
ok = ok && !(back < a.opposite()) && !(out < a.opposite());
ok = ok && on - a < Angle<long long>(-1, 0) && in - a < Angle<long long>(-1, 0);
ok = ok && !(back - a < Angle<long long>(-1, 0)) && !(out - a < Angle<long long>(-1, 0));
}
// 入出力は x y (周回数は読み書きしない)
{
std::istringstream is("3 -4 -5 6");
Angle<long long> a(0, 0, 5), b;
is >> a >> b;
ok = ok && a.x == 3 && a.y == -4 && a.turn == 0 && b.x == -5 && b.y == 6;
std::ostringstream os;
os << Angle<long long>(-1, 2, 3);
ok = ok && os.str() == "-1 2";
}
// Vector、std::array、std::pair から作れる
Vector<long long> v(-3, 2);
std::array<long long, 2> w{-3, 2};
std::pair<long long, long long> p(-3, 2);
Angle av(v), aw(w), ap(p);
ok = ok && av.x == -3 && av.y == 2 && aw.x == -3 && aw.y == 2 && ap.x == -3 && ap.y == 2;
ok = ok && av == Angle<long long>(-6, 4);
// 浮動小数点型でも使える
ok = ok && Angle(1.0, 1.0) < Angle(-1.0, 0.0) && !(Angle(0.0, 0.0) < Angle(2.0, 0.0));
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}