Math - Polkm105/DesignPatternPractice GitHub Wiki
PARTIAL
This section contains all functions relating to CP_Math
Table Of Contents
- CP_Math_ClampInt
- CP_Math_ClampFloat
- CP_Math_LerpInt
- CP_Math_LerpFloat
- CP_Math_Square
- CP_Math_Distance
- CP_Math_Degrees
- CP_Math_Radians
- CP_Math_ScreenToWorld
- CP_Math_WorldToScreen
CP_Math_ClampInt
Clamps an input int to an input int range and returns the clamped value.
Function
int CP_Math_ClampInt(int value, int min, int max);
Parameters
- value - (int) The value to clamp to the input range.
- min - (int) The minimum value in the range.
- max - (int) The maximum value in the range.
Return
int
Example
void update(void)
{
// Get the mouse position/canvas size ratio
float mx = (float)CP_Input_GetMouseWorldX()/(float)CP_System_GetWindowWidth();
float my = (float)CP_Input_GetMouseWorldY()/(float)CP_System_GetWindowHeight();
// Convert to 0-255 values for color
int r_color = (int)(mx * 255);
int b_color = (int)(my * 255);
// Clamp the values
r_color = CP_Math_ClampInt(r_color, 0, 255);
b_color = CP_Math_ClampInt(b_color, 0, 255);
// Set the background as the color
CP_Graphics_ClearBackground(CP_Color_Create(r_color, 0, b_color, 255));
}
Related
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Input_GetMouseWorldX
- CP_Input_GetMouseWorldY
- CP_System_GetWindowWidth
- CP_System_GetWindowHeight
CP_Math_ClampFloat
Clamps an input float to an input float range and returns the clamped value.
Function
float CP_Math_ClampFloat(float value, float min, float max);
Parameters
- value - (int) The value to clamp to the input range.
- min - (int) The minimum value in the range.
- max - (int) The maximum value in the range.
Return
float
Example
static CP_Color Red;
static CP_Color Green;
static CP_Color Blue;
static CP_Color White;
void init(void)
{
// Create colors for the four corners of the screen
Red = CP_Color_Create(255, 0, 0, 255);
Green = CP_Color_Create(0, 255, 0, 255);
Blue = CP_Color_Create(0, 0, 255, 255);
White = CP_Color_Create(255, 255, 255, 255);
}
void update(void)
{
// Get the mouse position
float mx = (float)CP_Input_GetMouseWorldX()/(float)CP_System_GetWindowWidth();
float my = (float)CP_Input_GetMouseWorldY()/(float)CP_System_GetWindowHeight();
// Clamp the values
mx = CP_Math_ClampFloat(mx, 0.0, 1.0);
my = CP_Math_ClampFloat(my, 0.0, 1.0);
// Lerp along both axis
CP_Color lerpx = CP_Color_Lerp(Red, Blue, mx);
CP_Color lerpy = CP_Color_Lerp(Green, White, my);
// Lerp them together
CP_Color lerp = CP_Color_Lerp(lerpx, lerpy, 0.5f);
// Set the background based on the lerp
CP_Graphics_ClearBackground(lerp);
}
Related
- CP_Color
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Color_Lerp
- CP_Input_GetMouseWorldX
- CP_Input_GetMouseWorldY
- CP_System_GetWindowWidth
- CP_System_GetWindowHeight
CP_Math_LerpInt
Linearly interpolates between two input integers with an input lerp factor.
Function
int CP_Math_LerpInt(int a, int b, float lerpFactor);
Parameters
- a - (int) The value the function returns when lerp_factor is 0.0, the starting value.
- b - (int) The value the function returns when lerp_factor is 1.0, the ending value.
- lerp_factor - (float) The ratio used to determine what value to return between a and b.
Return
int
Example
void update(void)
{
// Get the mouse position/canvas size ratio
float mx = (float)CP_Input_GetMouseWorldX()/(float)CP_System_GetWindowWidth();
float my = (float)CP_Input_GetMouseWorldY()/(float)CP_System_GetWindowHeight();
// Convert to 0-255 values for color
int r_color = CP_Math_LerpInt(0, 255, mx);
int b_color = CP_Math_LerpInt(0, 255, my);
// Clamp the values
r_color = CP_Math_ClampInt(r_color, 0, 255);
b_color = CP_Math_ClampInt(b_color, 0, 255);
// Set the background as the color
CP_Graphics_ClearBackground(CP_Color_Create(r_color, 0, b_color, 255));
}
Related
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Math_ClampInt
- CP_Input_GetMouseWorldX
- CP_Input_GetMouseWorldY
- CP_System_GetWindowWidth
- CP_System_GetWindowHeight
CP_Math_LerpFloat
Linearly interpolates between two input floats with an input lerp factor.
Function
float CP_Math_LerpFloat(float a, float b, float lerpFactor);
Parameters
- a - (float) The value the function returns when lerp_factor is 0.0, the starting value.
- b - (float) The value the function returns when lerp_factor is 1.0, the ending value.
- lerp_factor - (float) The ratio used to determine what value to return between a and b.
Return
int
Example
CP_Image justin_face;
void init()
{
justin_face = CP_Image_Load("./Assets/justin1.png");
}
float rot_counter = 0;
void update()
{
// Increment rotation lerp factor or reset
if (rot_counter >= 1.0f)
rot_counter -= 1.0f;
else
rot_counter += CP_System_GetDt();
// Lerp rotation and draw image
float rotation = CP_Math_LerpFloat(0.0f, 360.0f, rot_counter);
CP_Graphics_ClearBackground(CP_Color_Create(255, 255, 255, 255));
CP_Image_DrawAdvanced(justin_face, CP_Input_GetMouseWorldX(), CP_Input_GetMouseWorldY(), 100, 150, 255, rotation);
}
Related
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Image_Load
- CP_Image_DrawAdvanced
- CP_Input_GetMouseWorldX
- CP_Input_GetMouseWorldY
- CP_System_GetDt
CP_Math_Square
CP_Math_Distance
CP_Math_Degrees
Takes an input number of radians as a float, then returns it converted to degrees as a float
Function
float CP_Math_Degrees(float radians);
Parameters
- radians - (float) The number to convert to degrees from radians.
Return
float
Example
void init()
{
CP_Settings_EllipseMode(CP_POSITION_CENTER);
CP_Font_Set(CP_Font_GetDefault(), 30);
CP_Settings_Fill(CP_Color_Create(0, 0, 0, 255));
}
float r_radians;
float counter = 2.0f;
void update()
{
CP_Graphics_ClearBackground(CP_Color_Create(255, 255, 255, 255));
// Change degrees every few seconds
if (counter >= 2.0f)
{
r_radians = CP_Random_RangeFloat(0.0f, PI);
counter = 0.0f;
}
else
counter += CP_System_GetDt();
// Print out number of radians and convert to degrees
char buffer[128] = { 0 };
sprintf_s(buffer, 128, "Radians: %.3f\nis equal to\nDegrees: %.3f", r_radians, CP_Math_Degrees(r_radians));
CP_Font_DrawTextBox(buffer, 100, 150, 200);
}
Related
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Settings_EllipseMode
- CP_Settings_Fill
- CP_Font_Set
- CP_Font_GetDefault
- CP_Font_DrawTextBox
- CP_System_GetDt
- CP_Random_RangeFloat
CP_Math_Radians
Takes an input number of degrees as a float, then returns it converted to radians as a float
Function
float CP_Math_Radians(float degrees);
Parameters
- degrees - (float) The number to convert to radians from degrees.
Return
float
Example
void init()
{
CP_Settings_EllipseMode(CP_POSITION_CENTER);
CP_Font_Set(CP_Font_GetDefault(), 30);
CP_Settings_Fill(CP_Color_Create(0, 0, 0, 255));
}
float r_degrees;
float counter = 2.0f;
void update()
{
CP_Graphics_ClearBackground(CP_Color_Create(255, 255, 255, 255));
// Change degrees every few seconds
if (counter >= 2.0f)
{
r_degrees = CP_Random_RangeFloat(0.0f, 360.0f);
counter = 0.0f;
}
else
counter += CP_System_GetDt();
// Print out number of degrees and convert to radians
char buffer[128] = { 0 };
sprintf_s(buffer, 128, "Degrees: %.3f\nis equal to\nRadians: %.3f", r_degrees, CP_Math_Radians(r_degrees));
CP_Font_DrawTextBox(buffer, 100, 150, 200);
}
Related
- CP_Graphics_ClearBackground
- CP_Color_Create
- CP_Settings_EllipseMode
- CP_Settings_Fill
- CP_Font_Set
- CP_Font_GetDefault
- CP_Font_DrawTextBox
- CP_System_GetDt
- CP_Random_RangeFloat