a port of the Processing Visualization Language


PVector v1, v2;

void setup() {
  v1 = new PVector(40, 20);
  v2 = new PVector(25, 50); 

void draw() {
  ellipse(v1.x, v1.y, 12, 12);
  ellipse(v2.x, v2.y, 12, 12);
  ellipse(v2.x, v2.y, 24, 24);

A class to describe a two or three dimensional vector. This datatype stores two or three variables that are commonly used as a position, velocity, and/or acceleration. Technically, position is a point and velocity and acceleration are vectors, but this is often simplified to consider all three as vectors. For example, if you consider a rectangle moving across the screen, at any given instant it has a position (the object's location, expressed as a point.), a velocity (the rate at which the object's position changes per time unit, expressed as a vector), and acceleration (the rate at which the object's velocity changes per time unit, expressed as a vector). Since vectors represent groupings of values, we cannot simply use traditional addition/multiplication/etc. Instead, we'll need to do some "vector" math, which is made easy by the methods inside the PVector class.

There are additional methods and other ways to use some of these methods in the developers reference.

x x component of the vector
y y component of the vector
z z component of the vector
get() Gets the x, y, z component of the vector
set() Sets the x, y, z component of the vector
mag() Calculates the magnitude (length) of the vector and returns the result as a float
add() Adds one vector to another
sub() Subtracts one vector from another
mult() Multiplies the vector by a scalar
div() Divides the vector by a scalar
dist() Calculate the Euclidean distance between two points
dot() Calculates the dot product
cross() Calculates the cross product
normalize() Normalizes the vector
limit() Limits the magnitude of the vector
angleBetween() Calculates the angle between two vectors
array() Return a representation of the vector as an array


x float: x component
y float: y component
z float: z component
Usage Web & Application

This reference is licensed under the CC BY-NC-SA 2.0 license:

Creative Commons License
Fork me on GitHub