-
-
Notifications
You must be signed in to change notification settings - Fork 140
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
Reputeless
merged 4 commits into
Siv3D:v6_develop
from
sashi0034:feature/add_ClampAngle
Nov 17, 2024
Merged
Math::ClampAngle 追加 #1271
Reputeless
merged 4 commits into
Siv3D:v6_develop
from
sashi0034:feature/add_ClampAngle
Nov 17, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
失礼しました、先程の実装に誤りがあったので修正しました (上の動作確認用のコードは偶然 Clamp が成功していたようでした) # include <Siv3D.hpp> // Siv3D v0.6.15
void Main()
{
double angleRad{};
double minRad{};
double maxRad{Math::HalfPi};
while (System::Update())
{
constexpr double range = Math::TwoPi * 2;
SimpleGUI::Headline(U"angleRad: {:.4f}"_fmt(angleRad), {20, 20});
SimpleGUI::Slider(angleRad, -range, range, {200, 20}, 280);
SimpleGUI::Headline(U"minRad: {:.4f}"_fmt(minRad), {20, 60});
SimpleGUI::Slider(minRad, -range, range, {200, 60}, 280);
SimpleGUI::Headline(U"maxRad: {:.4f}"_fmt(maxRad), {20, 100});
SimpleGUI::Slider(maxRad, -range, range, {200, 100}, 280);
const double clampedRad = Math::ClampAngle(angleRad, minRad, maxRad);
SimpleGUI::Headline(U"ClampAngle(angleRad, minRad, maxRad) -> {:.4f}"_fmt(clampedRad), {20, 140});
Circle(Scene::Center(), 100)
.drawPie(minRad, maxRad - minRad, Palette::Gray)
.drawFrame(1.0, Palette::White);
Line{Scene::Center(), Circular{100, clampedRad}.toVec2() + Scene::Center()}.draw(5.0, Palette::Yellow);
Line{Scene::Center(), Circular{100, angleRad}.toVec2() + Scene::Center()}.draw(2.0, Palette::Red);
Line{Scene::Center(), Circular{100, minRad}.toVec2() + Scene::Center()}.draw(2.0, Palette::Green);
Line{Scene::Center(), Circular{100, maxRad}.toVec2() + Scene::Center()}.draw(2.0, Palette::Blue);
}
} |
ありがとうございます。近日中に確認します。 |
追加の計算コストが発生しますが、 double ClampAngle2(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 NormalizeAngle(Clamp(angle, min + floor, max + floor), (min + max) * 0.5);
} のように 一方で失われる情報もあるので、そうしたい場合はユーザが自前で Print << Math::ToDegrees(Math::ClampAngle(-3610_deg, -60_deg, 60_deg)); // -3610
Print << Math::ToDegrees(Math::ClampAngle2(-3610_deg, -60_deg, 60_deg)); // -10
Print << Math::ToDegrees(Math::ClampAngle(-350_deg, -60_deg, 60_deg)); // -350
Print << Math::ToDegrees(Math::ClampAngle2(-350_deg, -60_deg, 60_deg)); // 10
Print << Math::ToDegrees(Math::ClampAngle(10_deg, 340_deg, 380_deg)); // 10
Print << Math::ToDegrees(Math::ClampAngle2(10_deg, 340_deg, 380_deg)); // 370
Print << Math::ToDegrees(Math::ClampAngle(90_deg, 340_deg, 380_deg)); // 20
Print << Math::ToDegrees(Math::ClampAngle2(90_deg, 340_deg, 380_deg)); // 380 ↓ クランプ範囲が 2 π 以上のとき Print << Math::ToDegrees(Math::ClampAngle(10_deg, 380_deg, 3580_deg)); // 10
Print << Math::ToDegrees(Math::ClampAngle2(10_deg, 380_deg, 3580_deg)); // 1810 |
|
Merged. Thank you! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Discord 文脈: https://discord.com/channels/443310697397354506/999983621408567326/1305003589588226211
最小値と最大値の範囲にクランプした角度を返す Math::ClampAngle を実装しました。
検索エンジンで ClampAngle を検索したとき上位に出てくる以下の実装を参考にしています。
https://gist.github.com/johnsoncodehk/2ecb0136304d4badbb92bd0c1dbd8bae
以下の Main.cpp で動作を確認できます。
実行結果