2023-10-20 03:32:46 +00:00
|
|
|
///
|
|
|
|
/// \file include/scmp/Math.h
|
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2017-06-05
|
|
|
|
/// \brief Common math functions.
|
|
|
|
///
|
|
|
|
/// Arena defines a memory management backend for pre-allocating memory.
|
|
|
|
///
|
|
|
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
///
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
#ifndef SCSL_SCMP_MATH_H
|
|
|
|
#define SCSL_SCMP_MATH_H
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
namespace scmp {
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// MAX_RADIAN is a precomputed 2 * M_PI.
|
2023-10-20 03:32:46 +00:00
|
|
|
constexpr double MAX_RADIAN = 2 * M_PI;
|
2023-10-19 06:44:05 +00:00
|
|
|
constexpr double MIN_RADIAN = -2 * M_PI;
|
2023-10-20 09:59:36 +00:00
|
|
|
constexpr double PI_D = 3.141592653589793;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Roll m die of n sides, returning a vector of the dice.
|
|
|
|
std::vector<int> Die(int m, int n);
|
2023-10-20 03:32:46 +00:00
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
/// Roll m die of n sides, returning the total of the die.
|
|
|
|
int DieTotal(int m, int n);
|
2023-10-20 03:32:46 +00:00
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
/// Roll m die of n sides, and take the total of the top k die.
|
|
|
|
int BestDie(int k, int m, int n);
|
|
|
|
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Convert radians to degrees.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param rads the Angle in radians
|
|
|
|
/// \return the Angle in degrees.
|
2023-10-19 06:44:05 +00:00
|
|
|
float RadiansToDegreesF(float rads);
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Convert radians to degrees.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param rads the Angle in radians
|
|
|
|
/// \return the Angle in degrees.
|
2023-10-19 06:44:05 +00:00
|
|
|
double RadiansToDegreesD(double rads);
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Convert degrees to radians.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param degrees the Angle in degrees
|
|
|
|
/// \return the Angle in radians.
|
2023-10-19 06:44:05 +00:00
|
|
|
float DegreesToRadiansF(float degrees);
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Convert degrees to radians.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param degrees the Angle in degrees
|
|
|
|
/// \return the Angle in radians.
|
2023-10-19 06:44:05 +00:00
|
|
|
double DegreesToRadiansD(double degrees);
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief RotateRadians rotates theta0 by theta1 radians, wrapping
|
|
|
|
/// the result to MIN_RADIAN <= result <= MAX_RADIAN.
|
|
|
|
///
|
|
|
|
/// \param theta0
|
|
|
|
/// \param theta1
|
|
|
|
/// \return
|
2023-10-19 06:44:05 +00:00
|
|
|
double RotateRadians(double theta0, double theta1);
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Get the default epsilon value.
|
|
|
|
///
|
|
|
|
/// \param epsilon The variable to store the epsilon value in.
|
2023-10-19 06:44:05 +00:00
|
|
|
void DefaultEpsilon(double &epsilon);
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief Get the default epsilon value.
|
2023-10-20 09:59:36 +00:00
|
|
|
///
|
|
|
|
/// \param epsilon The variable to store the epsilon value in.
|
2023-10-19 06:44:05 +00:00
|
|
|
void DefaultEpsilon(float &epsilon);
|
|
|
|
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief Get the default epsilon for integer types.
|
|
|
|
///
|
|
|
|
/// \param epsilon The variable to store the epsilon value in.
|
|
|
|
void DefaultEpsilon(int& epsilon);
|
|
|
|
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
/// \brief Return whether the two values of type T are equal to within
|
|
|
|
/// some tolerance.
|
|
|
|
///
|
|
|
|
/// \tparam T The type of value
|
|
|
|
/// \param a A value of type T used as the left-hand side of an
|
|
|
|
/// equality check.
|
|
|
|
/// \param b A value of type T used as the right-hand side of an
|
|
|
|
/// equality check.
|
|
|
|
/// \param epsilon The tolerance value.
|
|
|
|
/// \return Whether the two values are "close enough" to be considered
|
|
|
|
/// equal.
|
2023-10-19 06:44:05 +00:00
|
|
|
template <typename T>
|
|
|
|
static T
|
|
|
|
WithinTolerance(T a, T b, T epsilon)
|
|
|
|
{
|
2023-10-20 09:59:36 +00:00
|
|
|
return std::abs(a - b) <= epsilon;
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-19 07:37:56 +00:00
|
|
|
} // namespace scmp
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-21 09:07:59 +00:00
|
|
|
#endif //SCSL_SCMP_MATH_H
|