GSDP:GAM100/CProcessing/CP Vector Normalize()
< GSDP:GAM100 | CProcessing
CP_Vector_Normalize()
Description
Returns a copy of the normalized input CP_Vector. A normalized vector is one where the magnitude (length) is 1. Meaning CP_Vector_Length() will always return 1 on a normalized vector.
Parameters
CP_Vector_Normalize(CP_Vector a)
- a - (CP_Vector) - Vector to normalize
Example
CP_Vector position;
CP_Vector position2;
CP_Vector speed;
CP_Vector norm;
void init()
{
position = CP_Vector_Set(CP_System_GetWindowWidth() / 2, CP_System_GetWindowHeight() / 4);
position2 = CP_Vector_Set(CP_System_GetWindowWidth() / 2, 3 * CP_System_GetWindowHeight() / 4);
speed = CP_Vector_Set(CP_Random_RangeFloat(5.0f, 10.0f), 0.0f);
norm = CP_Vector_Normalize(speed);
CP_Settings_EllipseMode(CP_POSITION_CENTER);
CP_Font_Set(CP_Font_GetDefault());
}
void update()
{
CP_Settings_Background(CP_Color_Create(255, 255, 255, 255));
// Check if direction should be inverted
if (position.x > CP_System_GetWindowWidth())
{
speed = CP_Vector_Negate(speed);
norm = CP_Vector_Normalize(speed);
position.x = CP_System_GetWindowWidth();
}
else if (position.x < 0)
{
speed = CP_Vector_Negate(speed);
norm = CP_Vector_Normalize(speed);
position.x = 0;
}
// Update position
position = CP_Vector_Add(position, speed);
position2 = CP_Vector_Add(position2, norm);
// Draw the circles
CP_Settings_Fill(color(255, 0, 0, 255));
CP_Graphics_DrawCircle(position.x, position.y, 40);
CP_Settings_Fill(color(0, 0, 255, 255));
CP_Graphics_DrawCircle(position2.x, position2.y, 40);
// Draw the speed and normzlied speed
char speed_text[128] = {0};
sprintf_s(speed_text, 128, "Speed: [%.2f,%.2f] Normalized: [%.2f,%.2f]", speed.x, speed.y, norm.x, norm.y);
CP_Settings_Fill(color(0, 0, 0, 255));
CP_Font_DrawText(speed_text, CP_System_GetWindowWidth() / 2 - 160, CP_System_GetWindowHeight() / 2);
}
Related
- Main Page
- CP_Vector
- CP_Vector_Set()
- CP_System_GetWindowHeight()
- CP_System_GetWindowWidth()
- CP_Settings_EllipseMode()
- CP_Settings_Fill()
- CP_Vector_Negate()
- CP_Vector_Add()
- CP_Graphics_DrawCircle()
- CP_Color_Create()
- CP_Random_RangeFloat()
- CP_Font_Set()
- CP_Font_GetDefault()
- CP_Font_DrawText()
- CP_Graphics_DrawCircle()