GSDP:GAM100/CProcessing/CP Vector Distance()

From Inside
Jump to: navigation, search

CP_Vector_Distance()

Description

Returns the distance between two points input as CP_Vectors as a float

Parameters

CP_Vector_Distance(CP_Vector a, CP_Vector b)

  • a - (CP_Vector) - First point in calculation
  • b - (CP_Vector) - Second point in calculation

Example

CP_Vector position;
CP_Vector position2;
CP_Vector speed;

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(50.0f, 0.0f);

  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);
    position.x = CP_System_GetWindowWidth();
  }
  else if (position.x < 0)
  {
    speed = CP_Vector_Negate(speed);
    position.x = 0;
  }

  // Update position
  position = CP_Vector_Add(position, CP_Vector_Scale(speed, CP_System_GetDt()));
  position2 = CP_Vector_Subtract(position2, CP_Vector_Scale(speed, CP_System_GetDt()));

  // Draw the circles
  CP_Settings_Fill(CP_Color_Create(255, 0, 0, 255));
  CP_Graphics_DrawCircle(position.x, position.y, 40);
  CP_Settings_Fill(CP_Color_Create(0, 0, 255, 255));
  CP_Graphics_DrawCircle(position2.x, position2.y, 40);

  // Draw the distance between the points
  float distance = CP_Vector_Distance(position, position2);
  char distance_text[128] = {0};
  sprintf_s(distance_text, 128, "Distance: %.0f", distance);
  CP_Settings_Fill(color(0, 0, 0, 255));
  CP_Font_DrawText(distance_text, CP_System_GetWindowWidth() / 2 - 60, CP_System_GetWindowHeight() / 2);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>