Claude Chappe' Curse - A C Game
Logo Institut d'Informatique Claude Chappe Logo Université de Le Mans Logo Raeptor Production
 
Loading...
Searching...
No Matches
Math Utilities

Bunch of math functions. More...

Macros

#define sqr(x)   ((x)*(x))
 Computes the square of a number.
 
#define cube(x)   ((x)*(x)*(x))
 Computes the cube of a number.
 
#define PI   3.14159265358979323846264338327
 The mathematical constant π (pi).
 
#define MIN(x, y)   ((x < y) ? x : y)
 Computes the minimum of two values.
 
#define MAX(x, y)   ((x > y) ? x : y)
 Computes the maximum of two values.
 
#define CLAMP(x, y, z)   (MIN(MAX(x,y),z))
 Clamps a value between a minimum and maximum range.
 

Functions

f32 frsqrt (f32 number)
 Computes the fast inverse square root of a given number using the "Fast Inverse Square Root" algorithm.
 
f32 to_radians (f32 angle)
 Converts an angle from degrees to radians.
 
f32 to_degrees (f32 angle)
 Converts an angle from radians to degrees.
 
f32 float_approach (f32 *const origin, const f32 dest, const f32 factor)
 Approaches a target float by a given factor.
 
float * vec3_approach (float *origin, float *dest, const f32 factor)
 Approaches a target vector by a given factor.
 

Detailed Description

Bunch of math functions.

Macro Definition Documentation

◆ sqr

#define sqr (   x)    ((x)*(x))

Computes the square of a number.

This macro takes a number and returns its square by multiplying the number by itself.

Parameters
xThe number to be squared.
Returns
The square of the input number.

◆ cube

#define cube (   x)    ((x)*(x)*(x))

Computes the cube of a number.

This macro takes a number and returns its cube by multiplying the number by itself twice.

Parameters
xThe number to be cubed.
Returns
The cube of the input number.

◆ PI

#define PI   3.14159265358979323846264338327

The mathematical constant π (pi).

This macro defines the value of π (pi) to a high degree of precision.

◆ MIN

#define MIN (   x,
 
)    ((x < y) ? x : y)

Computes the minimum of two values.

This macro takes two values and returns the smaller of the two.

Parameters
xThe first value.
yThe second value.
Returns
The smaller of the two input values.

◆ MAX

#define MAX (   x,
 
)    ((x > y) ? x : y)

Computes the maximum of two values.

This macro takes two values and returns the larger of the two.

Parameters
xThe first value.
yThe second value.
Returns
The larger of the two input values.

◆ CLAMP

#define CLAMP (   x,
  y,
 
)    (MIN(MAX(x,y),z))

Clamps a value between a minimum and maximum range.

This macro takes a value and ensures it falls within the specified range. If the value is less than the minimum, it returns the minimum. If the value is greater than the maximum, it returns the maximum. Otherwise, it returns the value itself.

Parameters
xThe minimum allowable value.
yThe value to be clamped.
zThe maximum allowable value.
Returns
The clamped value.

Function Documentation

◆ frsqrt()

f32 frsqrt ( f32  number)

Computes the fast inverse square root of a given number using the "Fast Inverse Square Root" algorithm.

Parameters
number{f32} The input number for which the inverse square root is to be calculated. Must be a positive number.
Returns
{f32} The approximate value of the inverse square root of the input number.

This function utilizes a clever bit manipulation technique to compute the fast inverse square root, originally popularized by its use in the Quake III Arena graphics engine. The algorithm performs an initial approximation using a magic constant and then refines the result using one iteration of Newton's method for improved accuracy.

The formula used for the approximation is as follows:

x = number
i = *(int*)&x        // Bit-level hacking to get integer representation
i = 0x5f3759df - (i >> 1) // The magic constant
x = *(float*)&i      // Convert back to float
x = x * (1.5 - (number * 0.5 * x * x)) // Newton's method refinement

This function is typically used in graphics and game development, where performance is critical, and is suitable for scenarios where the input number is non-negative.

◆ to_radians()

f32 to_radians ( f32  angle)

Converts an angle from degrees to radians.

Parameters
angle{f32} The angle in degrees to be converted.
Returns
{f32} The converted angle in radians.

This function takes an angle specified in degrees and converts it to radians using the formula:

radians = degrees * (PI / 180)

where PI is a constant representing the mathematical constant π. The function returns the equivalent angle in radians, allowing for easier interpretation and usage in applications where radians are preferred.

◆ to_degrees()

f32 to_degrees ( f32  angle)

Converts an angle from radians to degrees.

Parameters
angle{f32} The angle in radians to be converted.
Returns
{f32} The converted angle in degrees.

This function takes an angle specified in radians and converts it to degrees using the formula:

degrees = radians * (180 / PI)

where PI is a constant representing the mathematical constant π. The function returns the equivalent angle in degrees, allowing for easier interpretation and usage in applications where degrees are preferred.

◆ float_approach()

f32 float_approach ( f32 *const  origin,
const f32  dest,
const f32  factor 
)

Approaches a target float by a given factor.

Parameters
a{f32} The float to be approached.
b{f32} The target float.
t{f32} The approach factor.

This macro moves float a towards float b by a factor of t.

◆ vec3_approach()

float * vec3_approach ( float *  origin,
float *  dest,
const f32  factor 
)

Approaches a target vector by a given factor.

Parameters
a{vec3} The vector to be approached.
b{vec3} The target vector.
t{f32} The approach factor.

This macro moves vector a towards vector b by a factor of t.