8#ifndef DESIGNLAB_MATH_UTIL_H_
9#define DESIGNLAB_MATH_UTIL_H_
34template <
typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
44template <::std::
floating_po
int T>
45constexpr bool IsEqual(
const T num1,
const T num2)
noexcept
47 const T dif = num1 - num2;
58template <Arithmetic T>
59constexpr T
Squared(
const T num)
noexcept {
return num * num; }
67template <Arithmetic T>
73 return (a + b > c && b + c > a && c + a > b);
90 if (current == target) {
return current; }
92 return static_cast<T
>(current * (1 - rate) + target * rate);
101template <Arithmetic T>
106 std::random_device rd;
107 std::mt19937 gen(rd());
108 std::uniform_real_distribution<> dis(min, max);
116template <::std::
floating_po
int T>
125template <::std::
floating_po
int T>
134template <::std::
floating_po
int T>
160template <::std::
floating_po
int T>
162 const T num,
const int digit =
kDigit,
const int width =
kWidth)
164 std::ostringstream ss;
166 ss << std::fixed << std::setprecision(digit);
167 ss << std::setw(width) << std::setfill(
' ') << num;
T ApproachTarget(const T ¤t, const T &target, float rate)
目標値に値を近づける関数. 描画用なので,線形でなく,適当に値を近づける. そのため,計算に使いたいなら作り直すこと.
T LimitRangeAngleDeg(T angle)
角度を -180° ~ 180° の範囲に収める関数.
constexpr bool CanMakeTriangle(const T a, const T b, const T c) noexcept
3辺で三角形が作れるか調べる関数.
T GenerateRandomNumber(T min, T max)
指定した範囲内の乱数を生成する.
std::string FloatingPointNumToString(const T num, const int digit=kDigit, const int width=kWidth)
小数を文字列に変換する関数. C++ では C のフォーマットのように %3.3f とかで小数を文字列に変換できないため自作する.
constexpr int kDigit
小数点以下の桁数.
constexpr T ConvertRadToDeg(const T rad) noexcept
角度を [rad]から [deg] に変換する関数.
constexpr T ConvertDegToRad(const T deg) noexcept
角度を [deg] から [rad] に変換する関数.
constexpr bool IsEqual(const T num1, const T num2) noexcept
C++において,小数同士の計算は誤差が出てしまう. 誤差込みで値が等しいか調べる.
constexpr T Squared(const T num) noexcept
2乗した値を返す関数.
constexpr int kWidth
文字列の幅.
float 型と double 型の定数を提供するクラス.