Package nub.primitives
Class Vector
- java.lang.Object
-
- nub.primitives.Vector
-
public class Vector extends Object
A class to describe a two or three dimensional vector. This class API aims to conform that of the great Processing PVector.The result of all functions are applied to the vector itself, with the exception of cross(), which returns a new vector (or writes to a specified 'target' vector). 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 vector. Functions that act on multiple vectors also include static versions. Because creating new objects can be computationally expensive, most functions include an optional 'target' vector, so that a new vector object is not created with each operation.
Initially based on the Processing PVector class.
-
-
Field Summary
Fields Modifier and Type Field Description float[]_vectorThe x, y and z coordinates of the Vector.static VectorminusIOpposite of the first canonical vector (coordinates: -1, 0, 0).static VectorminusJOpposite of the second canonical vector (coordinates: 0, -1, 0).static VectorminusKOpposite of the third canonical vector (coordinates: 0, 0, -1).static VectorplusIFirst canonical vector (coordinates: 1, 0, 0).static VectorplusJSecond canonical vector (coordinates: 0, 1, 0).static VectorplusKThird canonical vector (coordinates: 0, 0, 1).static VectorzeroZero vector (coordinates: 0, 0, 0).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(float x, float y, float z)voidadd(Vector vector)Add a vector to this vectorstatic Vectoradd(Vector vector1, Vector vector2)Add two vectors.static Vectoradd(Vector vector1, Vector vector2, Vector target)Add two vectors into a target vector.static floatangleBetween(Vector vector1, Vector vector2)Calculate the angle between two vectors, using the dot product.Vectorcopy()Returns a deep copy of this vector.Vectorcross(Vector v)Return a vector composed of the cross product between this and another.Vectorcross(Vector vector, Vector target)Perform cross product between this and another vector, and store the result in 'target'.static Vectorcross(Vector vector1, Vector vector2, Vector target)Cross product: target = vector1 * vector2.floatdistance(Vector vector)Calculate the Euclidean distance between two points (considering a point as a vector object).static floatdistance(Vector vector1, Vector vector2)Calculate the Euclidean distance between two points (considering a point as a vector object).voiddivide(float n)Divide this vector by a scalar.static Vectordivide(Vector vector, float n)Divide a vector by a scalar and return the result in a new vector.static Vectordivide(Vector vector, float n, Vector target)Divide a vector by a scalar and store the result in another vector.floatdot(float x, float y, float z)floatdot(Vector vector)Calculate the dot product with another vector.static floatdot(Vector vector1, Vector vector2)static VectorfromAngle(float angle)Make a new 2D unit vector from an angle.static VectorfromAngle(float angle, Vector target)Make a new 2D unit vector from an angle.float[]get(float[] target)Returns this vector as an array.floatheading()Calculate the angle of rotation for this vector (only 2D vectors).static floatlerp(float start, float stop, float amount)Calculates a number between two numbers at a specific increment.voidlerp(float x, float y, float z, float amount)Linear interpolate the vector to x,y,z values.voidlerp(Vector vector, float amount)Linear interpolate the vector to another vector.static Vectorlerp(Vector vector1, Vector vector2, float amount)Linear interpolate between two vectors (returns a new Vector object).voidlimit(float maximum)Limit the magnitude of this vector.floatmagnitude()Calculate the magnitude (length) of the vectorbooleanmatches(Vector vector)Returns whether or not this vector matches other.voidmultiply(float n)Multiply this vector by a scalar.static Vectormultiply(Vector vector, float n)Multiply a vector by a scalar.static Vectormultiply(Vector vector, float n, Vector target)Multiply a vector by a scalar, and write the result into a target vector.voidnormalize()Normalize the vector to length 1 (make it a unit vector).Vectornormalize(Vector target)Normalize this vector, storing the result in another vector.VectororthogonalVector()Same asreturn orthogonalVector(this).static VectororthogonalVector(Vector vector)Utility function that returns a vector orthogonal tovector.VectorprojectVectorOnAxis(Vector direction)Same asreturn projectVectorOnAxis(this, direction).static VectorprojectVectorOnAxis(Vector vector, Vector direction)Projects thevectoron the axis defined bydirection(which does not need to be normalized, but must be non null) that passes through the origin.VectorprojectVectorOnPlane(Vector normal)Same asprojectVectorOnPlane(this, normal).static VectorprojectVectorOnPlane(Vector vector, Vector normal)Projectsvectoron the plane defined bynormal(which does not need to be normalized, but must be non null) that passes through the origin.static Vectorrandom()Returns a normalized random vector.voidrandomize()Randomize this vector.voidreset()Sets all vector components to 0.voidrotate(float theta)Rotate the vector by an angle (only 2D vectors), magnitude remains the same.static floatscalarProjection(Vector a, Vector b)Returns the scalar projection of a onto b.voidset(float[] source)Set the x, y (and maybe z) coordinates using a float[] array as the source.voidset(float x, float y)Set x and y coordinates.voidset(float x, float y, float z)Set x, y, and z coordinates.voidset(Vector vector)Set x, y, and z coordinates from a Vector object.voidsetMagnitude(float magnitude)Sets the magnitude of the vector to an arbitrary amount.VectorsetMagnitude(Vector target, float magnitude)Sets the magnitude of this vector, storing the result in another vector.voidsetX(float x)Sets the x component of the vector.voidsetY(float y)Sets the y component of the vector.voidsetZ(float z)Sets the z component of the vector.floatsquaredMagnitude()Calculate the squared magnitude of the vector.floatsquaredNorm()Same asreturn squaredNorm(this).static floatsquaredNorm(Vector vector)Utility function that returns the squared norm of the vector.voidsubtract(float x, float y, float z)voidsubtract(Vector vector)Subtract a vector from this vector.static Vectorsubtract(Vector vector1, Vector vector2)Subtract one vector from another.static Vectorsubtract(Vector vector1, Vector vector2, Vector target)Subtract one vector from another and store in another vector.StringtoString()Return this vector components as a String.static VectorvectorProjection(Vector a, Vector b)Returns the vector projection of a onto b.floatx()Returns the x component of the vector.floaty()Returns the y component of the vector.floatz()Returns the z component of the vector.
-
-
-
Field Detail
-
_vector
public float[] _vector
The x, y and z coordinates of the Vector.
-
plusI
public static final Vector plusI
First canonical vector (coordinates: 1, 0, 0).
-
minusI
public static final Vector minusI
Opposite of the first canonical vector (coordinates: -1, 0, 0).
-
plusJ
public static final Vector plusJ
Second canonical vector (coordinates: 0, 1, 0).
-
minusJ
public static final Vector minusJ
Opposite of the second canonical vector (coordinates: 0, -1, 0).
-
plusK
public static final Vector plusK
Third canonical vector (coordinates: 0, 0, 1).
-
minusK
public static final Vector minusK
Opposite of the third canonical vector (coordinates: 0, 0, -1).
-
zero
public static final Vector zero
Zero vector (coordinates: 0, 0, 0).
-
-
Constructor Detail
-
Vector
public Vector()
Constructor for an empty vector: x, y, and z are set to 0.
-
Vector
public Vector(float x, float y, float z)Constructor for a 3D vector.- Parameters:
x- the x coordinate.y- the y coordinate.z- the y coordinate.
-
Vector
public Vector(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
-
matches
public boolean matches(Vector vector)
Returns whether or not this vector matches other.- Parameters:
vector- other vector
-
randomize
public void randomize()
Randomize this vector. The vector is normalized too.- See Also:
random()
-
random
public static Vector random()
Returns a normalized random vector.- See Also:
randomize()
-
x
public float x()
Returns the x component of the vector.
-
y
public float y()
Returns the y component of the vector.
-
z
public float z()
Returns the z component of the vector.
-
setX
public void setX(float x)
Sets the x component of the vector.
-
setY
public void setY(float y)
Sets the y component of the vector.
-
setZ
public void setZ(float z)
Sets the z component of the vector.
-
projectVectorOnAxis
public Vector projectVectorOnAxis(Vector direction)
Same asreturn projectVectorOnAxis(this, direction).- See Also:
projectVectorOnAxis(Vector, Vector)
-
vectorProjection
public static Vector vectorProjection(Vector a, Vector b)
Returns the vector projection of a onto b. Vector b should be normalized. See: https://en.wikipedia.org/wiki/Vector_projection
-
scalarProjection
public static float scalarProjection(Vector a, Vector b)
Returns the scalar projection of a onto b. Vector b should be normalized. See: https://en.wikipedia.org/wiki/Scalar_projection
-
projectVectorOnAxis
public static Vector projectVectorOnAxis(Vector vector, Vector direction)
Projects thevectoron the axis defined bydirection(which does not need to be normalized, but must be non null) that passes through the origin.
-
projectVectorOnPlane
public Vector projectVectorOnPlane(Vector normal)
Same asprojectVectorOnPlane(this, normal).- See Also:
projectVectorOnPlane(Vector, Vector)
-
projectVectorOnPlane
public static Vector projectVectorOnPlane(Vector vector, Vector normal)
Projectsvectoron the plane defined bynormal(which does not need to be normalized, but must be non null) that passes through the origin.
-
squaredNorm
public float squaredNorm()
Same asreturn squaredNorm(this).- See Also:
squaredNorm(Vector)
-
squaredNorm
public static float squaredNorm(Vector vector)
Utility function that returns the squared norm of the vector.
-
orthogonalVector
public Vector orthogonalVector()
Same asreturn orthogonalVector(this).- See Also:
orthogonalVector(Vector)
-
orthogonalVector
public static Vector orthogonalVector(Vector vector)
Utility function that returns a vector orthogonal tovector. Itsmagnitude()depends on the vector, but is zero only for anullvector. Note that the function that associates anorthogonalVector()to a vector is not continuous.
-
reset
public void reset()
Sets all vector components to 0.
-
set
public void set(float x, float y)Set x and y coordinates. The z coordinate is set to 0.- Parameters:
x- the x coordinate.y- the y coordinate.- See Also:
set(float, float, float)
-
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.- See Also:
set(float, float)
-
set
public void set(Vector vector)
Set x, y, and z coordinates from a Vector object.- Parameters:
vector- the Vector 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
-
copy
public Vector copy()
Returns a deep copy of this vector.
-
get
public float[] get(float[] target)
Returns this vector as an array.
-
magnitude
public float magnitude()
Calculate the magnitude (length) of the vector- Returns:
- the magnitude of the vector
-
squaredMagnitude
public float squaredMagnitude()
Calculate the squared magnitude of the vector.- Returns:
- squared magnitude of the vector
-
add
public void add(Vector vector)
Add a vector to this vector- Parameters:
vector- the vector to be added
-
add
public void add(float x, float y, float z)- Parameters:
x- x component of the vectory- y component of the vectorz- z component of the vector
-
add
public static Vector add(Vector vector1, Vector vector2)
Add two vectors.- Parameters:
vector1- a vectorvector2- another vector- Returns:
- a new vector that is the sum of vector1 and vector2
-
add
public static Vector add(Vector vector1, Vector vector2, Vector target)
Add two vectors into a target vector.- Parameters:
vector1- a vectorvector2- another vectortarget- the target vector (if null, a new vector will be created)- Returns:
- a new vector that is the sum of vector1 and vector2
-
subtract
public void subtract(Vector vector)
Subtract a vector from this vector.- Parameters:
vector- the vector to be subtracted
-
subtract
public void subtract(float x, float y, float z)- Parameters:
x- the x component of the vectory- the y component of the vectorz- the z component of the vector
-
subtract
public static Vector subtract(Vector vector1, Vector vector2)
Subtract one vector from another.- Parameters:
vector1- a vectorvector2- another vector- Returns:
- a new vector that is vector1 - vector2
-
subtract
public static Vector subtract(Vector vector1, Vector vector2, Vector target)
Subtract one vector from another and store in another vector.- Parameters:
vector1- the x, y, and z components of a Vector objectvector2- the x, y, and z components of a Vector objecttarget- Vector in which to store the result
-
multiply
public void multiply(float n)
Multiply this vector by a scalar.- Parameters:
n- the value to multiply by
-
multiply
public static Vector multiply(Vector vector, float n)
Multiply a vector by a scalar.- Parameters:
vector- a vectorn- scalar- Returns:
- a new vector that is vector * n
-
multiply
public static Vector multiply(Vector vector, float n, Vector target)
Multiply a vector by a scalar, and write the result into a target vector.- Parameters:
vector- a vectorn- scalartarget- Vector to store the result- Returns:
- the target vector, now set to vector * n
-
divide
public void divide(float n)
Divide this vector by a scalar.- Parameters:
n- the value to divide by
-
divide
public static Vector divide(Vector vector, float n)
Divide a vector by a scalar and return the result in a new vector.- Parameters:
vector- a vectorn- scalar- Returns:
- a new vector that is vector / n
-
divide
public static Vector divide(Vector vector, float n, Vector target)
Divide a vector by a scalar and store the result in another vector.- Parameters:
target- Vector in which to store the result
-
distance
public float distance(Vector vector)
Calculate the Euclidean distance between two points (considering a point as a vector object).- Parameters:
vector- another vector- Returns:
- the Euclidean distance between
-
distance
public static float distance(Vector vector1, Vector vector2)
Calculate the Euclidean distance between two points (considering a point as a vector object).- Parameters:
vector1- a vectorvector2- another vector- Returns:
- the Euclidean distance between vector1 and vector2
-
dot
public float dot(Vector vector)
Calculate the dot product with another vector.- Returns:
- the dot product
-
dot
public float dot(float x, float y, float z)- Parameters:
x- x component of the vectory- y component of the vectorz- z component of the vector
-
dot
public static float dot(Vector vector1, Vector vector2)
- Parameters:
vector1- any variable of type Vectorvector2- any variable of type Vector
-
cross
public Vector cross(Vector v)
Return a vector composed of the cross product between this and another.
-
cross
public Vector cross(Vector vector, Vector 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 Vector cross(Vector vector1, Vector vector2, Vector target)
Cross product: target = vector1 * vector2.- Parameters:
vector1- any variable of type Vectorvector2- any variable of type Vectortarget- Vector to store the result
-
normalize
public void normalize()
Normalize the vector to length 1 (make it a unit vector).
-
normalize
public Vector normalize(Vector 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 maximum)
Limit the magnitude of this vector.- Parameters:
maximum- the maximum length to limit this vector
-
setMagnitude
public void setMagnitude(float magnitude)
Sets the magnitude of the vector to an arbitrary amount.- Parameters:
magnitude- the new length for this vector
-
setMagnitude
public Vector setMagnitude(Vector target, float magnitude)
Sets the magnitude of this vector, storing the result in another vector.- Parameters:
target- Set to null to create a new vectormagnitude- the new length for the new vector- Returns:
- a new vector (if target was null), or target
-
heading
public float heading()
Calculate the angle of rotation for this vector (only 2D vectors).- Returns:
- the angle of rotation
-
rotate
public void rotate(float theta)
Rotate the vector by an angle (only 2D vectors), magnitude remains the same.- Parameters:
theta- the angle of rotation
-
lerp
public static float lerp(float start, float stop, float amount)Calculates a number between two numbers at a specific increment. Theamountparameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc.
-
lerp
public void lerp(Vector vector, float amount)
Linear interpolate the vector to another vector.- Parameters:
vector- the vector to lerp toamount- 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.
-
lerp
public static Vector lerp(Vector vector1, Vector vector2, float amount)
Linear interpolate between two vectors (returns a new Vector object).- Parameters:
vector1- the vector to start fromvector2- the vector to lerp to
-
lerp
public void lerp(float x, float y, float z, float amount)Linear interpolate the vector to x,y,z values.- Parameters:
x- the x component to lerp toy- the y component to lerp toz- the z component to lerp to
-
angleBetween
public static float angleBetween(Vector vector1, Vector vector2)
Calculate the angle between two vectors, using the dot product.- Parameters:
vector1- a vectorvector2- another vector- Returns:
- the angle between the vectors
-
fromAngle
public static Vector fromAngle(float angle)
Make a new 2D unit vector from an angle.- Parameters:
angle- the angle- Returns:
- the new unit PVec
-
fromAngle
public static Vector fromAngle(float angle, Vector target)
Make a new 2D unit vector from an angle.- Parameters:
angle- the angletarget- the target vector (if null, a new vector will be created)- Returns:
- the Vector
-
-