Skip to content

Commit

Permalink
BLUGA: Add maths functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Nov 4, 2023
1 parent 01750d2 commit 79aae2a
Showing 1 changed file with 90 additions and 65 deletions.
155 changes: 90 additions & 65 deletions libs/B-luga/include/B-luga/Maths/Maths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Maths {
static constexpr int DECIMALS_TO_CONSERVE = 2;

/**
* @brief Calculates a multiplier based on the DECIMALS_TO_CONSERVE constant.
* @return The calculated multiplier.
* @example If DECIMALS_TO_CONSERVE is 2, the function returns 100.
* @brief Calculates a multiplier based on the DECIMALS_TO_CONSERVE constant.
* @return The calculated multiplier.
* @example If DECIMALS_TO_CONSERVE is 2, the function returns 100.
*/
static constexpr int getMultiplier()
{
Expand All @@ -36,11 +36,11 @@ class Maths {
}

/**
* @brief convert a float to an int conserving the x first decimals
* @param normalFloat the float to convert
* @return int
* @example 99.99 -> 9999 for DECIMALS_TO_CONSERVE = 2
*
* @brief convert a float to an int conserving the x first decimals
* @param normalFloat the float to convert
* @return int
* @example 99.99 -> 9999 for DECIMALS_TO_CONSERVE = 2
*
*/
static int floatToIntConservingDecimals(const float normalFloat)
{
Expand All @@ -50,11 +50,11 @@ class Maths {
}

/**
* @brief convert an int to a float with x decimals
* @param decimalInt the int to convert
* @return float
* @example 9999 -> 99.99 for DECIMALS_TO_CONSERVE = 2
*
* @brief convert an int to a float with x decimals
* @param decimalInt the int to convert
* @return float
* @example 9999 -> 99.99 for DECIMALS_TO_CONSERVE = 2
*
*/
static float intToFloatConservingDecimals(const int decimalInt)
{
Expand All @@ -64,36 +64,36 @@ class Maths {
}

/**
* @brief remove the decimals of an int
* @param decimalInt the int to modify
* @return int
* @example 9999 -> 99 for DECIMALS_TO_CONSERVE = 2
*
* @brief remove the decimals of an int
* @param decimalInt the int to modify
* @return int
* @example 9999 -> 99 for DECIMALS_TO_CONSERVE = 2
*
*/
static int removeIntDecimals(const int decimalInt)
{
return decimalInt / getMultiplier();
}

/**
* @brief add the x decimals to an int
* @param normalInt the int to modify
* @return int with x decimals
* @example 99 -> 9900 for DECIMALS_TO_CONSERVE = 2
*
* @brief add the x decimals to an int
* @param normalInt the int to modify
* @return int with x decimals
* @example 99 -> 9900 for DECIMALS_TO_CONSERVE = 2
*
*/
static int addIntDecimals(const int normalInt)
{
return normalInt * getMultiplier();
}

/**
* @brief addition of two ints with x decimals
* @param decimalInt decimalInt the first int
* @param otherDecimalInt decimalInt the second int
* @return the result of the addition
* @example 9999 + 9999 -> 199.98 for DECIMALS_TO_CONSERVE = 2
*
* @brief addition of two ints with x decimals
* @param decimalInt decimalInt the first int
* @param otherDecimalInt decimalInt the second int
* @return the result of the addition
* @example 9999 + 9999 -> 199.98 for DECIMALS_TO_CONSERVE = 2
*
*/
static int additionWithTwoIntDecimals(const int decimalInt, const int otherDecimalInt)
{
Expand All @@ -104,12 +104,12 @@ class Maths {
}

/**
* @brief subtraction of two ints with x decimals
* @param minuend int the substraction minuend
* @param subtrahend int the substraction subtrahend
* @return the result of the subtraction
* @example 9999 - 9999 -> 0 for DECIMALS_TO_CONSERVE = 2
*
* @brief subtraction of two ints with x decimals
* @param minuend int the substraction minuend
* @param subtrahend int the substraction subtrahend
* @return the result of the subtraction
* @example 9999 - 9999 -> 0 for DECIMALS_TO_CONSERVE = 2
*
*/
static int subtractionWithTwoIntDecimals(const int minuend, const int subtrahend)
{
Expand All @@ -120,11 +120,11 @@ class Maths {
}

/**
* @brief multiplication of two ints with x decimals
* @param decimalInt decimalInt the first int
* @param otherDecimalInt decimalInt the first int
* @return the result of the multiplication
*
* @brief multiplication of two ints with x decimals
* @param decimalInt decimalInt the first int
* @param otherDecimalInt decimalInt the first int
* @return the result of the multiplication
*
*/
static int multiplicationWithTwoIntDecimals(const int decimalInt, const int otherDecimalInt)
{
Expand All @@ -135,11 +135,11 @@ class Maths {
}

/**
* @brief division of two int with x decimals
* @param dividend decimalInt the first int
* @param divisor decimalInt the first int
* @return the result of the division
*
* @brief division of two int with x decimals
* @param dividend decimalInt the first int
* @param divisor decimalInt the first int
* @return the result of the division
*
*/
static int divisionWithTwoIntDecimals(const int dividend, const int divisor)
{
Expand All @@ -154,50 +154,75 @@ class Maths {
}

/**
* @brief addition with a decimal int and a normal int
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalInt normalInt the normal int that will be added
* @return void
* @example 500 + 5 = 550 for DECIMALS_TO_CONSERVE = 2
* @brief addition with a decimal int and a normal int
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalInt normalInt the normal int that will be added
* @return void
* @example 500 + 5 = 550 for DECIMALS_TO_CONSERVE = 2
*/
static void addNormalIntToDecimalInt(int &decimalInt, const int normalInt)
{
decimalInt += addIntDecimals(normalInt);
}

/**
* @brief subtraction with a decimal int and a normal int
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalInt the normal int that will be subtracted
* @return void
* @example 550 - 5 = 500 for DECIMALS_TO_CONSERVE = 2
* @brief subtraction with a decimal int and a normal int
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalInt the normal int that will be subtracted
* @return void
* @example 550 - 5 = 500 for DECIMALS_TO_CONSERVE = 2
*/
static void subNormalIntToDecimalInt(int &decimalInt, const int normalInt)
{
decimalInt -= addIntDecimals(normalInt);
}

/**
* @brief add a float to an int with decimals
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalFloat the normal float that will be added
* @return nothing
* @example 500 + 5.5 = 555 for DECIMALS_TO_CONSERVE = 2
* @brief add a float to an int with decimals
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalFloat the normal float that will be added
* @return nothing
* @example 500 + 5.5 = 555 for DECIMALS_TO_CONSERVE = 2
*/
static void addFloatToDecimalInt(int &decimalInt, const float normalFloat)
{
decimalInt += floatToIntConservingDecimals(normalFloat);
}

/**
* @brief sub a float to an int with decimals
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalFloat the normal float that will be subtracted
* @return nothing
* @example 500 - 5.5 = 445 for DECIMALS_TO_CONSERVE = 2
* @brief sub a float to an int with decimals
* @param decimalInt decimalInt the decimal int that will be modified
* @param normalFloat the normal float that will be subtracted
* @return nothing
* @example 500 - 5.5 = 445 for DECIMALS_TO_CONSERVE = 2
*/
static void subFloatToDecimalInt(int &decimalInt, const float normalFloat)
{
decimalInt -= floatToIntConservingDecimals(normalFloat);
}

/**
* @brief get the angle from a vector
* @param x the x of the vector
* @param y the y of the vector
* @return float
*/
float getAngleFromVector(const float x, const float y)
{
float angle = atan2(y, x) * 180.0F / static_cast<float>(M_PI);
if (angle < 0) {
angle += 360;
}
return angle;
}

/**
* @brief convert degrees to radians
* @param degrees the degrees to convert
* @return float
*/
float degreesToRadians(const float degrees)
{
return degrees * static_cast<float>(M_PI) / 180;
}
};

0 comments on commit 79aae2a

Please sign in to comment.