Module

PVector

Methods

# static add(v1, v2, target)

Parameters:
Name Type Description
v1 PVector

a PVector to add

v2 PVector

a PVector to add

target PVector

the vector to receive the result

# static cross(v1, v2) → {Number}

Calculates the cross product of two vectors.

Parameters:
Name Type Description
v1 PVector

the first PVector

v2 PVector

the second PVector

the cross product

Number

# static dist(v1, v2) → {Number}

Calculates the Euclidean distance between two points (considering a point as a vector object).

Parameters:
Name Type Description
v1 PVector

the first PVector

v2 PVector

the second PVector

the distance

Number

# static div(v, n, target)

Divides a vector by a scalar and returns a new vector.

Parameters:
Name Type Description
v PVector

the vector to divide

n Number
target PVector

if undefined a new vector will be created

# static dot(v1, v2) → {Number}

Calculates the dot product of two vectors.

Parameters:
Name Type Description
v1 PVector

the first PVector

v2 PVector

the second PVector

the dot product

Number

# static fromAngle(angle, lengthopt) → {PVector}

Make a new 2D vector from an angle

Parameters:
Name Type Attributes Description
angle Number

the desired angle, in radians

length Number <optional>

the length of the new vector (defaults to 1)

the new PVector object

PVector
Example
function draw() {
  background(200);

  // Create a variable, proportional to the mouseX,
  // varying from 0-360, to represent an angle in degrees.
  angleMode(DEGREES);
  let myDegrees = map(mouseX, 0, width, 0, 360);

  // Display that variable in an onscreen text.
  // (Note the nfc() function to truncate additional decimal places,
  // and the "\xB0" character for the degree symbol.)
  let readout = 'angle = ' + nfc(myDegrees, 1) + '\xB0';
  noStroke();
  fill(0);
  text(readout, 5, 15);

  // Create a PVector using the fromAngle function,
  // and extract its x and y components.
  let v = PVector.fromAngle(radians(myDegrees), 30);
  let vx = v.x;
  let vy = v.y;

  push();
  translate(width / 2, height / 2);
  noFill();
  stroke(150);
  line(0, 0, 30, 0);
  stroke(0);
  line(0, 0, vx, vy);
  pop();
}

# static fromAngles(theta, phi, lengthopt) → {PVector}

Make a new 3D vector from a pair of ISO spherical angles

Parameters:
Name Type Attributes Description
theta Number

the polar angle, in radians (zero is up)

phi Number

the azimuthal angle, in radians (zero is out of the screen)

length Number <optional>

the length of the new vector (defaults to 1)

the new PVector object

PVector
Example
* function setup() {
  createCanvas(100, 100, WEBGL);
  fill(255);
  noStroke();
}
function draw() {
  background(255);

  let t = millis() / 1000;

  // add three point lights
  pointLight(color('#f00'), PVector.fromAngles(t * 1.0, t * 1.3, 100));
  pointLight(color('#0f0'), PVector.fromAngles(t * 1.1, t * 1.2, 100));
  pointLight(color('#00f'), PVector.fromAngles(t * 1.2, t * 1.1, 100));

  sphere(35);
}

# static lerp(v1, v2, amt) → {Number}

Linear interpolate a vector to another vector and return the result as a new vector.

Parameters:
Name Type Description
v1 PVector
v2 PVector
amt Number

the lerped value

Number

# static mag(vecT) → {Number}

Parameters:
Name Type Description
vecT PVector

the vector to return the magnitude of

the magnitude of vecT

Number

# static mult(v, n, target)

Multiplies a vector by a scalar and returns a new vector.

Parameters:
Name Type Description
v PVector

the vector to multiply

n Number
target PVector

if undefined a new vector will be created

# static random2D() → {PVector}

Make a new 2D unit vector from a random angle

the new PVector object

PVector
Example
let v = PVector.random2D();
// May make v's attributes something like:
// [0.61554617, -0.51195765, 0.0] or
// [-0.4695841, -0.14366731, 0.0] or
// [0.6091097, -0.22805278, 0.0]
print(v);

function setup() {
  frameRate(1);
}

function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = PVector.random2D();
  drawArrow(v0, v1.mult(50), 'black');
}

// draw an arrow for a vector at a given base position
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();
}

# static random3D() → {PVector}

Make a new random 3D unit vector.

the new PVector object

