GSDP:GAM100/CProcessing/vec2 distance()

From Inside
Jump to: navigation, search

vec2_distance()

Description

Returns the distance between two points input as Vec2s as a float.

Parameters

vec2_distance(Vec2 a, Vec2 b)

  • a - (Vec2) First input point.
  • b - (Vec2) Second input point.

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 distance between the points
  float distance = vec2_distance(position, position2);
  char distance_text[128] = {0};
  sprintf_s(distance_text, 128, "Distance: %.0f", distance);
  fill(color(0, 0, 0, 255));
  text(distance_text, canvasWidth / 2 - 60, canvasHeight / 2);
}

Related

Personal tools
Namespaces

Variants
Actions
Navigation
NameSpaces:>
Tools
Dynamic:>