This reference is for Processing 2.0+. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.
| Name |
PVector |
| 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);
}
|
| Description |
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.
The methods for this class are extensive. For a complete list, visit the developer's reference.
|
| Fields |
| x |
The x component of the vector |
| y |
The y component of the vector |
| z |
The z component of the vector |
|
| Methods |
| set() |
Set the x, y, and z component of the vector |
| get() |
Get a copy of the vector |
| mag() |
Calculate the magnitude of the vector |
| add() |
Adds x, y, and z components to a vector, one vector to another, or two independent vectors |
| sub() |
Subtract x, y, and z components from a vector, one vector from another, or two independent vectors |
| mult() |
Multiply a vector by a scalar or one vector by another |
| div() |
Divide a vector by a scalar or one vector by another |
| dist() |
Calculate the distance between two points |
| dot() |
Calculate the dot product of two vectors |
| cross() |
Calculate and return the cross product |
| normalize() |
Normalize the vector to a length of 1 |
| limit() |
Limit the magnitude of the vector |
| setMag() |
Set the magnitude of the vector |
| angleBetween() |
Calculate and return the angle between two vectors |
| array() |
Return a representation of the vector as a float array |
|
| Constructor | PVector()
PVector(x, y, z)
PVector(x, y)
|
| Parameters |
| x |
float: the x coordinate. |
| y |
float: the y coordinate. |
| z |
float: the z coordinate. |
|
Updated on December 7, 2012 08:53:45am PST