PVector
Example
let v = PVector.random3D();
// May make v's attributes something like:
// [0.61554617, -0.51195765, 0.599168] or
// [-0.4695841, -0.14366731, -0.8711202] or
// [0.6091097, -0.22805278, -0.7595902]
print(v);

# static sub(v1, v2, target)

Subtracts one PVector from another and returns a new one. The second vector (v2) is subtracted from the first (v1), resulting in v1-v2.

Parameters:
Name Type Description
v1 PVector

a PVector to subtract from

v2 PVector

a PVector to subtract

target PVector

if undefined a new vector will be created

# inner add(x, yopt, zopt)

Adds x, y, and z components to a vector, adds one vector to another, or adds two independent vectors together. The version of the method that adds two vectors together is a static method and returns a PVector, the others acts directly on the vector. See the examples for more context.

Parameters:
Name Type Attributes Description
x Number

the x component of the vector to be added

y Number <optional>

the y component of the vector to be added

z Number <optional>

the z component of the vector to be added

Example
let v = createVector(1, 2, 3);
v.add(4, 5, 6);
// v's components are set to [5, 7, 9]
// Static method
let v1 = createVector(1, 2, 3);
let v2 = createVector(2, 3, 4);

let v3 = PVector.add(v1, v2);
// v3 has components [3, 5, 7]
print(v3);

// red vector + blue vector = purple vector
function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(mouseX, mouseY);
  drawArrow(v0, v1, 'red');

  let v2 = createVector(-30, 20);
  drawArrow(v1, v2, 'blue');

  let v3 = PVector.add(v1, v2);
  drawArrow(v0, v3, 'purple');
}

// draw an arrow for a vector at a given base position
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();
}

# inner angleBetween(the) → {Number}

Calculates and returns the angle (in radians) between two vectors.

Parameters:
Name Type Description
the PVector

x, y, and z components of a PVector

the angle between (in radians)

Number
Example
let v1 = createVector(1, 0, 0);
let v2 = createVector(0, 1, 0);

let angle = v1.angleBetween(v2);
// angle is PI/2
print(angle);

function draw() {
  background(240);
  let v0 = createVector(50, 50);

  let v1 = createVector(50, 0);
  drawArrow(v0, v1, 'red');

  let v2 = createVector(mouseX - 50, mouseY - 50);
  drawArrow(v0, v2, 'blue');

  let angleBetween = v1.angleBetween(v2);
  noStroke();
  text(
    'angle between: ' +
      angleBetween.toFixed(2) +
      ' radians or ' +
      degrees(angleBetween).toFixed(2) +
      ' degrees',
    10,
    50,
    90,
    50
  );
}

// draw an arrow for a vector at a given base position
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();
}

# inner array() → {Array.<Number>}

Return a representation of this vector as a float array. This is only for temporary use. If used in any other fashion, the contents should be copied by using the PVector.copy() method to copy into your own array.

an Array with the 3 values

Array.<Number>
Example
* function setup() {
  let v = createVector(20, 30);
  print(v.array()); // Prints : Array [20, 30, 0]
}

let v = createVector(10.0, 20.0, 30.0);
let f = v.array();
print(f[0]); // Prints "10.0"
print(f[1]); // Prints "20.0"
print(f[2]); // Prints "30.0"

# inner copy() → {PVector}

Gets a copy of the vector, returns a PVector object.

the copy of the PVector object

PVector
Example
let v1 = createVector(1, 2, 3);
let v2 = v1.copy();
print(v1.x === v2.x && v1.y === v2.y && v1.z === v2.z);
// Prints "true"

# inner cross(v) → {PVector}

Calculates and returns a vector composed of the cross product between two vectors. Both the static and non static methods return a new PVector. See the examples for more context.

Parameters:
Name Type Description
v PVector

PVector to be crossed

PVector composed of cross product

PVector
Example
let v1 = createVector(1, 2, 3);
let v2 = createVector(1, 2, 3);

v1.cross(v2); // v's components are [0, 0, 0]

// Static method
let v1 = createVector(1, 0, 0);
let v2 = createVector(0, 1, 0);

let crossProduct = PVector.cross(v1, v2);
// crossProduct has components [0, 0, 1]
print(crossProduct);

# inner dist(v) → {Number}

Calculates the Euclidean distance between two points (considering a point as a vector object).

Parameters:
Name Type Description
v PVector

