processing.core
Class PVector

java.lang.Object
  extended by processing.core.PVector

public class PVector
extends java.lang.Object

A class to describe a two or three dimensional vector.

The result of all functions are applied to the vector itself, with the exception of cross(), which returns a new PVector (or writes to a specified 'target' PVector). That is, add() will add the contents of one vector to this one. Using add() with additional parameters allows you to put the result into a new PVector. Functions that act on multiple vectors also include static versions. Because creating new objects can be computationally expensive, most functions include an optional 'target' PVector, so that a new PVector object is not created with each operation.

Initially based on the Vector3D class by Dan Shiffman.


Field Summary
 float x
          The x component of the vector.
 float y
          The y component of the vector.
 float z
          The z component of the vector.
 
Constructor Summary
PVector()
          Constructor for an empty vector: x, y, and z are set to 0.
PVector(float x, float y)
          Constructor for a 2D vector: z coordinate is set to 0.
PVector(float x, float y, float z)
          Constructor for a 3D vector.
 
Method Summary
 void add(float x, float y, float z)
           
 void add(PVector v)
          Add a vector to this vector
static PVector add(PVector v1, PVector v2)
          Add two vectors
static PVector add(PVector v1, PVector v2, PVector target)
          Add two vectors into a target vector
static float angleBetween(PVector v1, PVector v2)
          Calculate the angle between two vectors, using the dot product
 float[] array()
          Return a representation of this vector as a float array.
 PVector cross(PVector v)
          Return a vector composed of the cross product between this and another.
 PVector cross(PVector v, PVector target)
          Perform cross product between this and another vector, and store the result in 'target'.
static PVector cross(PVector v1, PVector v2, PVector target)
           
 float dist(PVector v)
          Calculate the Euclidean distance between two points (considering a point as a vector object)
static float dist(PVector v1, PVector v2)
          Calculate the Euclidean distance between two points (considering a point as a vector object)
 void div(float n)
          Divide this vector by a scalar
 void div(PVector v)
          Divide each element of one vector by the elements of another vector.
static PVector div(PVector v, float n)
          Divide a vector by a scalar and return the result in a new vector.
static PVector div(PVector v, float n, PVector target)
           
static PVector div(PVector v1, PVector v2)
          Multiply each element of one vector by the individual elements of another vector, and return the result as a new PVector.
static PVector div(PVector v1, PVector v2, PVector target)
          Divide each element of one vector by the individual elements of another vector, and write the result into a target vector.
 float dot(float x, float y, float z)
           
 float dot(PVector v)
          Calculate the dot product with another vector
 PVector get()
          Get a copy of this vector.
 float[] get(float[] target)
           
 float heading2D()
          Calculate the angle of rotation for this vector (only 2D vectors)
 void limit(float max)
          Limit the magnitude of this vector
 float mag()
          Calculate the magnitude (length) of the vector
 void mult(float n)
          Multiply this vector by a scalar
 void mult(PVector v)
          Multiply each element of one vector by the elements of another vector.
static PVector mult(PVector v, float n)
          Multiply a vector by a scalar
static PVector mult(PVector v, float n, PVector target)
          Multiply a vector by a scalar, and write the result into a target PVector.
static PVector mult(PVector v1, PVector v2)
          Multiply each element of one vector by the individual elements of another vector, and return the result as a new PVector.
static PVector mult(PVector v1, PVector v2, PVector target)
          Multiply each element of one vector by the individual elements of another vector, and write the result into a target vector.
 void normalize()
          Normalize the vector to length 1 (make it a unit vector)
 PVector normalize(PVector target)
          Normalize this vector, storing the result in another vector.
 void set(float[] source)
          Set the x, y (and maybe z) coordinates using a float[] array as the source.
 void set(float x, float y, float z)
          Set x, y, and z coordinates.
 void set(PVector v)
          Set x, y, and z coordinates from a Vector3D object.
 void sub(float x, float y, float z)
           
 void sub(PVector v)
          Subtract a vector from this vector
static PVector sub(PVector v1, PVector v2)
          Subtract one vector from another
static PVector sub(PVector v1, PVector v2, PVector target)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public float x
The x component of the vector.


y

public float y
The y component of the vector.


z

public float z
The z component of the vector.

Constructor Detail

PVector

public PVector()
Constructor for an empty vector: x, y, and z are set to 0.


PVector

public PVector(float x,
               float y,
               float z)
Constructor for a 3D vector.

Parameters:
x - the x coordinate.
y - the y coordinate.
z - the y coordinate.

PVector

public PVector(float x,
               float y)
Constructor for a 2D vector: z coordinate is set to 0.

Parameters:
x - the x coordinate.
y - the y coordinate.
Method Detail

set

public void set(float x,
                float y,
                float z)
Set x, y, and z coordinates.

Parameters:
x - the x coordinate.
y - the y coordinate.
z - the z coordinate.

set

public void set(PVector v)
Set x, y, and z coordinates from a Vector3D object.

Parameters:
v - the PVector object to be copied

set

