public class PVector extends Object implements Serializable, org.praxislive.core.DataObject
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.
Modifier and Type | Field and Description |
---|---|
double |
x
( begin auto-generated from PVector_x.xml )
The x component of the vector.
|
double |
y
( begin auto-generated from PVector_y.xml )
The y component of the vector.
|
double |
z
( begin auto-generated from PVector_z.xml )
The z component of the vector.
|
Constructor and Description |
---|
PVector()
Constructor for an empty vector: x, y, and z are set to 0.
|
PVector(double x,
double y)
Constructor for a 2D vector: z coordinate is set to 0.
|
PVector(double x,
double y,
double z)
Constructor for a 3D vector.
|
Modifier and Type | Method and Description |
---|---|
PVector |
add(double x,
double y) |
PVector |
add(double x,
double y,
double z) |
PVector |
add(PVector v)
( begin auto-generated from PVector_add.xml )
Adds x, y, and z components to a vector, adds one vector to another, or
adds two independent vectors together.
|
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 double |
angleBetween(PVector v1,
PVector v2)
( begin auto-generated from PVector_angleBetween.xml )
Calculates and returns the angle (in radians) between two vectors.
|
double[] |
array()
( begin auto-generated from PVector_array.xml )
Return a representation of this vector as a double array.
|
PVector |
copy()
( begin auto-generated from PVector_copy.xml )
Gets a copy of the vector, returns a PVector object.
|
PVector |
cross(PVector v)
( begin auto-generated from PVector_cross.xml )
Calculates and returns a vector composed of the cross product between two
vectors.
|
PVector |
cross(PVector v,
PVector target) |
static PVector |
cross(PVector v1,
PVector v2,
PVector target) |
double |
dist(PVector v)
( begin auto-generated from PVector_dist.xml )
Calculates the Euclidean distance between two points (considering a point
as a vector object).
|
static double |
dist(PVector v1,
PVector v2) |
PVector |
div(double n)
( begin auto-generated from PVector_div.xml )
Divides a vector by a scalar or divides one vector by another.
|
static PVector |
div(PVector v,
double n)
Divide a vector by a scalar and return the result in a new vector.
|
static PVector |
div(PVector v,
double n,
PVector target)
Divide a vector by a scalar and store the result in another vector.
|
double |
dot(double x,
double y,
double z) |
double |
dot(PVector v)
( begin auto-generated from PVector_dot.xml )
Calculates the dot product of two vectors.
|
static double |
dot(PVector v1,
PVector v2) |
boolean |
equals(Object obj) |
static PVector |
fromAngle(double angle)
( begin auto-generated from PVector_sub.xml )
Make a new 2D unit vector from an angle.
|
static PVector |
fromAngle(double angle,
PVector target)
Make a new 2D unit vector from an angle
|
PVector |
get()
Deprecated.
|
double[] |
get(double[] target) |
int |
hashCode() |
double |
heading()
( begin auto-generated from PVector_setMag.xml )
Calculate the angle of rotation for this vector (only 2D vectors)
( end auto-generated )
|
double |
heading2D()
Deprecated.
|
PVector |
lerp(double x,
double y,
double z,
double amt)
Linear interpolate the vector to x,y,z values
|
PVector |
lerp(PVector v,
double amt)
( begin auto-generated from PVector_rotate.xml )
Linear interpolate the vector to another vector
( end auto-generated )
|
static PVector |
lerp(PVector v1,
PVector v2,
double amt)
Linear interpolate between two vectors (returns a new PVector object)
|
PVector |
limit(double max)
( begin auto-generated from PVector_limit.xml )
Limit the magnitude of this vector to the value used for the max
parameter.
|
double |
mag()
( begin auto-generated from PVector_mag.xml )
Calculates the magnitude (length) of the vector and returns the result as
a double (this is simply the equation sqrt(x*x + y*y + z*z).)
( end auto-generated )
|
double |
magSq()
( begin auto-generated from PVector_mag.xml )
Calculates the squared magnitude of the vector and returns the result as
a double (this is simply the equation (x*x + y*y + z*z).) Faster
if the real length is not required in the case of comparing vectors, etc.
|
PVector |
mult(double n)
( begin auto-generated from PVector_mult.xml )
Multiplies a vector by a scalar or multiplies one vector by another.
|
static PVector |
mult(PVector v,
double n) |
static PVector |
mult(PVector v,
double n,
PVector target)
Multiply a vector by a scalar, and write the result into a target
PVector.
|
PVector |
normalize()
( begin auto-generated from PVector_normalize.xml )
Normalize the vector to length 1 (make it a unit vector).
|
PVector |
normalize(PVector target) |
static PVector |
random2D()
( begin auto-generated from PVector_random2D.xml )
Make a new 2D unit vector with a random direction.
|
static PVector |
random2D(PVector target)
Set a 2D vector to a random unit vector with a random direction
|
static PVector |
random3D()
( begin auto-generated from PVector_random3D.xml )
Make a new 3D unit vector with a random direction.
|
static PVector |
random3D(PVector target)
Set a 3D vector to a random unit vector with a random direction
|
void |
readFrom(DataInput in) |
PVector |
rotate(double theta)
( begin auto-generated from PVector_rotate.xml )
Rotate the vector by an angle (only 2D vectors), magnitude remains the
same
( end auto-generated )
|
PVector |
set(double[] source)
Set the x, y (and maybe z) coordinates using a double[] array as the
source.
|
PVector |
set(double x,
double y) |
PVector |
set(double x,
double y,
double z)
( begin auto-generated from PVector_set.xml )
Sets the x, y, and z component of the vector using two or three separate
variables, the data from a PVector, or the values from a double array.
|
PVector |
set(PVector v) |
PVector |
setMag(double len)
( begin auto-generated from PVector_setMag.xml )
Set the magnitude of this vector to the value used for the len
parameter.
|
PVector |
setMag(PVector target,
double len)
Sets the magnitude of this vector, storing the result in another vector.
|
OptionalInt |
size() |
PVector |
sub(double x,
double y) |
PVector |
sub(double x,
double y,
double z) |
PVector |
sub(PVector v)
( begin auto-generated from PVector_sub.xml )
Subtracts x, y, and z components from a vector, subtracts one vector from
another, or subtracts two independent vectors.
|
static PVector |
sub(PVector v1,
PVector v2)
Subtract one vector from another
|
static PVector |
sub(PVector v1,
PVector v2,
PVector target)
Subtract one vector from another and store in another vector
|
String |
toString() |
void |
writeTo(DataOutput out) |
public double x
public double y
public double z
public PVector()
public PVector(double x, double y, double z)
x
- the x coordinate.y
- the y coordinate.z
- the z coordinate.public PVector(double x, double y)
public PVector set(double x, double y, double z)
x
- the x component of the vectory
- the y component of the vectorz
- the z component of the vectorpublic PVector set(double x, double y)
x
- the x component of the vectory
- the y component of the vectorpublic PVector set(double[] source)
source
- array to copy frompublic static PVector random2D()
random3D()
public static PVector random2D(PVector target)
target
- the target vector (if null, a new vector will be created)public static PVector random3D()
random2D()
public static PVector random3D(PVector target)
target
- the target vector (if null, a new vector will be created)public static PVector fromAngle(double angle)
angle
- the angle in radianspublic static PVector fromAngle(double angle, PVector target)
target
- the target vector (if null, a new vector will be created)public PVector copy()
@Deprecated public PVector get()
public double[] get(double[] target)
target
- public double mag()
magSq()
public double magSq()
mag()
public PVector add(PVector v)
v
- the vector to be addedpublic PVector add(double x, double y)
x
- x component of the vectory
- y component of the vectorpublic PVector add(double x, double y, double z)
z
- z component of the vectorpublic static PVector add(PVector v1, PVector v2)
v1
- a vectorv2
- another vectorpublic static PVector add(PVector v1, PVector v2, PVector target)
target
- the target vector (if null, a new vector will be created)public PVector sub(PVector v)
v
- any variable of type PVectorpublic PVector sub(double x, double y)
x
- the x component of the vectory
- the y component of the vectorpublic PVector sub(double x, double y, double z)
z
- the z component of the vectorpublic static PVector sub(PVector v1, PVector v2)
v1
- the x, y, and z components of a PVector objectv2
- the x, y, and z components of a PVector objectpublic static PVector sub(PVector v1, PVector v2, PVector target)
target
- PVector in which to store the resultpublic PVector mult(double n)
n
- the number to multiply with the vectorpublic static PVector mult(PVector v, double n)
v
- the vector to multiply by the scalarpublic static PVector mult(PVector v, double n, PVector target)
target
- PVector in which to store the resultpublic PVector div(double n)
n
- the number by which to divide the vectorpublic static PVector div(PVector v, double n)
v
- the vector to divide by the scalarpublic static PVector div(PVector v, double n, PVector target)
target
- PVector in which to store the resultpublic double dist(PVector v)
v
- the x, y, and z coordinates of a PVectorpublic static double dist(PVector v1, PVector v2)
v1
- any variable of type PVectorv2
- any variable of type PVectorpublic double dot(PVector v)
v
- any variable of type PVectorpublic double dot(double x, double y, double z)
x
- x component of the vectory
- y component of the vectorz
- z component of the vectorpublic static double dot(PVector v1, PVector v2)
v1
- any variable of type PVectorv2
- any variable of type PVectorpublic PVector cross(PVector v)
v
- the vector to calculate the cross productpublic PVector cross(PVector v, PVector target)
v
- any variable of type PVectortarget
- PVector to store the resultpublic static PVector cross(PVector v1, PVector v2, PVector target)
v1
- any variable of type PVectorv2
- any variable of type PVectortarget
- PVector to store the resultpublic PVector normalize()
public PVector normalize(PVector target)
target
- Set to null to create a new vectorpublic PVector limit(double max)
max
- the maximum magnitude for the vectorpublic PVector setMag(double len)
len
- the new length for this vectorpublic PVector setMag(PVector target, double len)
target
- Set to null to create a new vectorlen
- the new length for the new vectorpublic double heading()
@Deprecated public double heading2D()
public PVector rotate(double theta)
theta
- the angle of rotationpublic PVector lerp(PVector v, double amt)
v
- the vector to lerp toamt
- The amount of interpolation; some value between 0.0 (old
vector) and 1.0 (new vector). 0.1 is very near the old vector; 0.5 is
halfway in between.PApplet#lerp(double, double, double)
public static PVector lerp(PVector v1, PVector v2, double amt)
v1
- the vector to start fromv2
- the vector to lerp topublic PVector lerp(double x, double y, double z, double amt)
x
- the x component to lerp toy
- the y component to lerp toz
- the z component to lerp topublic static double angleBetween(PVector v1, PVector v2)
v1
- the x, y, and z components of a PVectorv2
- the x, y, and z components of a PVectorpublic double[] array()
public void writeTo(DataOutput out) throws Exception
writeTo
in interface org.praxislive.core.DataObject
Exception
public void readFrom(DataInput in) throws Exception
readFrom
in interface org.praxislive.core.DataObject
Exception
public OptionalInt size()
size
in interface org.praxislive.core.DataObject