Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math::ClampAngle 追加 #1271

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading