From 627b04d8c8c7ed368a762980780c8df14a860e2f Mon Sep 17 00:00:00 2001 From: Nick McIntyre Date: Tue, 12 Mar 2024 13:59:37 -0500 Subject: [PATCH 1/3] Update p5.Vector references --- src/math/p5.Vector.js | 1828 +++++++++++++++++++++++++++++------------ 1 file changed, 1316 insertions(+), 512 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 473816a9ea..4ea09333da 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -9,8 +9,10 @@ import * as constants from '../core/constants'; /** - * A class to describe a two or three-dimensional vector. A vector is like an - * arrow pointing in space. Vectors have both magnitude (length) and + * 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 @@ -22,6 +24,9 @@ import * as constants from '../core/constants'; * 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. @@ -30,14 +35,26 @@ import * as constants from '../core/constants'; * @example *
* - * let p1 = createVector(25, 25); - * let p2 = createVector(75, 75); + * 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); * - * strokeWeight(5); - * point(p1); - * point(p2.x, p2.y); + * // 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.'); + * describe('Two black dots on a gray square, one at the top left and the other at the bottom right.'); + * } * *
* @@ -48,23 +65,29 @@ import * as constants from '../core/constants'; * * function setup() { * createCanvas(100, 100); - * pos = createVector(width / 2, height); + * + * // 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 = height; + * pos.y = 100; * } * + * // Draw the dot. * strokeWeight(5); * point(pos); - * - * describe('A black dot moves from bottom to top on a gray square. The dot reappears at the bottom when it reaches the top.'); * } * * @@ -111,15 +134,20 @@ p5.Vector = class { } /** - * Returns a string representation of a vector. This method is useful for - * printing vectors to the console while debugging. + * Returns a string representation of a vector. + * + * Calling `toString()` is useful for printing vectors to the console while + * debugging. + * * @method toString * @return {String} string representation of the vector. + * * @example *
* * function setup() { * let v = createVector(20, 30); + * * // Prints 'p5.Vector Object : [20, 30, 0]'. * print(v.toString()); * } @@ -131,9 +159,16 @@ p5.Vector = class { } /** - * Sets the `x`, `y`, and `z` components of the vector using separate numbers, - * a p5.Vector object, or an array of numbers. - * Calling `set()` with no arguments sets the vector's components to 0. + * Sets the vector's `x`, `y`, and `z` components. + * + * `set()` can use separate numbers, as in `v.set(1, 2, 3)`, a + * p5.Vector object, as in `v.set(v2)`, or an + * array of numbers, as in `v.set([1, 2, 3])`. + * + * If a value isn't provided for a component, it will be set to 0. For + * example, `v.set(4, 5)` sets `v.x` to 4, `v.y` to 5, and `v.z` to 0. + * Calling `set()` with no arguments, as in `v.set()`, sets all the vector's + * components to 0. * * @method set * @param {Number} [x] x component of the vector. @@ -143,27 +178,37 @@ p5.Vector = class { * @example *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); * - * // Top left. - * let pos = createVector(25, 25); - * point(pos); + * background(200); + * + * // Style the points. + * strokeWeight(5); + * + * // Top left. + * let pos = createVector(25, 25); + * point(pos); * - * // Top right. - * pos.set(75, 25); - * point(pos); + * // Top right. + * // set() with numbers. + * pos.set(75, 25); + * point(pos); * - * // Bottom right. - * let p2 = createVector(75, 75); - * pos.set(p2); - * point(pos); + * // Bottom right. + * // set() with a p5.Vector. + * let p2 = createVector(75, 75); + * pos.set(p2); + * point(pos); * - * // Bottom left. - * let arr = [25, 75]; - * pos.set(arr); - * 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.'); + * describe('Four black dots arranged in a square on a gray background.'); + * } * *
*/ @@ -196,16 +241,27 @@ p5.Vector = class { * * @method copy * @return {p5.Vector} copy of the p5.Vector object. + * * @example *
* - * let pos = createVector(50, 50); - * let pc = pos.copy(); + * function setup() { + * createCanvas(100 ,100); + * + * background(200); + * + * // Create a p5.Vector object. + * let pos = createVector(50, 50); * - * strokeWeight(5); - * point(pc); + * // 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.'); + * describe('A black point drawn in the middle of a gray square.'); + * } * *
*/ @@ -224,9 +280,15 @@ p5.Vector = class { } /** - * Adds to a vector's `x`, `y`, and `z` components using separate numbers, - * another p5.Vector object, or an array of numbers. - * Calling `add()` with no arguments has no effect. + * Adds to a vector's `x`, `y`, and `z` components. + * + * `add()` can use separate numbers, as in `v.add(1, 2, 3)`, + * another p5.Vector object, as in `v.add(v2)`, or + * an array of numbers, as in `v.add([1, 2, 3])`. + * + * If a value isn't provided for a component, it won't change. For + * example, `v.add(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`. + * Calling `add()` with no arguments, as in `v.add()`, has no effect. * * The static version of `add()`, as in `p5.Vector.add(v2, v1)`, returns a new * p5.Vector object and doesn't change the @@ -237,71 +299,99 @@ p5.Vector = class { * @param {Number} [y] y component of the vector to be added. * @param {Number} [z] z component of the vector to be added. * @chainable + * * @example *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * // Top left. - * let pos = createVector(25, 25); - * point(pos); + * // Style the points. + * strokeWeight(5); + * + * // Top left. + * let pos = createVector(25, 25); + * point(pos); * - * // Top right. - * pos.add(50, 0); - * point(pos); + * // Top right. + * // Add numbers. + * pos.add(50, 0); + * point(pos); * - * // Bottom right. - * let p2 = createVector(0, 50); - * pos.add(p2); - * point(pos); + * // Bottom right. + * // Add a p5.Vector. + * let p2 = createVector(0, 50); + * pos.add(p2); + * point(pos); * - * // Bottom left. - * let arr = [-50, 0]; - * pos.add(arr); - * 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.'); + * describe('Four black dots arranged in a square on a gray background.'); + * } * *
* *
* - * // Top left. - * let p1 = createVector(25, 25); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Top left. + * let p1 = createVector(25, 25); * - * // Center. - * let p2 = createVector(50, 50); + * // Center. + * let p2 = createVector(50, 50); * - * // Bottom right. - * let p3 = p5.Vector.add(p1, p2); + * // Bottom right. + * // Add p1 and p2. + * let p3 = p5.Vector.add(p1, p2); * - * strokeWeight(5); - * point(p1); - * point(p2); - * point(p3); + * // 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.'); + * 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'); - * - * 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.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -375,11 +465,19 @@ p5.Vector = class { /** * Performs modulo (remainder) division with a vector's `x`, `y`, and `z` - * components using separate numbers, another - * p5.Vector object, or an array of numbers. + * components. + * + * `rem()` can use separate numbers, as in `v.rem(1, 2, 3)`, + * another p5.Vector object, as in `v.rem(v2)`, or + * an array of numbers, as in `v.rem([1, 2, 3])`. * - * The static version of `rem()` as in `p5.Vector.rem(v2, v1)`, returns a new - * p5.Vector object and doesn't change the + * If only one value is provided, as in `v.rem(2)`, then all the components + * will be set to their values modulo 2. If two values are provided, as in + * `v.rem(2, 3)`, then `v.z` won't change. Calling `rem()` with no + * arguments, as in `v.rem()`, has no effect. + * + * The static version of `rem()`, as in `p5.Vector.rem(v2, v1)`, returns a + * new p5.Vector object and doesn't change the * originals. * * @method rem @@ -387,46 +485,98 @@ p5.Vector = class { * @param {Number} y y component of divisor vector. * @param {Number} z z component of divisor vector. * @chainable + * * @example *
* - * let v = createVector(3, 4, 5); - * v.rem(2, 3, 4); - * // Prints 'p5.Vector Object : [1, 1, 1]'. - * print(v.toString()); + * 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()); + * } * *
* *
* - * let v1 = createVector(3, 4, 5); - * let v2 = createVector(2, 3, 4); - * v1.rem(v2); + * function setup() { + * // Create p5.Vector objects. + * let v1 = createVector(3, 4, 5); + * let v2 = createVector(2, 3, 4); * - * // Prints 'p5.Vector Object : [1, 1, 1]'. - * print(v1.toString()); + * // Divide a p5.Vector. + * v1.rem(v2); + * + * // Prints 'p5.Vector Object : [1, 1, 1]'. + * print(v1.toString()); + * } * *
* *
* - * let v = createVector(3, 4, 5); - * let arr = [2, 3, 4]; - * v.rem(arr); + * 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()); + * // Prints 'p5.Vector Object : [1, 1, 1]'. + * print(v.toString()); + * } * *
* *
* - * let v1 = createVector(3, 4, 5); - * let v2 = createVector(2, 3, 4); - * let v3 = p5.Vector.rem(v1, v2); + * function setup() { + * // Create p5.Vector objects. + * let v1 = createVector(3, 4, 5); + * let v2 = createVector(2, 3, 4); * - * // Prints 'p5.Vector Object : [1, 1, 1]'. - * print(v3.toString()); + * // Divide without modifying the original vectors. + * let v3 = p5.Vector.rem(v1, v2); + * + * // Prints 'p5.Vector Object : [1, 1, 1]'. + * print(v3.toString()); + * } * *
*/ @@ -488,9 +638,15 @@ p5.Vector = class { } /** - * Subtracts from a vector's `x`, `y`, and `z` components using separate - * numbers, another p5.Vector object, or an array of - * numbers. Calling `sub()` with no arguments has no effect. + * Subtracts from a vector's `x`, `y`, and `z` components. + * + * `sub()` can use separate numbers, as in `v.sub(1, 2, 3)`, another + * p5.Vector object, as in `v.sub(v2)`, or an array + * of numbers, as in `v.sub([1, 2, 3])`. + * + * If a value isn't provided for a component, it won't change. For + * example, `v.sub(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`. + * Calling `sub()` with no arguments, as in `v.sub()`, has no effect. * * The static version of `sub()`, as in `p5.Vector.sub(v2, v1)`, returns a new * p5.Vector object and doesn't change the @@ -501,71 +657,96 @@ p5.Vector = class { * @param {Number} [y] y component of the vector to subtract. * @param {Number} [z] z component of the vector to subtract. * @chainable + * * @example *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * // Bottom right. - * let pos = createVector(75, 75); - * point(pos); + * // Style the points. + * strokeWeight(5); + * + * // Bottom right. + * let pos = createVector(75, 75); + * point(pos); * - * // Top right. - * pos.sub(0, 50); - * point(pos); + * // Top right. + * // Subtract numbers. + * pos.sub(0, 50); + * point(pos); * - * // Top left. - * let p2 = createVector(50, 0); - * pos.sub(p2); - * point(pos); + * // Top left. + * // Subtract a p5.Vector. + * let p2 = createVector(50, 0); + * pos.sub(p2); + * point(pos); * - * // Bottom left. - * let arr = [0, -50]; - * pos.sub(arr); - * 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.'); + * describe('Four black dots arranged in a square on a gray background.'); + * } * *
* *
* - * // Bottom right. - * let p1 = createVector(75, 75); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * // Center. - * let p2 = createVector(50, 50); + * // Create p5.Vector objects. + * let p1 = createVector(75, 75); + * let p2 = createVector(50, 50); * - * // Top left. - * let p3 = p5.Vector.sub(p1, p2); + * // Subtract with modifying the original vectors. + * let p3 = p5.Vector.sub(p1, p2); * - * strokeWeight(5); - * point(p1); - * point(p2); - * point(p3); + * // 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.'); + * 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'); - * - * 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.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -607,10 +788,17 @@ p5.Vector = class { } /** - * Multiplies a vector's `x`, `y`, and `z` components by the same number, - * separate numbers, the components of another - * p5.Vector object, or an array of numbers. Calling - * `mult()` with no arguments has no effect. + * Multiplies a vector's `x`, `y`, and `z` components. + * + * `mult()` can use separate numbers, as in `v.mult(1, 2, 3)`, another + * p5.Vector object, as in `v.mult(v2)`, or an array + * of numbers, as in `v.mult([1, 2, 3])`. + * + * If only one value is provided, as in `v.mult(2)`, then all the components + * will be multiplied by 2. If a value isn't provided for a component, it + * won't change. For example, `v.mult(4, 5)` multiplies `v.x` by, `v.y` by 5, + * and `v.z` by 1. Calling `mult()` with no arguments, as in `v.mult()`, has + * no effect. * * The static version of `mult()`, as in `p5.Vector.mult(v, 2)`, returns a new * p5.Vector object and doesn't change the @@ -622,92 +810,146 @@ p5.Vector = class { * @example *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * let p = createVector(25, 25); - * point(p); + * // Style the points. + * strokeWeight(5); + * + * // Top-left. + * let p = createVector(25, 25); + * point(p); * - * p.mult(2); - * 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.'); + * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * strokeWeight(5); * - * let p = createVector(25, 25); - * point(p); + * // Top-left. + * let p = createVector(25, 25); + * point(p); * - * p.mult(2, 3); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); * - * let p = createVector(25, 25); - * point(p); + * background(200); + * + * // Style the points. + * strokeWeight(5); + * + * // Top-left. + * let p = createVector(25, 25); + * point(p); * - * let arr = [2, 3]; - * p.mult(arr); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Style the points. + * strokeWeight(5); * - * let p = createVector(25, 25); - * point(p); + * // Top-left. + * let p = createVector(25, 25); + * point(p); * - * let p2 = createVector(2, 3); - * p.mult(p2); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * let p = createVector(25, 25); - * point(p); + * // Style the points. + * strokeWeight(5); + * + * // Top-left. + * let p = createVector(25, 25); + * point(p); * - * let p2 = createVector(2, 3); - * let p3 = p5.Vector.mult(p, p2); - * point(p3); + * // 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.'); + * 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'); - * - * describe('Two arrows extending from the top left corner. The blue arrow is twice the length of the red arrow.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -825,10 +1067,17 @@ p5.Vector = class { } /** - * Divides a vector's `x`, `y`, and `z` components by the same number, - * separate numbers, the components of another - * p5.Vector object, or an array of numbers. Calling - * `div()` with no arguments has no effect. + * Divides a vector's `x`, `y`, and `z` components. + * + * `div()` can use separate numbers, as in `v.div(1, 2, 3)`, another + * p5.Vector object, as in `v.div(v2)`, or an array + * of numbers, as in `v.div([1, 2, 3])`. + * + * If only one value is provided, as in `v.div(2)`, then all the components + * will be divided by 2. If a value isn't provided for a component, it + * won't change. For example, `v.div(4, 5)` divides `v.x` by, `v.y` by 5, + * and `v.z` by 1. Calling `div()` with no arguments, as in `v.div()`, has + * no effect. * * The static version of `div()`, as in `p5.Vector.div(v, 2)`, returns a new * p5.Vector object and doesn't change the @@ -840,74 +1089,126 @@ p5.Vector = class { * @example *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Style the points. + * strokeWeight(5); * - * let p = createVector(50, 50); - * point(p); + * // Center. + * let p = createVector(50, 50); + * point(p); * - * p.div(2); - * 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.'); + * describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); * - * let p = createVector(50, 75); - * point(p); + * // Style the points. + * strokeWeight(5); + * + * // Bottom-right. + * let p = createVector(50, 75); + * point(p); * - * p.div(2, 3); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Style the points. + * strokeWeight(5); * - * let p = createVector(50, 75); - * point(p); + * // Bottom-right. + * let p = createVector(50, 75); + * point(p); * - * let arr = [2, 3]; - * p.div(arr); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Style the points. + * strokeWeight(5); * - * let p = createVector(50, 75); - * point(p); + * // Bottom-right. + * let p = createVector(50, 75); + * point(p); * - * let p2 = createVector(2, 3); - * p.div(p2); - * 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.'); + * 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.'); + * } * *
* *
* - * strokeWeight(5); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Style the points. + * strokeWeight(5); * - * let p = createVector(50, 75); - * point(p); + * // Bottom-right. + * let p = createVector(50, 75); + * point(p); * - * let p2 = createVector(2, 3); - * let p3 = p5.Vector.div(p, p2); - * point(p3); + * // 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.'); + * 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.'); + * } * *
* @@ -917,15 +1218,19 @@ p5.Vector = class { * 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); @@ -1059,20 +1364,38 @@ p5.Vector = class { return this; } /** - * Returns the magnitude (length) of the vector. + * Calculates the magnitude (length) of the vector. + * + * Use mag() to calculate the magnitude of a 2D vector + * using components as in `mag(x, y)`. * * @method mag * @return {Number} magnitude of the vector. + * * @example *
* - * let p = createVector(30, 40); - * line(0, 0, p.x, p.y); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Create a p5.Vector object. + * let p = createVector(30, 40); * - * let m = p.mag(); - * text(m, p.x, p.y); + * // Draw a line from the origin. + * line(0, 0, 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.'); + * // 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.'); + * } * *
*/ @@ -1081,20 +1404,34 @@ p5.Vector = class { } /** - * Returns the magnitude (length) of the vector squared. + * Calculates the magnitude (length) of the vector squared. * * @method magSq * @return {number} squared magnitude of the vector. * @example *
* - * let p = createVector(30, 40); - * line(0, 0, p.x, p.y); + * function setup() { + * createCanvas(100, 100); + * + * background(200); + * + * // Create a p5.Vector object. + * let p = createVector(30, 40); * - * let m = p.magSq(); - * text(m, p.x, p.y); + * // Draw a line from the origin. + * line(0, 0, 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.'); + * // 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.'); + * } * *
*/ @@ -1106,11 +1443,13 @@ p5.Vector = class { } /** - * Returns the dot product of two vectors. The dot product is a number that - * describes the overlap between two vectors. Visually, the dot product can be - * thought of as the "shadow" one vector casts on another. The dot product's - * magnitude is largest when two vectors point in the same or opposite - * directions. Its magnitude is 0 when two vectors form a right angle. + * Calculates the dot product of two vectors. + * + * The dot product is a number that describes the overlap between two vectors. + * Visually, the dot product can be thought of as the "shadow" one vector + * casts on another. The dot product's magnitude is largest when two vectors + * point in the same or opposite directions. Its magnitude is 0 when two + * vectors form a right angle. * * The version of `dot()` with one parameter interprets it as another * p5.Vector object. @@ -1130,42 +1469,64 @@ p5.Vector = class { * @example *
* - * let v1 = createVector(3, 4); - * let v2 = createVector(3, 0); - * let dp = v1.dot(v2); - * // Prints "9" to the console. - * print(dp); + * 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); + * } * *
* *
* - * let v1 = createVector(1, 0); - * let v2 = createVector(0, 1); - * let dp = p5.Vector.dot(v1, v2); - * // Prints "0" 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); * - * let v0 = createVector(width / 2, height / 2); + * // Center. + * let v0 = createVector(50, 50); + * + * // Draw the black arrow. * let v1 = createVector(30, 0); * drawArrow(v0, v1, 'black'); * - * let v2 = createVector(mouseX - width / 2, mouseY - height / 2); + * // 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}`, 15, 20); - * - * 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.'); + * text(`v2 • v1 = ${dp}`, 10, 20); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1195,10 +1556,11 @@ p5.Vector = class { } /** - * Returns the cross product of two vectors. The cross product is a vector - * that points straight out of the plane created by two vectors. The cross - * product's magnitude is the area of the parallelogram formed by the original - * two vectors. + * Calculates the cross product of two vectors. + * + * The cross product is a vector that points straight out of the plane created + * by two vectors. The cross product's magnitude is the area of the parallelogram + * formed by the original two vectors. * * The static version of `cross()`, as in `p5.Vector.cross(v1, v2)`, is the same * as calling `v1.cross(v2)`. @@ -1206,24 +1568,37 @@ p5.Vector = class { * @method cross * @param {p5.Vector} v p5.Vector to be crossed. * @return {p5.Vector} cross product as a p5.Vector. + * * @example *
* - * let v1 = createVector(1, 0); - * let v2 = createVector(3, 4); - * 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 = v1.cross(v2); + * + * // Prints "p5.Vector Object : [0, 0, 4]" to the console. + * print(cp.toString()); + * } * *
* *
* - * let v1 = createVector(1, 0); - * let v2 = createVector(3, 4); - * let cp = p5.Vector.cross(v1, 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()); + * } * *
*/ @@ -1239,8 +1614,10 @@ p5.Vector = class { } /** - * Returns the distance between two points represented by vectors. A point's - * coordinates can be thought of as a vector's components. + * Calculates the distance between two points represented by vectors. + * + * A point's coordinates can be represented by the components of a vector + * that extends from the origin to the point. * * The static version of `dist()`, as in `p5.Vector.dist(v1, v2)`, is the same * as calling `v1.dist(v2)`. @@ -1251,48 +1628,82 @@ p5.Vector = class { * @method dist * @param {p5.Vector} v x, y, and z coordinates of a p5.Vector. * @return {Number} distance. + * * @example *
* - * let v1 = createVector(1, 0); - * let v2 = createVector(0, 1); - * 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 = v1.dist(v2); + * + * // Prints "1.414..." to the console. + * print(d); + * } + *
+ *
* *
* - * let v1 = createVector(1, 0); - * let v2 = createVector(0, 1); - * let d = p5.Vector.dist(v1, 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. * let m = floor(v3.mag()); * text(m, 50, 75); - * - * 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.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1326,46 +1737,81 @@ p5.Vector = class { * * @method normalize * @return {p5.Vector} normalized p5.Vector. + * * @example *
* - * let v = createVector(10, 20, 2); - * 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 v = createVector(10, 20, 2); + * + * // Normalize. + * v.normalize(); + * + * // Prints "p5.Vector Object : [0.445..., 0.890..., 0.089...]" to the console. + * print(v.toString()); + * } * *
* *
* - * let v0 = createVector(10, 20, 2); - * 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); + * + * 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); - * - * 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."); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1400,43 +1846,69 @@ p5.Vector = class { * @method limit * @param {Number} max maximum magnitude for the vector. * @chainable + * * @example *
* - * let v = createVector(10, 20, 2); - * 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 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()); + * } * *
* *
* - * let v0 = createVector(10, 20, 2); - * 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() { + * // 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); - * - * 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."); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1472,46 +1944,69 @@ p5.Vector = class { * @method setMag * @param {number} len new length for this vector. * @chainable + * * @example *
* - * let v = createVector(3, 4, 0); - * // Prints "5" to the console. - * print(v.mag()); + * 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); * - * v.setMag(10); - * // Prints "p5.Vector Object : [6, 8, 0]" to the console. - * print(v.toString()); + * // Prints "p5.Vector Object : [6, 8, 0]" to the console. + * print(v.toString()); + * } * *
* *
* - * let v0 = createVector(3, 4, 0); - * 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() { + * // 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); - * drawArrow(origin, v, 'blue'); * - * 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.'); + * // Draw the blue arrow. + * drawArrow(origin, v, 'blue'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1533,8 +2028,10 @@ p5.Vector = class { } /** - * Calculates the angle a 2D vector makes with the positive x-axis. Angles - * increase in the clockwise direction. + * Calculates the angle a 2D vector makes with the positive x-axis. + * + * By convention, the positive x-axis has an angle of 0. Angles increase in + * the clockwise direction. * * If the vector was created with * createVector(), `heading()` returns angles @@ -1545,51 +2042,77 @@ p5.Vector = class { * * @method heading * @return {Number} angle of rotation. + * * @example *
* - * let v = createVector(1, 1); - * // Prints "0.785..." 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(v.heading()); * - * angleMode(DEGREES); - * // Prints "45" to the console. - * print(v.heading()); + * // Use degrees. + * angleMode(DEGREES); + * + * // Prints "45" to the console. + * print(v.heading()); + * } * *
* *
* - * let v = createVector(1, 1); - * // Prints "0.785..." to the console. - * print(p5.Vector.heading(v)); + * 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); * - * angleMode(DEGREES); - * // Prints "45" to the console. - * print(p5.Vector.heading(v)); + * // 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); - * - * 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.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1614,6 +2137,7 @@ p5.Vector = class { /** * Rotates a 2D vector to a specific angle without changing its magnitude. + * * By convention, the positive x-axis has an angle of 0. Angles increase in * the clockwise direction. * @@ -1627,45 +2151,69 @@ p5.Vector = class { * @example *
* - * let v = createVector(0, 1); - * // Prints "1.570..." to the console. - * print(v.heading()); + * 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); * - * v.setHeading(PI); - * // Prints "3.141..." to the console. - * print(v.heading()); + * // Prints "3.141..." to the console. + * print(v.heading()); + * } * *
* *
* - * angleMode(DEGREES); - * let v = createVector(0, 1); - * // Prints "90" 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); * - * v.setHeading(180); - * // Prints "180" to the console. - * print(v.heading()); + * // 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); - * drawArrow(v0, v1, 'blue'); * - * describe('Two arrows extend from the center of a gray square. The red arrow points to the right and the blue arrow points down.'); + * // Draw the blue arrow. + * drawArrow(v0, v1, 'blue'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1693,6 +2241,7 @@ p5.Vector = class { /** * Rotates a 2D vector by an angle without changing its magnitude. + * * By convention, the positive x-axis has an angle of 0. Angles increase in * the clockwise direction. * @@ -1710,47 +2259,78 @@ p5.Vector = class { * @example *
* - * let v = createVector(1, 0); - * // Prints "p5.Vector Object : [1, 0, 0]" to the console. - * print(v.toString()); - * v.rotate(HALF_PI); - * // Prints "p5.Vector Object : [0, 1, 0]" to the console. - * print(v.toString()); + * 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()); + * } * *
* *
* - * angleMode(DEGREES); - * let v = createVector(1, 0); - * // Prints "p5.Vector Object : [1, 0, 0]" to the console. - * print(v.toString()); - * v.rotate(90); - * // 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()); + * } * *
* *
* - * let v0 = createVector(1, 0); - * 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() { + * // 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()); + * } * *
* *
* - * angleMode(DEGREES); - * let v0 = createVector(1, 0); - * 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()); + * 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()); + * } * *
* @@ -1760,20 +2340,26 @@ p5.Vector = class { * 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'); - * - * describe('A black arrow extends from the center of a gray square. The arrow rotates clockwise.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1800,8 +2386,10 @@ p5.Vector = class { } /** - * Returns the angle between two vectors. The angles returned are signed, - * which means that `v1.angleBetween(v2) === -v2.angleBetween(v1)`. + * Calculates the angle between two vectors. + * + * The angles returned are signed, which means that + * `v1.angleBetween(v2) === -v2.angleBetween(v1)`. * * If the vector was created with * createVector(), `angleBetween()` returns @@ -1814,72 +2402,110 @@ p5.Vector = class { * @example *
* - * 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() { + * // 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)); + * } * *
* *
* - * angleMode(DEGREES); - * 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() { + * // 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)); + * } * *
* *
* - * 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() { + * // 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)); + * } * *
* *
* - * angleMode(DEGREES); - * 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() { + * // 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); - * - * 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.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -1915,10 +2541,11 @@ p5.Vector = class { /** * Calculates new `x`, `y`, and `z` components that are proportionally the - * same distance between two vectors. The `amt` parameter is the amount to - * interpolate between the old vector and the new vector. 0.0 keeps all - * components equal to the old vector's, 0.5 is halfway between, and 1.0 sets - * all components equal to the new vector's. + * same distance between two vectors. + * + * The `amt` parameter is the amount to interpolate between the old vector and + * the new vector. 0.0 keeps all components equal to the old vector's, 0.5 is + * halfway between, and 1.0 sets all components equal to the new vector's. * * The static version of `lerp()`, as in `p5.Vector.lerp(v0, v1, 0.5)`, * returns a new p5.Vector object and doesn't change @@ -1935,50 +2562,80 @@ p5.Vector = class { * @example *
* - * let v0 = createVector(1, 1, 1); - * let v1 = createVector(3, 3, 3); - * 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 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()); + * } * *
* *
* - * let v = createVector(1, 1, 1); - * v.lerp(3, 3, 3, 0.5); - * // Prints "p5.Vector Object : [2, 2, 2]" to the console. - * print(v.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()); + * } * *
* *
* - * let v0 = createVector(1, 1, 1); - * let v1 = createVector(3, 3, 3); - * let v2 = p5.Vector.lerp(v0, v1, 0.5); - * // Prints "p5.Vector Object : [2, 2, 2]" to the console. - * print(v2.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'); - * drawArrow(v0, v3, 'purple'); * - * 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.'); + * // Draw the purple arrow. + * drawArrow(v0, v3, 'purple'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -2012,8 +2669,9 @@ p5.Vector = class { } /** - * Calculates a new heading and magnitude that are between two vectors. The - * `amt` parameter is the amount to interpolate between the old vector and + * Calculates a new heading and magnitude that are between two vectors. + * + * The `amt` parameter is the amount to interpolate between the old vector and * the new vector. 0.0 keeps the heading and magnitude equal to the old * vector's, 0.5 sets them halfway between, and 1.0 sets the heading and * magnitude equal to the new vector's. @@ -2036,65 +2694,100 @@ p5.Vector = class { * @example *
* - * let v0 = createVector(3, 0); - * // Prints "3" to the console. - * print(v0.mag()); - * // Prints "0" 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()); * - * let v1 = createVector(0, 1); - * // Prints "1" to the console. - * print(v1.mag()); - * // Prints "1.570..." to the console. - * print(v1.heading()); + * // Prints "1.570..." to the console. + * print(v1.heading()); * - * v0.slerp(v1, 0.5); - * // Prints "2" to the console. - * print(v0.mag()); - * // Prints "0.785..." to the console. - * print(v0.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()); + * } * *
* *
* - * let v0 = createVector(3, 0); - * // Prints "3" to the console. - * print(v0.mag()); - * // Prints "0" 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()); * - * 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 object. + * let v1 = createVector(0, 1); * - * 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()); + * // 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'); - * drawArrow(v0, v3, 'purple'); * - * 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.'); + * // Draw the purple arrow. + * drawArrow(v0, v3, 'purple'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -2178,9 +2871,10 @@ p5.Vector = class { } /** - * Reflects a vector about a line in 2D or a plane in 3D. The orientation of - * the line or plane is described by a normal vector that points away from the - * shape. + * Reflects a vector about a line in 2D or a plane in 3D. + * + * The orientation of the line or plane is described by a normal vector that + * points away from the shape. * * The static version of `reflect()`, as in `p5.Vector.reflect(v, n)`, * returns a new p5.Vector object and doesn't change @@ -2193,44 +2887,78 @@ p5.Vector = class { * @example *
* - * let n = createVector(0, 1); - * let v = createVector(4, 6); - * 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 v = createVector(4, 6); + * + * // Reflect v about n. + * v.reflect(n); + * + * // Prints "p5.Vector Object : [4, -6, 0]" to the console. + * print(v.toString()); + * } * *
* *
* - * let n = createVector(0, 1); - * let v0 = createVector(4, 6); - * let v1 = p5.Vector.reflect(v0, n); - * // Prints "p5.Vector Object : [4, -6, 0]" to the console. - * print(v1.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'); - * drawArrow(v0, v2, 'blue'); * - * 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.'); + * // Draw the blue arrow. + * drawArrow(v0, v2, 'blue'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -2253,16 +2981,20 @@ p5.Vector = class { } /** - * Returns the vector's components as an array of numbers. + * Calculates the vector's components as an array of numbers. * * @method array * @return {Number[]} array with the vector's components. * @example *
* - * let v = createVector(20, 30); - * // Prints "[20, 30, 0]" to the console. - * print(v.array()); + * function setup() { + * // Create a p5.Vector object. + * let v = createVector(20, 30); + * + * // Prints "[20, 30, 0]" to the console. + * print(v.array()); + * } * *
*/ @@ -2271,7 +3003,9 @@ p5.Vector = class { } /** - * Returns `true` if the vector's components are all the same as another + * Checks whether all the vector's components are equal to another vector's. + * + * `equals()` returns `true` if the vector's components are all the same as another * vector's and `false` if not. * * The version of `equals()` with one parameter interprets it as another @@ -2292,40 +3026,52 @@ p5.Vector = class { * @example *
* - * let v0 = createVector(10, 20, 30); - * let v1 = createVector(10, 20, 30); - * let v2 = createVector(0, 0, 0); + * 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 "true" to the console. - * print(v0.equals(v1)); - * // Prints "false" to the console. - * print(v0.equals(v2)); + * // Prints "false" to the console. + * print(v0.equals(v2)); + * } * *
* *
* - * let v0 = createVector(5, 10, 20); - * let v1 = createVector(5, 10, 20); - * let v2 = createVector(13, 10, 19); + * 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 "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)); + * // Prints "false" to the console. + * print(v0.equals(v2.x, v2.y, v2.z)); + * } * *
* *
* - * let v0 = createVector(10, 20, 30); - * let v1 = createVector(10, 20, 30); - * let v2 = createVector(0, 0, 0); + * 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 "true" to the console. - * print(p5.Vector.equals(v0, v1)); - * // Prints "false" to the console. - * print(p5.Vector.equals(v0, v2)); + * // Prints "false" to the console. + * print(p5.Vector.equals(v0, v2)); + * } * *
*/ @@ -2355,35 +3101,60 @@ p5.Vector = class { // Static Methods /** - * Make a new 2D vector from an angle. + * Create a new 2D vector from an angle. * * @method fromAngle * @static * @param {Number} angle desired angle, in radians. Unaffected by angleMode(). * @param {Number} [length] length of the new vector (defaults to 1). * @return {p5.Vector} new p5.Vector object. + * * @example *
* - * 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); + * + * // 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'); - * - * describe('A black arrow extends from the center of a gray square. It points to the right.'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -2408,7 +3179,7 @@ p5.Vector = class { } /** - * Make a new 3D vector from a pair of ISO spherical angles. + * Create a new 3D vector from a pair of ISO spherical angles. * * @method fromAngles * @static @@ -2417,12 +3188,17 @@ p5.Vector = class { * (zero is out of the screen). * @param {Number} [length] length of the new vector (defaults to 1). * @return {p5.Vector} new p5.Vector object. + * * @example *
* - * let v = p5.Vector.fromAngles(0, 0); - * // Prints "p5.Vector Object : [0, -1, 0]" to the console. - * print(v.toString()); + * 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()); + * } * *
* @@ -2430,23 +3206,30 @@ p5.Vector = class { * * function setup() { * createCanvas(100, 100, WEBGL); + * + * describe('A light shines on a pink sphere as it orbits.'); * } * * function draw() { * background(0); * - * fill(255); - * noStroke(); - * + * // 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); * - * sphere(35); + * // Style the sphere. + * fill(255); + * noStroke(); * - * describe('A light shines on a pink sphere as it orbits.'); + * // Draw the sphere. + * sphere(35); * } * *
@@ -2468,7 +3251,7 @@ p5.Vector = class { } /** - * Make a new 2D unit vector with a random heading. + * Create a new 2D unit vector with a random heading. * * @method random2D * @static @@ -2476,28 +3259,45 @@ p5.Vector = class { * @example *
* - * 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() { + * // 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 draw() { - * background(200); + * 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); - * drawArrow(v0, v1, 'black'); * - * describe('A black arrow in extends from the center of a gray square. It changes direction once per second.'); + * // Draw the black arrow. + * drawArrow(v0, v1, 'black'); * } * + * // Draws an arrow between two vectors. * function drawArrow(base, vec, myColor) { * push(); * stroke(myColor); @@ -2519,7 +3319,7 @@ p5.Vector = class { } /** - * Make a new 3D unit vector with a random heading. + * Create a new 3D unit vector with a random heading. * * @method random3D * @static @@ -2527,10 +3327,14 @@ p5.Vector = class { * @example *
* - * 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()); + * 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()); + * } * *
*/ From f31b5199a7bd5bc45042518bc89043690498112b Mon Sep 17 00:00:00 2001 From: Nick McIntyre Date: Tue, 12 Mar 2024 14:03:11 -0500 Subject: [PATCH 2/3] Fix reference for p5.Vector.array() --- src/math/p5.Vector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 4ea09333da..a5625f3db3 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -2981,7 +2981,7 @@ p5.Vector = class { } /** - * Calculates the vector's components as an array of numbers. + * Returns the vector's components as an array of numbers. * * @method array * @return {Number[]} array with the vector's components. From 0f7b776f9e97d03d6d337da703773ef81f3d66f6 Mon Sep 17 00:00:00 2001 From: Qianqian Ye Date: Thu, 14 Mar 2024 13:16:34 -0700 Subject: [PATCH 3/3] Update 'create' to 'creates' for consistent tent --- src/math/p5.Vector.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index a5625f3db3..357f729d60 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -3101,7 +3101,7 @@ p5.Vector = class { // Static Methods /** - * Create a new 2D vector from an angle. + * Creates a new 2D vector from an angle. * * @method fromAngle * @static @@ -3179,7 +3179,7 @@ p5.Vector = class { } /** - * Create a new 3D vector from a pair of ISO spherical angles. + * Creates a new 3D vector from a pair of ISO spherical angles. * * @method fromAngles * @static @@ -3251,7 +3251,7 @@ p5.Vector = class { } /** - * Create a new 2D unit vector with a random heading. + * Creates a new 2D unit vector with a random heading. * * @method random2D * @static @@ -3319,7 +3319,7 @@ p5.Vector = class { } /** - * Create a new 3D unit vector with a random heading. + * Creates a new 3D unit vector with a random heading. * * @method random3D * @static