the x, y, and z coordinates of a PVector

the distance

Number
Example
let v1 = createVector(1, 0, 0);
let v2 = createVector(0, 1, 0);

let distance = v1.dist(v2); // distance is 1.4142...
print(distance);

// Static method
let v1 = createVector(1, 0, 0);
let v2 = createVector(0, 1, 0);

let distance = PVector.dist(v1, v2);
// distance is 1.4142...
print(distance);

function draw() {
  background(240);

  let v0 = createVector(0, 0);

  let v1 = createVector(70, 50);
  drawArrow(v0, v1, 'red');

  let v2 = createVector(mouseX, mouseY);
  drawArrow(v0, v2, 'blue');

  noStroke();
  text('distance between vectors: ' + v2.dist(v1).toFixed(2), 5, 50, 95, 50);
}

// draw an arrow for a vector at a given base position
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();
}

# inner div(n)

Divide the vector by a scalar. The static version of this method creates a new PVector while the non static version acts on the vector directly. See the examples for more context.

Parameters:
Name Type Description
n number

the number to divide the vector by

Example
let v = createVector(6, 4, 2);
v.div(2); //v's components are set to [3, 2, 1]

// Static method
let v1 = createVector(6, 4, 2);
let v2 = PVector.div(v1, 2);
// v2 has components [3, 2, 1]
print(v2);

function draw() {
  background(240);

  let v0 = createVector(0, 100);
  let v1 = createVector(50, -50);
  drawArrow(v0, v1, 'red');

  let num = map(mouseX, 0, width, 10, 0.5, true);
  let v2 = PVector.div(v1, num);
  drawArrow(v0, v2, 'blue');

  noStroke();
  text('divided by ' + num.toFixed(2), 10, 90);
}

// draw an arrow for a vector at a given base position
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();
}

# inner dot(value) → {Number}

Parameters:
Name Type Description
value PVector

value component of the vector or a PVector

Number

# inner dot(x, yopt, zopt) → {Number}

Calculates the dot product of two vectors. The version of the method that computes the dot product of two independent vectors is a static method. See the examples for more context.

Parameters:
Name Type Attributes Description
x Number

x component of the vector

y Number <optional>

y component of the vector

z Number <optional>

z component of the vector

the dot product

Number
Example
let v1 = createVector(1, 2, 3);
let v2 = createVector(2, 3, 4);

print(v1.dot(v2)); // Prints "20"

//Static method
let v1 = createVector(1, 2, 3);
let v2 = createVector(3, 2, 1);
print(PVector.dot(v1, v2)); // Prints "10"

# inner equals(value) → {Boolean}

Parameters:
Name Type Description
value PVector | Array

the vector to compare

Boolean

# inner equals(xopt, yopt, zopt) → {Boolean}

Equality check against a PVector

Parameters:
Name Type Attributes Description
x Number <optional>

the x component of the vector

y Number <optional>

the y component of the vector

z Number <optional>

the z component of the vector

whether the vectors are equals

Boolean
Example
* let v1 = createVector(5, 10, 20);
let v2 = createVector(5, 10, 20);
let v3 = createVector(13, 10, 19);

print(v1.equals(v2.x, v2.y, v2.z)); // true
print(v1.equals(v3.x, v3.y, v3.z)); // false

let v1 = createVector(10.0, 20.0, 30.0);
let v2 = createVector(10.0, 20.0, 30.0);
let v3 = createVector(0.0, 0.0, 0.0);
print(v1.equals(v2)); // true
print(v1.equals(v3)); // false

# inner heading() → {Number}

Calculate the angle of rotation for this vector (only 2D vectors)

the angle of rotation

Number
Example
* function setup() {
  let v1 = createVector(30, 50);
  print(v1.heading()); // 1.0303768265243125

  v1 = createVector(40, 50);
  print(v1.heading()); // 0.8960553845713439

  v1 = createVector(30, 70);
  print(v1.heading()); // 1.1659045405098132
}

function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = createVector(mouseX - 50, mouseY - 50);

  drawArrow(v0, v1, 'black');

  let myHeading = v1.heading();
  noStroke();
  text(
    'vector heading: ' +
      myHeading.toFixed(2) +
      ' radians or ' +
      degrees(myHeading).toFixed(2) +
      ' degrees',
    10,
    50,
    90,
    50
  );
}

// draw an arrow for a vector at a given base position
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();
}

