diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index bb212fea4f..33023b2dcb 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -1,3919 +1,12 @@ -/** - * @module Math - * @submodule Vector - * @requires constants - */ - -import p5 from '../core/main'; -import * as constants from '../core/constants'; - - -/** - * A class to describe a two or three-dimensional vector. - * - * A vector can be thought of in different ways. In one view, a vector is like - * an arrow pointing in space. Vectors have both magnitude (length) and - * direction. - * - * `p5.Vector` objects are often used to program motion because they simplify - * the math. For example, a moving ball has a position and a velocity. - * Position describes where the ball is in space. The ball's position vector - * extends from the origin to the ball's center. Velocity describes the ball's - * speed and the direction it's moving. If the ball is moving straight up, its - * velocity vector points straight up. Adding the ball's velocity vector to - * its position vector moves it, as in `pos.add(vel)`. Vector math relies on - * methods inside the `p5.Vector` class. - * - * Note: createVector() is the recommended way - * to make an instance of this class. - * - * @class p5.Vector - * @constructor - * @param {Number} [x] x component of the vector. - * @param {Number} [y] y component of the vector. - * @param {Number} [z] z component of the vector. - * @example - *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create p5.Vector objects.
- * let p1 = createVector(25, 25);
- * let p2 = createVector(75, 75);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Draw the first point using a p5.Vector.
- * point(p1);
- *
- * // Draw the second point using a p5.Vector's components.
- * point(p2.x, p2.y);
- *
- * describe('Two black dots on a gray square, one at the top left and the other at the bottom right.');
- * }
- *
- *
- * let pos;
- * let vel;
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * // Create p5.Vector objects.
- * pos = createVector(50, 100);
- * vel = createVector(0, -1);
- *
- * describe('A black dot moves from bottom to top on a gray square. The dot reappears at the bottom when it reaches the top.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * // Add velocity to position.
- * pos.add(vel);
- *
- * // If the dot reaches the top of the canvas,
- * // restart from the bottom.
- * if (pos.y < 0) {
- * pos.y = 100;
- * }
- *
- * // Draw the dot.
- * strokeWeight(5);
- * point(pos);
- * }
- *
- *
- * function setup() {
- * let v = createVector(20, 30);
- *
- * // Prints 'p5.Vector Object : [20, 30, 0]'.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top left.
- * let pos = createVector(25, 25);
- * point(pos);
- *
- * // Top right.
- * // set() with numbers.
- * pos.set(75, 25);
- * point(pos);
- *
- * // Bottom right.
- * // set() with a p5.Vector.
- * let p2 = createVector(75, 75);
- * pos.set(p2);
- * point(pos);
- *
- * // Bottom left.
- * // set() with an array.
- * let arr = [25, 75];
- * pos.set(arr);
- * point(pos);
- *
- * describe('Four black dots arranged in a square on a gray background.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100 ,100);
- *
- * background(200);
- *
- * // Create a p5.Vector object.
- * let pos = createVector(50, 50);
- *
- * // Make a copy.
- * let pc = pos.copy();
- *
- * // Draw the point.
- * strokeWeight(5);
- * point(pc);
- *
- * describe('A black point drawn in the middle of a gray square.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top left.
- * let pos = createVector(25, 25);
- * point(pos);
- *
- * // Top right.
- * // Add numbers.
- * pos.add(50, 0);
- * point(pos);
- *
- * // Bottom right.
- * // Add a p5.Vector.
- * let p2 = createVector(0, 50);
- * pos.add(p2);
- * point(pos);
- *
- * // Bottom left.
- * // Add an array.
- * let arr = [-50, 0];
- * pos.add(arr);
- * point(pos);
- *
- * describe('Four black dots arranged in a square on a gray background.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Top left.
- * let p1 = createVector(25, 25);
- *
- * // Center.
- * let p2 = createVector(50, 50);
- *
- * // Bottom right.
- * // Add p1 and p2.
- * let p3 = p5.Vector.add(p1, p2);
- *
- * // Draw the points.
- * strokeWeight(5);
- * point(p1);
- * point(p2);
- * point(p3);
- *
- * describe('Three black dots in a diagonal line from top left to bottom right.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Three arrows drawn on a gray square. A red arrow extends from the top left corner to the center. A blue arrow extends from the tip of the red arrow. A purple arrow extends from the origin to the tip of the blue 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(-30, 20);
- * drawArrow(v1, v2, 'blue');
- *
- * // Purple arrow.
- * let v3 = p5.Vector.add(v1, v2);
- * drawArrow(origin, v3, 'purple');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(3, 4, 5);
- *
- * // Divide numbers.
- * v.rem(2);
- *
- * // Prints 'p5.Vector Object : [1, 0, 1]'.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(3, 4, 5);
- *
- * // Divide numbers.
- * v.rem(2, 3);
- *
- * // Prints 'p5.Vector Object : [1, 1, 5]'.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(3, 4, 5);
- *
- * // Divide numbers.
- * v.rem(2, 3, 4);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(3, 4, 5);
- * let v2 = createVector(2, 3, 4);
- *
- * // Divide a p5.Vector.
- * v1.rem(v2);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(3, 4, 5);
- *
- * // Divide an array.
- * let arr = [2, 3, 4];
- * v.rem(arr);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(3, 4, 5);
- * let v2 = createVector(2, 3, 4);
- *
- * // Divide without modifying the original vectors.
- * let v3 = p5.Vector.rem(v1, v2);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v3.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Bottom right.
- * let pos = createVector(75, 75);
- * point(pos);
- *
- * // Top right.
- * // Subtract numbers.
- * pos.sub(0, 50);
- * point(pos);
- *
- * // Top left.
- * // Subtract a p5.Vector.
- * let p2 = createVector(50, 0);
- * pos.sub(p2);
- * point(pos);
- *
- * // Bottom left.
- * // Subtract an array.
- * let arr = [0, -50];
- * pos.sub(arr);
- * point(pos);
- *
- * describe('Four black dots arranged in a square on a gray background.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create p5.Vector objects.
- * let p1 = createVector(75, 75);
- * let p2 = createVector(50, 50);
- *
- * // Subtract without modifying the original vectors.
- * let p3 = p5.Vector.sub(p1, p2);
- *
- * // Draw the points.
- * strokeWeight(5);
- * point(p1);
- * point(p2);
- * point(p3);
- *
- * describe('Three black dots in a diagonal line from top left to bottom right.');
- * }
- *
- *
- * 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.');
- * }
- *
- * 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');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top-left.
- * let p = createVector(25, 25);
- * point(p);
- *
- * // Center.
- * // Multiply all components by 2.
- * p.mult(2);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.');
- * }
- *
- *
- * function setup() {
- * strokeWeight(5);
- *
- * // Top-left.
- * let p = createVector(25, 25);
- * point(p);
- *
- * // Bottom-right.
- * // Multiply p.x * 2 and p.y * 3
- * p.mult(2, 3);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top-left.
- * let p = createVector(25, 25);
- * point(p);
- *
- * // Bottom-right.
- * // Multiply p.x * 2 and p.y * 3
- * let arr = [2, 3];
- * p.mult(arr);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top-left.
- * let p = createVector(25, 25);
- * point(p);
- *
- * // Bottom-right.
- * // Multiply p.x * p2.x and p.y * p2.y
- * let p2 = createVector(2, 3);
- * p.mult(p2);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Top-left.
- * let p = createVector(25, 25);
- * point(p);
- *
- * // Bottom-right.
- * // Create a new p5.Vector with
- * // p3.x = p.x * p2.x
- * // p3.y = p.y * p2.y
- * let p2 = createVector(2, 3);
- * let p3 = p5.Vector.mult(p, p2);
- * point(p3);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Two arrows extending from the top left corner. The blue arrow is twice the length of the red arrow.');
- * }
- * function draw() {
- * background(200);
- *
- * let origin = createVector(0, 0);
- *
- * // Draw the red arrow.
- * let v1 = createVector(25, 25);
- * drawArrow(origin, v1, 'red');
- *
- * // Draw the blue arrow.
- * let v2 = p5.Vector.mult(v1, 2);
- * drawArrow(origin, v2, 'blue');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Center.
- * let p = createVector(50, 50);
- * point(p);
- *
- * // Top-left.
- * // Divide p.x / 2 and p.y / 2
- * p.div(2);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Bottom-right.
- * let p = createVector(50, 75);
- * point(p);
- *
- * // Top-left.
- * // Divide p.x / 2 and p.y / 3
- * p.div(2, 3);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Bottom-right.
- * let p = createVector(50, 75);
- * point(p);
- *
- * // Top-left.
- * // Divide p.x / 2 and p.y / 3
- * let arr = [2, 3];
- * p.div(arr);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Bottom-right.
- * let p = createVector(50, 75);
- * point(p);
- *
- * // Top-left.
- * // Divide p.x / 2 and p.y / 3
- * let p2 = createVector(2, 3);
- * p.div(p2);
- * point(p);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Style the points.
- * strokeWeight(5);
- *
- * // Bottom-right.
- * let p = createVector(50, 75);
- * point(p);
- *
- * // Top-left.
- * // Create a new p5.Vector with
- * // p3.x = p.x / p2.x
- * // p3.y = p.y / p2.y
- * let p2 = createVector(2, 3);
- * let p3 = p5.Vector.div(p, p2);
- * point(p3);
- *
- * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
- * }
- *
- *
- * 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 = p5.Vector.div(v1, 2);
- * drawArrow(origin, v2, 'blue');
- *
- * describe('Two arrows extending from the top left corner. The blue arrow is half the length of the red arrow.');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create a p5.Vector object.
- * let p = createVector(30, 40);
- *
- * // Draw a line from the origin.
- * line(0, 0, p.x, p.y);
- *
- * // Style the text.
- * textAlign(CENTER);
- * textSize(16);
- *
- * // Display the vector's magnitude.
- * let m = p.mag();
- * text(m, p.x, p.y);
- *
- * describe('A diagonal black line extends from the top left corner of a gray square. The number 50 is written at the end of the line.');
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create a p5.Vector object.
- * let p = createVector(30, 40);
- *
- * // Draw a line from the origin.
- * line(0, 0, p.x, p.y);
- *
- * // Style the text.
- * textAlign(CENTER);
- * textSize(16);
- *
- * // Display the vector's magnitude squared.
- * let m = p.magSq();
- * text(m, p.x, p.y);
- *
- * describe('A diagonal black line extends from the top left corner of a gray square. The number 2500 is written at the end of the line.');
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(3, 4);
- * let v2 = createVector(3, 0);
- *
- * // Calculate the dot product.
- * let dp = v1.dot(v2);
- *
- * // Prints "9" to the console.
- * print(dp);
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(1, 0);
- * let v2 = createVector(0, 1);
- *
- * // Calculate the dot product.
- * let dp = p5.Vector.dot(v1, v2);
- *
- * // Prints "0" to the console.
- * print(dp);
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Two arrows drawn on a gray square. A black arrow points to the right and a red arrow follows the mouse. The text "v1 • v2 = something" changes as the mouse moves.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * // Center.
- * let v0 = createVector(50, 50);
- *
- * // Draw the black arrow.
- * let v1 = createVector(30, 0);
- * drawArrow(v0, v1, 'black');
- *
- * // Draw the red arrow.
- * let v2 = createVector(mouseX - 50, mouseY - 50);
- * drawArrow(v0, v2, 'red');
- *
- * // Display the dot product.
- * let dp = v2.dot(v1);
- * text(`v2 • v1 = ${dp}`, 10, 20);
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(1, 0);
- * let v2 = createVector(3, 4);
- *
- * // Calculate the cross product.
- * let cp = v1.cross(v2);
- *
- * // Prints "p5.Vector Object : [0, 0, 4]" to the console.
- * print(cp.toString());
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v1 = createVector(1, 0);
- * let v2 = createVector(3, 4);
- *
- * // Calculate the cross product.
- * let cp = p5.Vector.cross(v1, v2);
- *
- * // Prints "p5.Vector Object : [0, 0, 4]" to the console.
- * print(cp.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v1 = createVector(1, 0);
- * let v2 = createVector(0, 1);
- *
- * // Calculate the distance between them.
- * let d = v1.dist(v2);
- *
- * // Prints "1.414..." to the console.
- * print(d);
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v1 = createVector(1, 0);
- * let v2 = createVector(0, 1);
- *
- * // Calculate the distance between them.
- * let d = p5.Vector.dist(v1, v2);
- *
- * // Prints "1.414..." to the console.
- * print(d);
- * }
- *
- *
- * 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. The same as floor(v3.mag());
- * let m = floor(p5.Vector.dist(v1, v2));
- * 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();
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create a p5.Vector.
- * let v = createVector(10, 20, 2);
- *
- * // Normalize.
- * v.normalize();
- *
- * // Prints "p5.Vector Object : [0.445..., 0.890..., 0.089...]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * background(200);
- *
- * // Create a p5.Vector.
- * let v0 = createVector(10, 20, 2);
- *
- * // Create a normalized copy.
- * let v1 = p5.Vector.normalize(v0);
- *
- * // Prints "p5.Vector Object : [10, 20, 2]" to the console.
- * print(v0.toString());
- * // Prints "p5.Vector Object : [0.445..., 0.890..., 0.089...]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe("A red and blue arrow extend from the center of a circle. Both arrows follow the mouse, but the blue arrow's length is fixed to the circle's radius.");
- * }
- *
- * function draw() {
- * background(240);
- *
- * // Vector to the center.
- * let v0 = createVector(50, 50);
- *
- * // Vector from the center to the mouse.
- * let v1 = createVector(mouseX - 50, mouseY - 50);
- *
- * // Circle's radius.
- * let r = 25;
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * v1.normalize();
- * drawArrow(v0, v1.mult(r), 'blue');
- *
- * // Draw the circle.
- * noFill();
- * circle(50, 50, r * 2);
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(10, 20, 2);
- *
- * // Limit its magnitude.
- * v.limit(5);
- *
- * // Prints "p5.Vector Object : [2.227..., 4.454..., 0.445...]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(10, 20, 2);
- *
- * // Create a copy an limit its magintude.
- * let v1 = p5.Vector.limit(v0, 5);
- *
- * // Prints "p5.Vector Object : [2.227..., 4.454..., 0.445...]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe("A red and blue arrow extend from the center of a circle. Both arrows follow the mouse, but the blue arrow never crosses the circle's edge.");
- * }
- * function draw() {
- * background(240);
- *
- * // Vector to the center.
- * let v0 = createVector(50, 50);
- *
- * // Vector from the center to the mouse.
- * let v1 = createVector(mouseX - 50, mouseY - 50);
- *
- * // Circle's radius.
- * let r = 25;
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v1.limit(r), 'blue');
- *
- * // Draw the circle.
- * noFill();
- * circle(50, 50, r * 2);
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(3, 4, 0);
- *
- * // Prints "5" to the console.
- * print(v.mag());
- *
- * // Set its magnitude to 10.
- * v.setMag(10);
- *
- * // Prints "p5.Vector Object : [6, 8, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(3, 4, 0);
- *
- * // Create a copy with a magnitude of 10.
- * let v1 = p5.Vector.setMag(v0, 10);
- *
- * // Prints "5" to the console.
- * print(v0.mag());
- *
- * // Prints "p5.Vector Object : [6, 8, 0]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Two arrows extend from the top left corner of a square toward its center. The red arrow reaches the center and the blue arrow only extends part of the way.');
- * }
- *
- * function draw() {
- * background(240);
- *
- * let origin = createVector(0, 0);
- * let v = createVector(50, 50);
- *
- * // Draw the red arrow.
- * drawArrow(origin, v, 'red');
- *
- * // Set v's magnitude to 30.
- * v.setMag(30);
- *
- * // Draw the blue arrow.
- * drawArrow(origin, v, 'blue');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(1, 1);
- *
- * // Prints "0.785..." to the console.
- * print(v.heading());
- *
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Prints "45" to the console.
- * print(v.heading());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(1, 1);
- *
- * // Prints "0.785..." to the console.
- * print(p5.Vector.heading(v));
- *
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Prints "45" to the console.
- * print(p5.Vector.heading(v));
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('A black arrow extends from the top left of a square to its center. The text "Radians: 0.79" and "Degrees: 45" is written near the tip of the arrow.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * let origin = createVector(0, 0);
- * let v = createVector(50, 50);
- *
- * // Draw the black arrow.
- * drawArrow(origin, v, 'black');
- *
- * // Use radians.
- * angleMode(RADIANS);
- *
- * // Display the heading in radians.
- * let h = round(v.heading(), 2);
- * text(`Radians: ${h}`, 20, 70);
- *
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Display the heading in degrees.
- * h = v.heading();
- * text(`Degrees: ${h}`, 20, 85);
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(0, 1);
- *
- * // Prints "1.570..." to the console.
- * print(v.heading());
- *
- * // Point to the left.
- * v.setHeading(PI);
- *
- * // Prints "3.141..." to the console.
- * print(v.heading());
- * }
- *
- *
- * function setup() {
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Create a p5.Vector object.
- * let v = createVector(0, 1);
- *
- * // Prints "90" to the console.
- * print(v.heading());
- *
- * // Point to the left.
- * v.setHeading(180);
- *
- * // Prints "180" to the console.
- * print(v.heading());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Two arrows extend from the center of a gray square. The red arrow points to the right and the blue arrow points down.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v0 = createVector(50, 50);
- * let v1 = createVector(30, 0);
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Point down.
- * v1.setHeading(HALF_PI);
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v1, 'blue');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(1, 0);
- *
- * // Prints "p5.Vector Object : [1, 0, 0]" to the console.
- * print(v.toString());
- *
- * // Rotate a quarter turn.
- * v.rotate(HALF_PI);
- *
- * // Prints "p5.Vector Object : [0, 1, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Create a p5.Vector object.
- * let v = createVector(1, 0);
- *
- * // Prints "p5.Vector Object : [1, 0, 0]" to the console.
- * print(v.toString());
- *
- * // Rotate a quarter turn.
- * v.rotate(90);
- *
- * // Prints "p5.Vector Object : [0, 1, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(1, 0);
- *
- * // Create a rotated copy.
- * let v1 = p5.Vector.rotate(v0, HALF_PI);
- *
- * // Prints "p5.Vector Object : [1, 0, 0]" to the console.
- * print(v0.toString());
- * // Prints "p5.Vector Object : [0, 1, 0]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Create a p5.Vector object.
- * let v0 = createVector(1, 0);
- *
- * // Create a rotated copy.
- * let v1 = p5.Vector.rotate(v0, 90);
- *
- * // Prints "p5.Vector Object : [1, 0, 0]" to the console.
- * print(v0.toString());
- *
- * // Prints "p5.Vector Object : [0, 1, 0]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * let v0;
- * let v1;
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * // Create p5.Vector objects.
- * v0 = createVector(50, 50);
- * v1 = createVector(30, 0);
- *
- * describe('A black arrow extends from the center of a gray square. The arrow rotates clockwise.');
- * }
- *
- * function draw() {
- * background(240);
- *
- * // Rotate v1.
- * v1.rotate(0.01);
- *
- * // Draw the black arrow.
- * drawArrow(v0, v1, 'black');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(1, 0);
- * let v1 = createVector(0, 1);
- *
- * // Prints "1.570..." to the console.
- * print(v0.angleBetween(v1));
- *
- * // Prints "-1.570..." to the console.
- * print(v1.angleBetween(v0));
- * }
- *
- *
- * function setup() {
- * // Use degrees.
- * angleMode(DEGREES);
- * // Create p5.Vector objects.
- * let v0 = createVector(1, 0);
- * let v1 = createVector(0, 1);
- *
- * // Prints "90" to the console.
- * print(v0.angleBetween(v1));
- *
- * // Prints "-90" to the console.
- * print(v1.angleBetween(v0));
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(1, 0);
- * let v1 = createVector(0, 1);
- *
- * // Prints "1.570..." to the console.
- * print(p5.Vector.angleBetween(v0, v1));
- *
- * // Prints "-1.570..." to the console.
- * print(p5.Vector.angleBetween(v1, v0));
- * }
- *
- *
- * function setup() {
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Create p5.Vector objects.
- * let v0 = createVector(1, 0);
- * let v1 = createVector(0, 1);
- *
- * // Prints "90" to the console.
- * print(p5.Vector.angleBetween(v0, v1));
- *
- * // Prints "-90" to the console.
- * print(p5.Vector.angleBetween(v1, v0));
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Two arrows extend from the center of a gray square. A red arrow points to the right and a blue arrow points down. The text "Radians: 1.57" and "Degrees: 90" is written above the arrows.');
- * }
- * function draw() {
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v0 = createVector(50, 50);
- * let v1 = createVector(30, 0);
- * let v2 = createVector(0, 30);
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v2, 'blue');
- *
- * // Use radians.
- * angleMode(RADIANS);
- *
- * // Display the angle in radians.
- * let angle = round(v1.angleBetween(v2), 2);
- * text(`Radians: ${angle}`, 20, 20);
- *
- * // Use degrees.
- * angleMode(DEGREES);
- *
- * // Display the angle in degrees.
- * angle = round(v1.angleBetween(v2), 2);
- * text(`Degrees: ${angle}`, 20, 35);
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(1, 1, 1);
- * let v1 = createVector(3, 3, 3);
- *
- * // Interpolate.
- * v0.lerp(v1, 0.5);
- *
- * // Prints "p5.Vector Object : [2, 2, 2]" to the console.
- * print(v0.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(1, 1, 1);
- *
- * // Interpolate.
- * v.lerp(3, 3, 3, 0.5);
- *
- * // Prints "p5.Vector Object : [2, 2, 2]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(1, 1, 1);
- * let v1 = createVector(3, 3, 3);
- *
- * // Interpolate.
- * let v2 = p5.Vector.lerp(v0, v1, 0.5);
- *
- * // Prints "p5.Vector Object : [2, 2, 2]" to the console.
- * print(v2.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Three arrows extend from the center of a gray square. A red arrow points to the right, a blue arrow points down, and a purple arrow points to the bottom right.');
- * }
- * function draw() {
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v0 = createVector(50, 50);
- * let v1 = createVector(30, 0);
- * let v2 = createVector(0, 30);
- *
- * // Interpolate.
- * let v3 = p5.Vector.lerp(v1, v2, 0.5);
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v2, 'blue');
- *
- * // Draw the purple arrow.
- * drawArrow(v0, v3, 'purple');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(3, 0);
- *
- * // Prints "3" to the console.
- * print(v0.mag());
- *
- * // Prints "0" to the console.
- * print(v0.heading());
- *
- * // Create a p5.Vector object.
- * let v1 = createVector(0, 1);
- *
- * // Prints "1" to the console.
- * print(v1.mag());
- *
- * // Prints "1.570..." to the console.
- * print(v1.heading());
- *
- * // Interpolate halfway between v0 and v1.
- * v0.slerp(v1, 0.5);
- *
- * // Prints "2" to the console.
- * print(v0.mag());
- *
- * // Prints "0.785..." to the console.
- * print(v0.heading());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v0 = createVector(3, 0);
- *
- * // Prints "3" to the console.
- * print(v0.mag());
- *
- * // Prints "0" to the console.
- * print(v0.heading());
- *
- * // Create a p5.Vector object.
- * let v1 = createVector(0, 1);
- *
- * // Prints "1" to the console.
- * print(v1.mag());
- *
- * // Prints "1.570..." to the console.
- * print(v1.heading());
- *
- * // Create a p5.Vector that's halfway between v0 and v1.
- * let v3 = p5.Vector.slerp(v0, v1, 0.5);
- *
- * // Prints "2" to the console.
- * print(v3.mag());
- *
- * // Prints "0.785..." to the console.
- * print(v3.heading());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Three arrows extend from the center of a gray square. A red arrow points to the right, a blue arrow points to the left, and a purple arrow points down.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * // Create p5.Vector objects.
- * let v0 = createVector(50, 50);
- * let v1 = createVector(20, 0);
- * let v2 = createVector(-40, 0);
- *
- * // Create a p5.Vector that's halfway between v1 and v2.
- * let v3 = p5.Vector.slerp(v1, v2, 0.5);
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v2, 'blue');
- *
- * // Draw the purple arrow.
- * drawArrow(v0, v3, 'purple');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a normal vector.
- * let n = createVector(0, 1);
- * // Create a vector to reflect.
- * let v = createVector(4, 6);
- *
- * // Reflect v about n.
- * v.reflect(n);
- *
- * // Prints "p5.Vector Object : [4, -6, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a normal vector.
- * let n = createVector(0, 1);
- *
- * // Create a vector to reflect.
- * let v0 = createVector(4, 6);
- *
- * // Create a reflected vector.
- * let v1 = p5.Vector.reflect(v0, n);
- *
- * // Prints "p5.Vector Object : [4, -6, 0]" to the console.
- * print(v1.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('Three arrows extend from the center of a gray square with a vertical line down its middle. A black arrow points to the right, a blue arrow points to the bottom left, and a red arrow points to the bottom right.');
- * }
- * function draw() {
- * background(200);
- *
- * // Draw a vertical line.
- * line(50, 0, 50, 100);
- *
- * // Create a normal vector.
- * let n = createVector(1, 0);
- *
- * // Center.
- * let v0 = createVector(50, 50);
- *
- * // Create a vector to reflect.
- * let v1 = createVector(30, 40);
- *
- * // Create a reflected vector.
- * let v2 = p5.Vector.reflect(v1, n);
- *
- * // Scale the normal vector for drawing.
- * n.setMag(30);
- *
- * // Draw the black arrow.
- * drawArrow(v0, n, 'black');
- *
- * // Draw the red arrow.
- * drawArrow(v0, v1, 'red');
- *
- * // Draw the blue arrow.
- * drawArrow(v0, v2, 'blue');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = createVector(20, 30);
- *
- * // Prints "[20, 30, 0]" to the console.
- * print(v.array());
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(10, 20, 30);
- * let v1 = createVector(10, 20, 30);
- * let v2 = createVector(0, 0, 0);
- *
- * // Prints "true" to the console.
- * print(v0.equals(v1));
- *
- * // Prints "false" to the console.
- * print(v0.equals(v2));
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(5, 10, 20);
- * let v1 = createVector(5, 10, 20);
- * let v2 = createVector(13, 10, 19);
- *
- * // Prints "true" to the console.
- * print(v0.equals(v1.x, v1.y, v1.z));
- *
- * // Prints "false" to the console.
- * print(v0.equals(v2.x, v2.y, v2.z));
- * }
- *
- *
- * function setup() {
- * // Create p5.Vector objects.
- * let v0 = createVector(10, 20, 30);
- * let v1 = createVector(10, 20, 30);
- * let v2 = createVector(0, 0, 0);
- *
- * // Prints "true" to the console.
- * print(p5.Vector.equals(v0, v1));
- *
- * // Prints "false" to the console.
- * print(p5.Vector.equals(v0, v2));
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = p5.Vector.fromAngle(0);
- *
- * // Prints "p5.Vector Object : [1, 0, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = p5.Vector.fromAngle(0, 30);
- *
- * // Prints "p5.Vector Object : [30, 0, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * describe('A black arrow extends from the center of a gray square. It points to the right.');
- * }
- * function draw() {
- * background(200);
- *
- * // Create a p5.Vector to the center.
- * let v0 = createVector(50, 50);
- *
- * // Create a p5.Vector with an angle 0 and magnitude 30.
- * let v1 = p5.Vector.fromAngle(0, 30);
- *
- * // Draw the black arrow.
- * drawArrow(v0, v1, 'black');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = p5.Vector.fromAngles(0, 0);
- *
- * // Prints "p5.Vector Object : [0, -1, 0]" to the console.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100, WEBGL);
- *
- * describe('A light shines on a pink sphere as it orbits.');
- * }
- *
- * function draw() {
- * background(0);
- *
- * // Calculate the ISO angles.
- * let theta = frameCount * 0.05;
- * let phi = 0;
- *
- * // Create a p5.Vector object.
- * let v = p5.Vector.fromAngles(theta, phi, 100);
- *
- * // Create a point light using the p5.Vector.
- * let c = color('deeppink');
- * pointLight(c, v);
- *
- * // Style the sphere.
- * fill(255);
- * noStroke();
- *
- * // Draw the sphere.
- * sphere(35);
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = p5.Vector.random2D();
- *
- * // Prints "p5.Vector Object : [x, y, 0]" to the console
- * // where x and y are small random numbers.
- * print(v.toString());
- * }
- *
- *
- * function setup() {
- * createCanvas(100, 100);
- *
- * // Slow the frame rate.
- * frameRate(1);
- *
- * describe('A black arrow in extends from the center of a gray square. It changes direction once per second.');
- * }
- *
- * function draw() {
- * background(200);
- *
- * // Create a p5.Vector to the center.
- * let v0 = createVector(50, 50);
- *
- * // Create a random p5.Vector.
- * let v1 = p5.Vector.random2D();
- *
- * // Scale v1 for drawing.
- * v1.mult(30);
- *
- * // Draw the black arrow.
- * drawArrow(v0, v1, 'black');
- * }
- *
- * // 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();
- * }
- *
- *
- * function setup() {
- * // Create a p5.Vector object.
- * let v = p5.Vector.random3D();
- *
- * // Prints "p5.Vector Object : [x, y, z]" to the console
- * // where x, y, and z are small random numbers.
- * print(v.toString());
- * }
- *
- *