From 762eb7933c5abe081081bfef952ae6fa6b51c495 Mon Sep 17 00:00:00 2001 From: sashi <82739042+sashi0034@users.noreply.github.com> Date: Sun, 17 Nov 2024 22:10:20 +0900 Subject: [PATCH] =?UTF-8?q?Math::ClampAngle=20=E8=BF=BD=E5=8A=A0=20(#1271)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Siv3D/include/Siv3D/Math.hpp | 22 ++++++++++++++++++++++ Siv3D/include/Siv3D/detail/Math.ipp | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/Siv3D/include/Siv3D/Math.hpp b/Siv3D/include/Siv3D/Math.hpp index 5c065c87d..bfd02845e 100644 --- a/Siv3D/include/Siv3D/Math.hpp +++ b/Siv3D/include/Siv3D/Math.hpp @@ -1321,6 +1321,28 @@ namespace s3d [[nodiscard]] inline constexpr Vec4 Smoothstep(Vec4 v) noexcept; + ////////////////////////////////////////////////// + // + // ClampAngle + // + ////////////////////////////////////////////////// + + /// @brief 最小角と最大角の範囲にクランプした角度を返します。 + /// @param angle クランプする角度(ラジアン) + /// @param min 範囲の最小角(ラジアン) + /// @param max 範囲の最大角(ラジアン) + /// @return クランプした角度(ラジアン) + /// @remark angle が min の方向と max の方向の間を指さない場合、より近いほうの角度を返します。 + [[nodiscard]] + inline float ClampAngle(float angle, float min, float max) noexcept; + + [[nodiscard]] + inline double ClampAngle(double angle, double min, double max) noexcept; + + SIV3D_CONCEPT_ARITHMETIC + [[nodiscard]] + inline double ClampAngle(Arithmetic angle, Arithmetic min, Arithmetic max) noexcept; + ////////////////////////////////////////////////// // // NormalizeAngle diff --git a/Siv3D/include/Siv3D/detail/Math.ipp b/Siv3D/include/Siv3D/detail/Math.ipp index 7aaf15eac..d2bf2b0da 100644 --- a/Siv3D/include/Siv3D/detail/Math.ipp +++ b/Siv3D/include/Siv3D/detail/Math.ipp @@ -1354,6 +1354,34 @@ namespace s3d SIV3D_MATH_FUNCTION_CONSTEXPR_X(Smoothstep) + ////////////////////////////////////////////////// + // + // ClampAngle + // + ////////////////////////////////////////////////// + + inline float ClampAngle(const float angle, const float min, float max) noexcept + { + const auto start = (min + max) * 0.5f - PiF; + const auto floor = Floor((angle - start) / TwoPiF) * TwoPiF; + return Clamp(angle, min + floor, max + floor); + } + + inline double ClampAngle(const double angle, const double min, double max) noexcept + { + const auto start = (min + max) * 0.5 - Pi; + const auto floor = Floor((angle - start) / TwoPi) * TwoPi; + return Clamp(angle, min + floor, max + floor); + } + + SIV3D_CONCEPT_ARITHMETIC_ + inline double ClampAngle(Arithmetic angle, Arithmetic min, Arithmetic max) noexcept + { + const auto start = (min + max) * 0.5 - Pi; + const auto floor = Floor((angle - start) / TwoPi) * TwoPi; + return Clamp(angle, min + floor, max + floor); + } + ////////////////////////////////////////////////// // // NormalizeAngle