# inner lerp(x, y, z, amt)

Linear interpolate the vector to another vector

Parameters:
Name Type Description
x Number

the x component

y Number

the y component

z Number

the z component

amt Number

the amount of interpolation; some value between 0.0 (old vector) and 1.0 (new vector). 0.9 is very near the new vector. 0.5 is halfway in between.

Example
let v = createVector(1, 1, 0);

v.lerp(3, 3, 0, 0.5); // v now has components [2,2,0]

let v1 = createVector(0, 0, 0);
let v2 = createVector(100, 100, 0);

let v3 = PVector.lerp(v1, v2, 0.5);
// v3 has components [50,50,0]
print(v3);

let step = 0.01;
let amount = 0;

function draw() {
  background(240);
  let v0 = createVector(0, 0);

  let v1 = createVector(mouseX, mouseY);
  drawArrow(v0, v1, 'red');

  let v2 = createVector(90, 90);
  drawArrow(v0, v2, 'blue');

  if (amount > 1 || amount < 0) {
    step *= -1;
  }
  amount += step;
  let v3 = PVector.lerp(v1, v2, amount);

  drawArrow(v0, v3, 'purple');
}

// draw an arrow for a vector at a given base position
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();
}

# inner limit(max)

Limit the magnitude of this vector to the value used for the max parameter.

Parameters:
Name Type Description
max Number

the maximum magnitude for the vector

Example
let v = createVector(10, 20, 2);
// v has components [10.0, 20.0, 2.0]
v.limit(5);
// v's components are set to
// [2.2271771, 4.4543543, 0.4454354]
* * function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = createVector(mouseX - 50, mouseY - 50);

  drawArrow(v0, v1, 'red');
  drawArrow(v0, v1.limit(35), 'blue');

  noFill();
  ellipse(50, 50, 35 * 2);
}

// draw an arrow for a vector at a given base position
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();
}

# inner mag() → {Number}

Calculates the magnitude (length) of the vector and returns the result as a float (this is simply the equation sqrt(xx + yy + z*z).)

magnitude of the vector

Number
Example
function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(mouseX, mouseY);
  drawArrow(v0, v1, 'black');

  noStroke();
  text('vector length: ' + v1.mag().toFixed(2), 10, 70, 90, 30);
}

// draw an arrow for a vector at a given base position
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();
}
* * let v = createVector(20.0, 30.0, 40.0);
let m = v.mag();
print(m); // Prints "53.85164807134504"

# inner magSq() → {number}

Calculates the squared magnitude of the vector and returns the result as a float (this is simply the equation (xx + yy + z*z).) Faster if the real length is not required in the case of comparing vectors, etc.

squared magnitude of the vector

number
Example
// Static method
let v1 = createVector(6, 4, 2);
print(v1.magSq()); // Prints "56"

function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(mouseX, mouseY);
  drawArrow(v0, v1, 'black');

  noStroke();
  text('vector length squared: ' + v1.magSq().toFixed(2), 10, 45, 90, 55);
}

// draw an arrow for a vector at a given base position
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();
}

# inner mult(n)

Multiply the vector by a scalar. The static version of this method creates a new PVector while the non static version acts on the vector directly. See the examples for more context.

Parameters:
Name Type Description
n Number

the number to multiply with the vector

Example
let v = createVector(1, 2, 3);
v.mult(2);
// v's components are set to [2, 4, 6]

// Static method
let v1 = createVector(1, 2, 3);
let v2 = PVector.mult(v1, 2);
// v2 has components [2, 4, 6]
print(v2);

function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = createVector(25, -25);
  drawArrow(v0, v1, 'red');

  let num = map(mouseX, 0, width, -2, 2, true);
  let v2 = PVector.mult(v1, num);
  drawArrow(v0, v2, 'blue');

  noStroke();
  text('multiplied by ' + num.toFixed(2), 5, 90);
}

// draw an arrow for a vector at a given base position
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();
}

# inner normalize() → {PVector}

Normalize the vector to length 1 (make it a unit vector).

normalized PVector

PVector
Example
let v = createVector(10, 20, 2);
// v has components [10.0, 20.0, 2.0]
v.normalize();
// v's components are set to
// [0.4454354, 0.8908708, 0.089087084]
* * function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = createVector(mouseX - 50, mouseY - 50);

  drawArrow(v0, v1, 'red');
  v1.normalize();
  drawArrow(v0, v1.mult(35), 'blue');

  noFill();
  ellipse(50, 50, 35 * 2);
}

