Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dist() isn't actually used in final example in reference .../reference/P5.Vector/dist/ #594

Closed
brandtryan opened this issue Oct 14, 2024 · 0 comments · Fixed by processing/p5.js#7324

Comments

@brandtryan
Copy link

Below you will find the last example in the reference area of the website for the p5.Vector.dist() method.

You'll notice the method isn't actually used in the example. I figure either:

A. The intention was to include a console log of p5.Vector.dist(v1, v3) to show that it was equal to the purple magnitude vector (as they should be). Both are 36 if you do the math.

or

B. There's no problem and the example intentionally left out the dist method in the example, showing instead the mag() method so the user could work out on their own that they should be the same?

function setup() {
  createCanvas(100, 100);

  describe('Three arrows drawn on a gray square. A red and a blue arrow extend from the top left. A purple arrow extends from the tip of the red arrow to the tip of the blue arrow. The number 36 is written in black near the purple arrow.');
}

function draw() {
  background(200);

  let origin = createVector(0, 0);

  // Draw the red arrow.
  let v1 = createVector(50, 50);
  drawArrow(origin, v1, 'red');

  // Draw the blue arrow.
  let v2 = createVector(20, 70);
  drawArrow(origin, v2, 'blue');

  // Purple arrow.
  let v3 = p5.Vector.sub(v2, v1);
  drawArrow(v1, v3, 'purple');

  // Style the text.
  textAlign(CENTER);

  // Display the magnitude.
  let m = floor(v3.mag());
  text(m, 50, 75);
}

// Draws an arrow between two vectors.
function drawArrow(base, vec, myColor) {
  push();
  stroke(myColor);
  strokeWeight(3);
  fill(myColor);
  translate(base.x, base.y);
  line(0, 0, vec.x, vec.y);
  rotate(vec.heading());
  let arrowSize = 7;
  translate(vec.mag() - arrowSize, 0);
  triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
  pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant