GSDP:GAM100/CProcessing/vec2 dot()

From Inside
Jump to: navigation, search

vec2_dot()

Description

Returns the dot product of two Vec2s as a float.

Parameters

vec2_dot(Vec2 a, Vec2 b)

  • a - (Vec2) The first vector in the operation.
  • b - (Vec2) The second vector in the operation.

Example

Vec2 vector1;
Vec2 vector2;

void init()
{
  vector1 = vec2_set(randomRangeFloat(0.0f, 50.0f), randomRangeFloat(0.0f, 50.0f));
  vector2 = vec2_set(randomRangeFloat(0.0f, 50.0f), randomRangeFloat(0.0f, 50.0f));
  textFont(defaultFont, 30);
  fill(color(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);
  text(vec_text, 0, 50);
  sprintf_s(vec_text, 128, "2st Vector: [%.2f, %.2f]", vector2.x, vector2.y);
  text(vec_text, 0, 100);

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

  // Manually figure out the angle of the two vectors
  float dot = vec2_dot(vector1, vector2);
  float length1 = vec2_length(vector1);
  float length2 = vec2_length(vector2);
  angle = 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);
  text(vec_text, 0, 200);

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

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>