GSDP:GAM100/CProcessing/CP Vector DotProduct()

From Inside
Jump to: navigation, search

CP_Vector_DotProduct()

Description

Returns the dot product of two CP_Vectors as a float.

Parameters

CP_Vector_DotProduct(CP_Vector a, CP_Vector b)

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

Example

CP_Vector vector1;
CP_Vector vector2;

void init()
{
  vector1 = CP_Vector_Set(CP_Random_RangeFloat(0.0f, 50.0f), CP_Random_RangeFloat(0.0f, 50.0f));
  vector2 = CP_Vector_Set(CP_Random_RangeFloat(0.0f, 50.0f), CP_Random_RangeFloat(0.0f, 50.0f));
  CP_Font_Set(CP_Font_GetDefault());
  CP_Settings_Fill(CP_Color_Create(0, 0, 0, 255));
}

void update()
{
  // Print out the randomly generated vectors
  char vec_text[128] = { 0 };
  sprintf_s(vec_text, 128, "1st Vector: [%.2f, %.2f]", vector1.x, vector1.y);
  CP_Font_DrawText(vec_text, 0, 50);
  sprintf_s(vec_text, 128, "2st Vector: [%.2f, %.2f]", vector2.x, vector2.y);
  CP_Font_DrawText(vec_text, 0, 100);

  // Print out finding the angle with a function
  float angle = CP_Vector_Angle(vector1, vector2);
  sprintf_s(vec_text, 128, "Angle (Function): %.2f", angle);
  CP_Font_DrawText(vec_text, 0, 150);

  // Manually figure out the angle of the two vectors
  float dot = CP_Vector_DotProduct(vector1, vector2);
  float length1 = CP_Vector_Length(vector1);
  float length2 = CP_Vector_Length(vector2);
  angle = CP_Math_Degrees((float)acos((double)(dot/(length1*length2))));

  // Print out the manual angle between the two vectors
  sprintf_s(vec_text, 128, "Angle (Manual): %.2f", angle);
  CP_Font_DrawText(vec_text, 0, 200);

  CP_Font_DrawText("They are the same!", 0, 250);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>