Class Name
PVector
Description
A class to describe a two or three dimensional vector, specifically a
Euclidean (also known as geometric) vector. A vector is an entity that has
both magnitude and direction. The datatype, however, stores the components of
the vector (x,y for 2D, and x,y,z for 3D). The magnitude and direction can be
accessed via the methods mag() and heading().
In many of the Processing examples, you will see PVector used to
describe a position, velocity, or acceleration. For example, if you consider
a rectangle moving across the screen, at any given instant it has a position
(a vector that points from the origin to its location), 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.
Examples
PVector v1, v2; void setup() { noLoop(); 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); v2.add(v1); ellipse(v2.x, v2.y, 24, 24); }
Constructors
PVector()PVector(x, y, z)PVector(x, y)
Parameters
x(float)the x coordinate.y(float)the y coordinate.z(float)the z coordinate.
Methods
set()Set the components of the vectorrandom2D()Make a new 2D unit vector with a random directionrandom3D()Make a new 3D unit vector with a random directionfromAngle()Make a new 2D unit vector from an anglecopy()Get a copy of the vectormag()Calculate the magnitude of the vectormagSq()Calculate the magnitude of the vector, squaredadd()Adds x, y, and z components to a vector, one vector to another, or two independent vectorssub()Subtract x, y, and z components from a vector, one vector from another, or two independent vectors- Ben Fry and Casey Reas. It is developed by a team of contributors around the world.