GSDP:GAM100/CProcessing/vec2 angle()

From Inside
Jump to: navigation, search

vec2_angle()

Description

Returns the angle between two vectors input as Vec2s as a float.

Parameters

vec2_distance(Vec2 a, Vec2 b)

  • a - (Vec2) First vector in calculation.
  • b - (Vec2) Second vector in calculation.

Example

Vec2 position;
Vec2 position2;
Vec2 speed;

void init()
{
  position = vec2_set(canvasWidth / 2, canvasHeight / 4);
  position2 = vec2_set(canvasWidth / 2, 3 * canvasHeight / 4);
  speed = vec2_set(50.0f, 0.0f);

  ellipseMode(CENTER);
  textFont(defaultFont, 30);
}

void update()
{
  background(color(255, 255, 255, 255));

  // Check if direction should be inverted
  if (position.x > canvasWidth)
  {
    speed = vec2_neg(speed);
    position.x = canvasWidth;
  }
  else if (position.x < 0)
  {
    speed = vec2_neg(speed);
    position.x = 0;
  }

  // Update position
  position = vec2_add(position, vec2_scale(speed, dt()));
  position2 = vec2_sub(position2, vec2_scale(speed, dt()));

  // Draw the circles
  fill(color(255, 0, 0, 255));
  circle(position.x, position.y, 40);
  fill(color(0, 0, 255, 255));
  circle(position2.x, position2.y, 40);

  // Draw the angle the two points make with the top left corner
  float angle = vec2_angle(position, position2);
  char angle_text[128] = {0};
  sprintf_s(angle_text, 128, "Origin to Positions Angle: %.0f", angle);
  fill(color(0, 0, 0, 255));
  text(angle_text, canvasWidth / 2 - 160, canvasHeight / 2);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>