Chapter 1. Vectors
“Roger, Roger. What’s our vector, Victor?”
— Captain Oveur (Airplane)
This book is all about looking at the world around us and coming up with clever ways to simulate that world with code. Divided into three parts, the book will start by looking at basic physics—how an apple falls from a tree, a pendulum swings in the air, the earth revolves around the sun, etc. Absolutely everything contained within the first five chapters of this book requires the use of the most basic building block for programming motion—the vector. And so this is where I begin our story.
Now, the word vector can mean a lot of different things. Vector is the name of a New Wave rock band formed in Sacramento, CA in the early 1980s. It’s the name of a breakfast cereal manufactured by Kellogg’s Canada. In the field of epidemiology, a vector is used to describe an organism that transmits infection from one host to another. In the C++ programming language, a vector (std::vector) is an implementation of a dynamically resizable array data structure. While all these definitions are interesting, they’re not what we’re looking for. What I want to focus on is a Euclidean vector (named for the Greek mathematician Euclid and also known as a geometric vector). When you see the term “vector” in this book, you can assume it refers to a Euclidean vector, defined as an entity that has both magnitude and direction.

A vector is typically drawn as a arrow; the direction is indicated by where the arrow is pointing, and the magnitude by the length of the arrow itself.
In the Figure 1.1, the vector is drawn as an arrow from point A to point B and serves as an instruction for how to travel from A to B.
1.1 Vectors, You Complete Me
Before we dive into more of the details about vectors, I’d like to create a basic p5.js example that demonstrates why you should care about vectors in the first place. If you’ve read any of the introductory p5.js textbooks or taken an introduction to creative coding course (and hopefully you’ve done one of these things to help prepare you for this book), you probably, at one point or another, learned how to write a simple bouncing ball sketch.
Example 1.1: Bouncing ball with no vectors
let x = 100;
let y = 100;
let xspeed = 1;
let yspeed = 3.3;
function setup() {
createCanvas(640, 360);
background(255);
}
Remember how p5 works? setup() is executed once when the sketch starts and draw() loops forever and ever (until you quit).
function draw() {
background(255);
x = x + xspeed;
y = y + yspeed;
Move the ball according to its speed.
if ((x > width) || (x < 0)) {
xspeed = xspeed * -1;
}
if ((y > height) || (y < 0)) {
yspeed = yspeed * -1;
}
Check for bouncing.
stroke(0);
fill(175);
ellipse(x, y, 16, 16);
Display the ball at the position (x,y).
}
In the above example, there is a very simple world—a blank canvas with a circular shape (a “ball”) traveling around. This ball has some properties, which are represented in the code as variables.
| Position | x and y |
| Speed | xspeed and yspeed |
In a more sophisticated sketch, you might have many more variables:
| Acceleration | xacceleration and yacceleration |
| Target position | xtarget and ytarget |
| Wind | xwind and ywind |
| Friction | xfriction and yfriction |
It’s becoming clearer that for every concept in this world (wind, position, acceleration, etc.), I’ll need two variables. And this is only a two-dimensional world. In a 3D world, I’d need x, y, z, xspeed, yspeed, zspeed, and so on.
Wouldn’t it be nice if I could simplify the code and use fewer variables?
Instead of:
let x;
let y;
let xspeed;
let yspeed;
I would love to have…
let position;
let speed;
Taking this first step in using vectors won’t allow me to do anything new. Just adding vectors won’t magically make your Processing sketches simulate physics. However, they will simplify your code and provide a set of functions for common mathematical operations that happen over and over and over again while programming motion.
As an introduction to vectors, I’m going to stick to two dimensions for quite some time (at least the first several chapters). All of these examples can be fairly easily extended to three dimensions (and the class I will use—p5.Vector—allows for three dimensions.) However, it’s easier to start with just two.
1.2 Vectors for p5.js Programmers
One way to think of a vector is the difference between two points. Consider how you might go about providing instructions to walk from one point to another.
Here are some vectors and possible translations:

| (-15, 3) | Walk fifteen steps west; turn and walk three steps north. |
| (3, 4) | Walk three steps east; turn and walk four steps north. |
| (2, -1) | Walk two steps east; turn and walk one step south. |
You’ve probably done this before when programming motion. For every frame of animation (i.e. a single cycle through p5’s draw() loop), you instruct each object on the screen to move a certain number of pixels horizontally and a certain number of pixels vertically.

For every frame:
new position = velocity applied to current position
If velocity is a vector (the difference between two points), what is position? Is it a vector too? Technically, you could argue that position is not a vector, since it’s not describing how to move from one point to another—it’s describing a singular point in space.
Nevertheless, another way to describe a position is the path taken from the origin to reach that position. Hence, a position is the vector representing the difference between position and origin.

Let’s examine the underlying data for both position and velocity. In the bouncing ball example, I had the following:
| position | x,y |
| velocity | xspeed,yspeed |
Notice how I am storing the same data for both—two floating point numbers, an x and a y. If I were to write a vector class myself, I’d start with something rather basic:
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
At its core, a Vector is just a convenient way to store two values (or three, as you’ll see in 3D examples).
And so this …
let x = 100;
let y = 100;
let xspeed = 1;
let yspeed = 3.3;
becomes …
let position = createVector(100, 100);
let velocity = createVector(1, 3.3);
I'll note that in the above code the vector objects are not created, as you might expect, by invoking a constructor function. Instead of new Vector(x, y) (or more accurately in p5 new p5.Vector(x, y)), createVector(x, y) is called. The createVector() function is included in a p5.js as a helper function to take care of some details behind the scenes as well as simplify the code. Except in special circumstances, p5.Vector objects should always be created with createVector().
Now that I have two vector objects (position and velocity), I’m ready to implement the algorithm for motion—position = position + velocity. In Example 1.1, without vectors, I had:
x = x + xspeed;
y = y + yspeed;
Add each speed to each position.
In an ideal world, I would be able to rewrite the above as:
position = position + velocity;
Add the velocity vector to the position vector.
However, in JavaScript, the addition operator + is reserved for primitive values (integers, floats, etc.) only. JavaScript doesn’t know how to add two p5.Vector objects together any more than it knows how to add two p5.Font objects or p5.Image objects. Fortunately, the p5.Vector class includes functions for common mathematical operations.
1.3 Vector Addition
Before I continue looking at the p5.Vector class and its add() method (purely for the sake of learning since it’s already implemented for us in p5.js itself), let’s examine vector addition using the notation found in math and physics textbooks.
Vectors are typically written either in boldface type or with an arrow on top. For the purposes of this book, to distinguish a vector from a scalar (scalar refers to a single value, such as an integer or a floating point number), we’ll use the arrow notation:
- Vector:
- Scalar:
Let’s say I have the following two vectors:

Each vector has two components, an x and a y. To add two vectors together, add both x’s and both y’s.

In other words:
can be written as:
Then, replacing u and v with their values from Figure 1.6, you get:
which means that:
Finally, writing that as a vector:
Now that I've covered how to add two vectors together, you can look at how addition is implemented in the p5.Vector class itself. Let’s write a function called add() that takes another Vector object as its argument.
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(v) {
this.y = this.y + v.y;
this.x = this.x + v.x;
}
New! A function to add another Vector to this Vector. Simply add the x components and the y; components together.
}
Basic Number Properties with Vectors
Addition with vectors follow the same algebraic rules as with real numbers.
The commutative rule:
Variables for position and speed of ball.