public class Vec extends Object implements Linkable
The result of all functions are applied to the vector itself, with the exception of cross(), which returns a new Vec (or writes to a specified 'target' Vec). 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 Vec. Functions that act on multiple vectors also include static versions. Because creating new objects can be computationally expensive, most functions include an optional 'target' Vec, so that a new Vec object is not created with each operation.
Initially based on the Processing PVector class.
Modifier and Type | Field and Description |
---|---|
float[] |
vec
The x, y and z coordinates of the Vec.
|
Constructor and Description |
---|
Vec()
Constructor for an empty vector: x, y, and z are set to 0.
|
Vec(float x,
float y)
Constructor for a 2D vector: z coordinate is set to 0.
|
Vec(float x,
float y,
float z)
Constructor for a 3D vector.
|
Modifier and Type | Method and Description |
---|---|
void |
add(float x,
float y,
float z) |
void |
add(Vec v)
Add a vector to this vector
|
static Vec |
add(Vec v1,
Vec v2)
Add two vectors
|
static Vec |
add(Vec v1,
Vec v2,
Vec target)
Add two vectors into a target vector
|
static float |
angleBetween(Vec v1,
Vec v2)
Calculate the angle between two vectors, using the dot product.
|
Vec |
cross(Vec v)
Return a vector composed of the cross product between this and another.
|
Vec |
cross(Vec v,
Vec target)
Perform cross product between this and another vector, and store the result in
'target'.
|
static Vec |
cross(Vec v1,
Vec v2,
Vec target) |
float |
distance(Vec v)
Calculate the Euclidean distance between two points (considering a point as a vector
object)
|
static float |
distance(Vec v1,
Vec v2)
Calculate the Euclidean distance between two points (considering a point as a vector
object)
|
void |
divide(float n)
Divide this vector by a scalar
|
static Vec |
divide(Vec v,
float n)
Divide a vector by a scalar and return the result in a new vector.
|
static Vec |
divide(Vec v,
float n,
Vec target)
Divide a vector by a scalar and store the result in another vector.
|
float |
dot(float x,
float y,
float z) |
float |
dot(Vec v)
Calculate the dot product with another vector
|
static float |
dot(Vec v1,
Vec v2) |
boolean |
equals(Object obj) |
static Vec |
fromAngle(float angle)
Make a new 2D unit vector from an angle.
|
static Vec |
fromAngle(float angle,
Vec target)
Make a new 2D unit vector from an angle
|
Vec |
get()
Get a copy of this vector.
|
float[] |
get(float[] target)
Gets data as an array
|
int |
hashCode() |
float |
heading()
Calculate the angle of rotation for this vector (only 2D vectors)
|
void |
lerp(float x,
float y,
float z,
float amt)
Linear interpolate the vector to x,y,z values.
|
void |
lerp(Vec v,
float amt)
Linear interpolate the vector to another vector
|
static Vec |
lerp(Vec v1,
Vec v2,
float amt)
Linear interpolate between two vectors (returns a new Vec object).
|
void |
limit(float max)
Limit the magnitude of this vector
|
void |
link(float[] src)
Share the src array data.
|
float |
magnitude()
Calculate the magnitude (length) of the vector
|
void |
multiply(float n)
Multiply this vector by a scalar
|
static Vec |
multiply(Vec v,
float n)
Multiply a vector by a scalar
|
static Vec |
multiply(Vec v,
float n,
Vec target)
Multiply a vector by a scalar, and write the result into a target Vec.
|
void |
normalize()
Normalize the vector to length 1 (make it a unit vector)
|
Vec |
normalize(Vec target)
Normalize this vector, storing the result in another vector.
|
Vec |
orthogonalVector()
Same as
return orthogonalVector(this) . |
static Vec |
orthogonalVector(Vec v)
Utility function that returns a Vec orthogonal to
v . |
void |
print() |
Vec |
projectVectorOnAxis(Vec direction)
Same as
return projectVectorOnAxis(this, direction) . |
static Vec |
projectVectorOnAxis(Vec src,
Vec direction)
Projects the
src Vec on the axis defined by direction (which does not
need to be normalized, but must be non null) that passes through the origin. |
Vec |
projectVectorOnPlane(Vec normal)
Same as
projectVectorOnPlane(this, normal) . |
static Vec |
projectVectorOnPlane(Vec src,
Vec normal)
Projects
src on the plane defined by normal (which does not need to
be normalized, but must be non null) that passes through the origin. |
void |
reset()
Sets all Vec components to 0.
|
void |
rotate(float theta)
Rotate the vector by an angle (only 2D vectors), magnitude remains the same
|
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(Linkable v)
Set x, y, and z coordinates from a Vec object.
|
void |
set(Vec v) |
void |
setMagnitude(float len)
Sets the magnitude of the vector to an arbitrary amount.
|
Vec |
setMagnitude(Vec target,
float len)
Sets the magnitude of this vector, storing the result in another vector.
|
void |
setX(float x)
Sets the x component of the Vec.
|
void |
setY(float y)
Sets the y component of the Vec.
|
void |
setZ(float z)
Sets the z component of the Vec.
|
float |
squaredMagnitude()
Calculate the squared magnitude of the vector Faster if the real length is not
required in the case of comparing vectors, etc.
|
float |
squaredNorm()
Same as
return squaredNorm(this) . |
static float |
squaredNorm(Vec v)
Utility function that returns the squared norm of the Vec.
|
void |
subtract(float x,
float y,
float z) |
void |
subtract(Vec v)
Subtract a vector from this vector
|
static Vec |
subtract(Vec v1,
Vec v2)
Subtract one vector from another
|
static Vec |
subtract(Vec v1,
Vec v2,
Vec target)
Subtract one vector from another and store in another vector
|
String |
toString() |
void |
unLink()
Stop sharing data with the source
|
float |
x()
Returns the x component of the Vec.
|
float |
y()
Returns the y component of the Vec.
|
float |
z()
Returns the z component of the Vec.
|
public Vec()
public Vec(float x, float y, float z)
x
- the x coordinate.y
- the y coordinate.z
- the y coordinate.public Vec(float x, float y)
x
- the x coordinate.y
- the y coordinate.public float x()
public float y()
public float z()
public void setX(float x)
public void setY(float y)
public void setZ(float z)
public Vec projectVectorOnAxis(Vec direction)
return projectVectorOnAxis(this, direction)
.projectVectorOnAxis(Vec, Vec)
public static Vec projectVectorOnAxis(Vec src, Vec direction)
src
Vec on the axis defined by direction
(which does not
need to be normalized, but must be non null) that passes through the origin.public Vec projectVectorOnPlane(Vec normal)
projectVectorOnPlane(this, normal)
.projectVectorOnPlane(Vec, Vec)
public static Vec projectVectorOnPlane(Vec src, Vec normal)
src
on the plane defined by normal
(which does not need to
be normalized, but must be non null) that passes through the origin.public float squaredNorm()
return squaredNorm(this)
.squaredNorm(Vec)
public static float squaredNorm(Vec v)
public Vec orthogonalVector()
return orthogonalVector(this)
.orthogonalVector(Vec)
public static Vec orthogonalVector(Vec v)
v
. Its mag()
depends on the Vec, but is zero only for a null
Vec. Note that the function
that associates an orthogonalVector()
to a Vec is not continuous.public void link(float[] src)
Linkable
public void unLink()
Linkable
public void set(float x, float y, float z)
x
- the x coordinate.y
- the y coordinate.z
- the z coordinate.public void set(Linkable v)
public void set(Vec v)
public void set(float[] source)
public Vec get()
public float[] get(float[] target)
Linkable
public float magnitude()
public float squaredMagnitude()
public void add(Vec v)
v
- the vector to be addedpublic void add(float x, float y, float z)
x
- x component of the vectory
- y component of the vectorz
- z component of the vectorpublic static Vec add(Vec v1, Vec v2)
v1
- a vectorv2
- another vectorpublic static Vec add(Vec v1, Vec v2, Vec target)
v1
- a vectorv2
- another vectortarget
- the target vector (if null, a new vector will be created)public void subtract(Vec v)
v
- the vector to be subtractedpublic void subtract(float x, float y, float z)
x
- the x component of the vectory
- the y component of the vectorz
- the z component of the vectorpublic static Vec subtract(Vec v1, Vec v2)
v1
- a vectorv2
- another vectorpublic static Vec subtract(Vec v1, Vec v2, Vec target)
v1
- the x, y, and z components of a Vec objectv2
- the x, y, and z components of a Vec objecttarget
- Vec in which to store the resultpublic void multiply(float n)
n
- the value to multiply bypublic static Vec multiply(Vec v, float n)
v
- a vectorn
- scalarpublic static Vec multiply(Vec v, float n, Vec target)
v
- a vectorn
- scalartarget
- Vec to store the resultpublic void divide(float n)
n
- the value to divide bypublic static Vec divide(Vec v, float n)
v
- a vectorn
- scalarpublic static Vec divide(Vec v, float n, Vec target)
target
- Vec in which to store the resultpublic float distance(Vec v)
v
- another vectorpublic static float distance(Vec v1, Vec v2)
v1
- a vectorv2
- another vectorpublic float dot(Vec v)
public float dot(float x, float y, float z)
x
- x component of the vectory
- y component of the vectorz
- z component of the vectorpublic static float dot(Vec v1, Vec v2)
v1
- any variable of type Vecv2
- any variable of type Vecpublic Vec cross(Vec v)
public Vec cross(Vec v, Vec target)
public static Vec cross(Vec v1, Vec v2, Vec target)
v1
- any variable of type Vecv2
- any variable of type Vectarget
- Vec to store the resultpublic void normalize()
public Vec normalize(Vec target)
target
- Set to null to create a new vectorpublic void limit(float max)
max
- the maximum length to limit this vectorpublic void setMagnitude(float len)
len
- the new length for this vectorpublic Vec setMagnitude(Vec target, float len)
target
- Set to null to create a new vectorlen
- the new length for the new vectorpublic float heading()
public void rotate(float theta)
theta
- the angle of rotationpublic void lerp(Vec v, float amt)
v
- the vector to lerp toamt
- The amt parameter is the amount to interpolate between the two vectors where
1.0 equal to the new vector 0.1 is very near the new vector, 0.5 is half-way
in between.public static Vec lerp(Vec v1, Vec v2, float amt)
v1
- the vector to start fromv2
- the vector to lerp topublic void lerp(float x, float y, float z, float amt)
x
- the x component to lerp toy
- the y component to lerp toz
- the z component to lerp topublic static float angleBetween(Vec v1, Vec v2)
v1
- a vectorv2
- another vectorpublic static Vec fromAngle(float angle)
angle
- the anglepublic static Vec fromAngle(float angle, Vec target)
angle
- the angletarget
- the target vector (if null, a new vector will be created)public void print()
Processing Library proscene by Jean Pierre Charalambos. (c) 2014-2017 National University of Colombia