Skip to content

Commit

Permalink
Math::ClampAngle 追加 (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashi0034 authored Nov 17, 2024
1 parent 4a11000 commit 762eb79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Siv3D/include/Siv3D/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions Siv3D/include/Siv3D/detail/Math.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 762eb79

Please sign in to comment.