public void set(float[] source)
Set the x, y (and maybe z) coordinates using a float[] array as the source.

Parameters:
source - array to copy from

get

public PVector get()
Get a copy of this vector.


get

public float[] get(float[] target)

mag

public float mag()
Calculate the magnitude (length) of the vector

Returns:
the magnitude of the vector

add

public void add(PVector v)
Add a vector to this vector

Parameters:
v - the vector to be added

add

public void add(float x,
                float y,
                float z)

add

public static PVector add(PVector v1,
                          PVector v2)
Add two vectors

Parameters:
v1 - a vector
v2 - another vector
Returns:
a new vector that is the sum of v1 and v2

add

public static PVector add(PVector v1,
                          PVector v2,
                          PVector target)
Add two vectors into a target vector

Parameters:
v1 - a vector
v2 - another vector
target - the target vector (if null, a new vector will be created)
Returns:
a new vector that is the sum of v1 and v2

sub

public void sub(PVector v)
Subtract a vector from this vector

Parameters:
v - the vector to be subtracted

sub

public void sub(float x,
                float y,
                float z)

sub

public static PVector sub(PVector v1,
                          PVector v2)
Subtract one vector from another

Parameters:
v1 - a vector
v2 - another vector
Returns:
a new vector that is v1 - v2

sub

public static PVector sub(PVector v1,
                          PVector v2,
                          PVector target)

mult

public void mult(float n)
Multiply this vector by a scalar

Parameters:
n - the value to multiply by

mult

public static PVector mult(PVector v,
                           float n)
Multiply a vector by a scalar

Parameters:
v - a vector
n - scalar
Returns:
a new vector that is v1 * n

mult

public static PVector mult(PVector v,
                           float n,
                           PVector target)
Multiply a vector by a scalar, and write the result into a target PVector.

Parameters:
v - a vector
n - scalar
target - PVector to store the result
Returns:
the target vector, now set to v1 * n

mult

public void mult(PVector v)
Multiply each element of one vector by the elements of another vector.

Parameters:
v - the vector to multiply by

mult

public static PVector mult(PVector v1,
                           PVector v2)
Multiply each element of one vector by the individual elements of another vector, and return the result as a new PVector.


mult

public static PVector mult(PVector v1,
                           PVector v2,
                           PVector target)
Multiply each element of one vector by the individual elements of another vector, and write the result into a target vector.

Parameters:
v1 - the first vector
v2 - the second vector
target - PVector to store the result

div

public void div(float n)
Divide this vector by a scalar

Parameters:
n - the value to divide by

div

public static PVector div(PVector v,
                          float n)
Divide a vector by a scalar and return the result in a new vector.

Parameters:
v - a vector
n - scalar
Returns:
a new vector that is v1 / n

div

public static PVector div(PVector v,
                          float n,
                          PVector target)

div

public void div(PVector v)
Divide each element of one vector by the elements of another vector.


div

public static PVector div(PVector v1,
                          PVector v2)
Multiply each element of one vector by the individual elements of another vector, and return the result as a new PVector.


div

public static PVector div(PVector v1,
                          PVector v2,
                          PVector target)
Divide each element of one vector by the individual elements of another vector, and write the result into a target vector.

Parameters:
v1 - the first vector
v2 - the second vector
target - PVector to store the result

dist

public float dist(PVector v)
Calculate the Euclidean distance between two points (considering a point as a vector object)

Parameters:
v - another vector
Returns:
the Euclidean distance between

dist

public static float dist(PVector v1,
                         PVector v2)
Calculate the Euclidean distance between two points (considering a point as a vector object)

Parameters:
v1 - a vector
v2 - another vector
Returns:
the Euclidean distance between v1 and v2

dot

public float dot(PVector v)
Calculate the dot product with another vector

Returns:
the dot product

dot

public float dot(float x,
                 float y,
                 float z)

cross

public PVector cross(PVector v)
Return a vector composed of the cross product between this and another.


cross

public PVector cross(PVector v,
                     PVector target)
Perform cross product between this and another vector, and store the result in 'target'. If target is null, a new vector is created.


cross

public static PVector cross(PVector v1,
                            PVector v2,
                            PVector target)

normalize

public void normalize()
Normalize the vector to length 1 (make it a unit vector)


normalize

public PVector normalize(PVector target)
Normalize this vector, storing the result in another vector.

Parameters:
target - Set to null to create a new vector
Returns:
a new vector (if target was null), or target

limit

public void limit(float max)
Limit the magnitude of this vector

Parameters:
max - the maximum length to limit this vector

heading2D

public float heading2D()
Calculate the angle of rotation for this vector (only 2D vectors)

Returns:
the angle of rotation

angleBetween

public static float angleBetween(PVector v1,
                                 PVector v2)
Calculate the angle between two vectors, using the dot product

Parameters:
v1 - a vector
v2 - another vector
Returns:
the angle between the vectors

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

array

public float[] array()
Return a representation of this vector as a float array. This is only for temporary use. If used in any other fashion, the contents should be copied by using the get() command to copy into your own array.