// draw an arrow for a vector at a given base position
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();
}

# inner rotate(angle)

Rotate the vector by an angle (only 2D vectors), magnitude remains the same

Parameters:
Name Type Description
angle number

the angle of rotation

Example
let v = createVector(10.0, 20.0);
// v has components [10.0, 20.0, 0.0]
v.rotate(HALF_PI);
// v's components are set to [-20.0, 9.999999, 0.0]

let angle = 0;
function draw() {
  background(240);

  let v0 = createVector(50, 50);
  let v1 = createVector(50, 0);

  drawArrow(v0, v1.rotate(angle), 'black');
  angle += 0.01;
}

// draw an arrow for a vector at a given base position
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();
}

# inner set(xopt, yopt, zopt)

Sets the x, y, and z component of the vector using two or three separate variables, the data from a PVector, or the values from a float array.

Parameters:
Name Type Attributes Description
x Number <optional>

the x component of the vector

y Number <optional>

the y component of the vector

z Number <optional>

the z component of the vector

Example
function setup() {
  let v = createVector(1, 2, 3);
  v.set(4, 5, 6); // Sets vector to [4, 5, 6]

  let v1 = createVector(0, 0, 0);
  let arr = [1, 2, 3];
  v1.set(arr); // Sets vector to [1, 2, 3]
}

let v0, v1;
function setup() {
  createCanvas(100, 100);

  v0 = createVector(0, 0);
  v1 = createVector(50, 50);
}

function draw() {
  background(240);

  drawArrow(v0, v1, 'black');
  v1.set(v1.x + random(-1, 1), v1.y + random(-1, 1));

  noStroke();
  text('x: ' + round(v1.x) + ' y: ' + round(v1.y), 20, 90);
}

// draw an arrow for a vector at a given base position
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();
}

# inner setMag(len)

Set the magnitude of this vector to the value used for the len parameter.

Parameters:
Name Type Description
len number

the new length for this vector

Example
let v = createVector(10, 20, 2);
// v has components [10.0, 20.0, 2.0]
v.setMag(10);
// v's components are set to [6.0, 8.0, 0.0]

function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(50, 50);

  drawArrow(v0, v1, 'red');

  let length = map(mouseX, 0, width, 0, 141, true);
  v1.setMag(length);
  drawArrow(v0, v1, 'blue');

  noStroke();
  text('magnitude set to: ' + length.toFixed(2), 10, 70, 90, 30);
}

// draw an arrow for a vector at a given base position
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();
}

# inner sub(x, yopt, zopt)

Subtracts x, y, and z components from a vector, subtracts one vector from another, or subtracts two independent vectors. The version of the method that subtracts two vectors is a static method and returns a PVector, the other acts directly on the vector. See the examples for more context.

Parameters:
Name Type Attributes Description
x Number

the x component of the vector to subtract

y Number <optional>

the y component of the vector to subtract

z Number <optional>

the z component of the vector to subtract

Example
let v = createVector(4, 5, 6);
v.sub(1, 1, 1);
// v's components are set to [3, 4, 5]

// Static method
let v1 = createVector(2, 3, 4);
let v2 = createVector(1, 2, 3);

let v3 = PVector.sub(v1, v2);
// v3 has components [1, 1, 1]
print(v3);

// red vector - blue vector = purple vector
function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(70, 50);
  drawArrow(v0, v1, 'red');

  let v2 = createVector(mouseX, mouseY);
  drawArrow(v0, v2, 'blue');

  let v3 = PVector.sub(v1, v2);
  drawArrow(v2, v3, 'purple');
}

// draw an arrow for a vector at a given base position
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();
}

# inner toString() → {String}

Returns a string representation of a vector v by calling String(v) or v.toString(). This method is useful for logging vectors in the console.

String
Example
function setup() {
  let v = createVector(20, 30);
  print(String(v)); // prints "PVector Object : [20, 30, 0]"
}

function draw() {
  background(240);

  let v0 = createVector(0, 0);
  let v1 = createVector(mouseX, mouseY);
  drawArrow(v0, v1, 'black');

  noStroke();
  text(v1.toString(), 10, 25, 90, 75);
}

// draw an arrow for a vector at a given base position
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();
}