Vectors (specifically Euclidean or geometric vectors) are simple data structures that store spatial information representing discrete points, displacement, or forces. Understanding how to work with vectors is one of the most critical skills to learn when working with physically-based simulations and digital morphogenesis.
Vectors can be manipulated using familiar algebraic operations like addition, subtraction, multiplication, and division, which makes them extremely useful when simulating physically-based systems with objects (or agents) in motion. We can have one vector that represents a point in space and another that represents a force (like gravity or wind), then apply that force to the point by adding the two vectors together.
The term "vector" has slightly different meanings and uses in mathematics, physics, machine learning, biology, and more. In the context of digital morphogenesis, you'll most often encounter vectors as they are used in physics, representing discrete physical properties like position, displacement, velocity, direction, and more.
Properties:
- Magnitude / length (
||v||) = the "size" of a vector obtained by taking the square root of the sum of the square of each of the vector's components (an abstraction of the Pythagorean Formula). Mathematically,||v|| = sqrt(v.x*v.x + v.y*v.y + v.z*v.z + ...). - Heading / direction / angle = the direction in which a vector is pointing. Applicable when using vectors to represent forces, but not so much when representing discrete points in space.
Key concepts:
- Unit vector = any vector with a magnitude (length) of exactly 1.
- Normalization = operation whereby a vector is divided by its own magnitude, resulting in a unit vector with the same heading (direction) as the original vector.
Algebraic operations:
| Operation | Using two vectors | Using a vector and a scalar |
|---|---|---|
| Addition | v1 + v2 = {v1.x + v2.x, v1.y + v2.y, ...} |
v + 10 = {v.x + 10, v.y + 10, ...} |
| Subtraction | v1 - v2 = {v1.x - v2.x, v1.y - v2.y, ...} |
v - 10 = {v.x - 10, v.y - 10, ...} |
| Multiplication | v1 * v2 = {v1.x * v2.x, v1.y * v2.y, ...} |
v * 10 = {v.x * 10, v.y * 10, ...} |
| Division | v1 / v2 = {v1.x / v2.x, v1.y / v2.y, ...} |
v / 10 = {v.x / 10, v.y / 10, ...} |
| Dot product | v1 · v2 = (v1.x * v2.x) + (v1.y * v2.y) + ... (produces a single number) |
Not applicable. |
| Cross product | v1 × v2 = mag(v1) * mag(v2) * sin(θ) * n where θ is the angle between v1 and v2, and n is the unit vector at right angles to both v1 and v2. Produces a vector. |
Not applicable. |
Articles:
- Euclidean vector on Wikipedia
- Chapter 1. Vectors in The Nature of Code by Daniel Shiffman
- Vectors from Math is Fun
Videos:
- What is a Vector? - Nature of Code lesson #1.1 by Daniel Shiffman
- Getting Started with Vector Math - Nature of Code lesson #1.2 by Daniel Shiffman
- A Random Vector - Nature of Code lesson #1.3 by Daniel Shiffman
- Unit: Vectors series from Khan Academy
Notable implementations:
- PVector in Processing (JavaDoc reference and source code)
- p5.Vector in p5.js
- Vector2, Vector3, and Vector4 in Three.js
- ofVec2f and ofVec3f in openFrameworks
- Vector data types in GLSL
- Vector Type in HLSL
- Vector2, Vector3, Vector4 in Unity