public class Vector3D extends java.lang.Object implements Locatable
add(float, float, float)
,
v1 != v2. The one exception is the cross(Vector3D)
method, which always
returns a new Vector3D. Also, none of the operations alter their parameters, except in
self-referential conditions (e.g., Vector3D.add(v1,v2,v1)
modifies v1).
The methods in the class are designed to allow "chaining"; given a Vector3D v1, it could be used as:
v1.add(1,2,3);
v1.add(3,2,1);
v1.multiplyBy(2);
v1.add(1,2,3).add(3,2,1).multiplyBy(2);
v1.add(1,2,3).add(3,2,1).multiplyBy(2) == v1.add(3,2,1).add(1,2,3).multiplyBy(2)
v1.add(1,2,3).add(3,2,1).multiplyBy(2) != v1.multiplyBy(2).add(1,2,3).add(3,2,1)
Constructor and Description |
---|
Vector3D()
Creates a (0,0,0) Vector3D.
|
Vector3D(float x,
float y,
float z)
Creates a 3D vector with the given x, y, z components.
|
Vector3D(processing.core.PVector location)
Creates a 3D vector from the location defined in the given pVector.
|
Vector3D(Vector3D p)
Creates a new cloned 3D vector copied from the components of the given vector.
|
Modifier and Type | Method and Description |
---|---|
Vector3D |
add(float x,
float y,
float z)
Adds the arguments to this vector's components.
|
Vector3D |
add(Vector3D p)
Adds the argument Vector3D's components to this Vector3D's components.
|
static Vector3D |
add(Vector3D v1,
Vector3D v2)
Creates a new Vector3D from v1+v2.
|
static Vector3D |
add(Vector3D v1,
Vector3D v2,
Vector3D target)
Returns v1+v2 in target, or a new Vector3D if target is null
|
void |
clear()
Resets this vector back to (0,0,0).
|
Vector3D |
copy()
Provides a copy of this Vector3D.
|
static Vector3D |
copy(Vector3D from)
Copies the provided Vector3D.
|
Vector3D |
cross(Vector3D v)
Creates a new Vector3D from this and the cross product with the given vector.
|
float |
distanceSquaredTo(Vector3D v)
Like
distanceTo(Vector3D) , only squared |
float |
distanceTo(float x,
float y,
float z)
Calculates the distance between this vector and that represented by the three given components.
|
float |
distanceTo(Vector3D v)
Calculates the distance between the tip of this Vector3D and that of p.
|
float |
dot(Vector3D p)
Calculates the dot product between this Vector3D and another - this.x*p.x + this.y*p.y + this.y*p.y
|
boolean |
equals(java.lang.Object other) |
protected boolean |
equals(Vector3D other)
Determines if the components of this vector match those of the given one.
|
Vector3D |
floor(float f)
The opposite of
limit(float) : puts a lower bound on the length of f . |
processing.core.PVector |
getLocation()
Reports the location represented by this vector.
|
float |
getX()
Reports the x component of this vector.
|
float |
getY()
Reports the y component of this vector.
|
float |
getZ()
Reports the the z component of this vector.
|
int |
hashCode()
Returns a unique hash code that represents the content of this vector.
|
boolean |
isZero()
Indicates whether or not this Vector3D is the zero vector
|
float |
length()
Reports the length of this vector.
|
Vector3D |
length(float f)
Reports the normalised length of this vector scaled by the given scale factor.
|
float |
lengthSquared()
Reports the squared length of this vector.
|
Vector3D |
limit(float f)
Limits the length of this Vector3D to
f . |
Vector3D |
multiplyBy(float f)
Multiplies each component by
f . |
static Vector3D |
multiplyBy(Vector3D v,
float f)
Creates a new Vector3D from v, by copying it and multiplying each of its components by f.
|
static Vector3D |
multiplyBy(Vector3D v,
float f,
Vector3D target)
Returns the result of v*f in target, or creates a new Vector3D if target==null.
|
Vector3D |
normalize()
Sets this Vector3Ds length to one by appropriately scaling x, y, and z.
|
static Vector3D |
of()
Creates a 'zero' vector.
|
static Vector3D |
of(float x,
float y,
float z)
Static constructor; convenience method for chaining calls.
|
Vector3D |
projectOnto(Vector3D p)
Projects this Vector3D onto another Vector3D.
|
Vector3D |
set(float x,
float y,
float z)
Sets all the components.
|
Vector3D |
set(Vector3D p)
Sets this Vector3D components to those of another Vector3D.
|
Vector3D |
setX(float x)
Sets the x component and return this Vector3D after modification.
|
Vector3D |
setY(float y)
Sets the y component and return this Vector3D after modification.
|
Vector3D |
setZ(float z)
Sets the z component and return this Vector3D after modification.
|
Vector3D |
subtract(float x,
float y,
float z)
Subtracts the arguments from this vector's components.
|
Vector3D |
subtract(Vector3D p)
Subtracts the argument from this vector.
|
static Vector3D |
subtract(Vector3D v1,
Vector3D v2)
Returns a new Vector3D, v1 - v2.
|
static Vector3D |
subtract(Vector3D v1,
Vector3D v2,
Vector3D target)
Returns v1-v2 in target, or a new Vector3D if target is null.
|
static Vector3D |
thrower(java.lang.String message)
Throws a NullPointerException with the provided message.
|
java.lang.String |
toString()
Provides a textual representation of this vector.
|
float |
x()
Reports the x component of this vector, but consider using getX() instead to follow standard accessor notation.
|
float |
y()
Reports the y component of this vector, but consider using getY() instead to follow standard accessor notation.
|
float |
z()
Reports the the z component of this vector, but consider using getZ() instead to follow standard accessor notation.
|
public Vector3D()
public Vector3D(processing.core.PVector location)
location
- Location information used to create this 3d vector.public Vector3D(float x, float y, float z)
x
- the x component of the vector.y
- the y component of the vector.z
- the z component of the vector.public Vector3D(Vector3D p) throws java.lang.NullPointerException
p
- the source Vector3D.java.lang.NullPointerException
- if p is nullpublic static final Vector3D of(float x, float y, float z)
Vector3D v2 = (new Vector3D(x,y,z)).add(v1)...
with Vector3D v2 = Vector3D.of(x,y,z).add(v1)...
x
- the x component of the vector.y
- the y component of the vector.z
- the z component of the vector.public static final Vector3D of()
public static final Vector3D copy(Vector3D from)
from
- the source Vector3D.java.lang.NullPointerException
- if the from vector is null.public final Vector3D copy()
public static final Vector3D thrower(java.lang.String message) throws java.lang.NullPointerException
message
- the messagejava.lang.NullPointerException
- Null pointer exception.public final Vector3D subtract(float x, float y, float z)
x
- the x component of the vector.y
- the y component of the vector.z
- the z component of the vector.public final Vector3D subtract(Vector3D p) throws java.lang.NullPointerException
p
- the vector to subtract from this one; p is unmodified by this calljava.lang.NullPointerException
- if p is nullpublic static final Vector3D subtract(Vector3D v1, Vector3D v2) throws java.lang.NullPointerException
v1
- the first vector; not modified by this operation.v2
- the second vector; not modified by this operation.java.lang.NullPointerException
- if either of v1 or v2 is nullpublic static final Vector3D subtract(Vector3D v1, Vector3D v2, Vector3D target) throws java.lang.NullPointerException
v1
- the first vector; unmodified by this operationv2
- the second vector; unmodified by this operationtarget
- the target vector, may be nulljava.lang.NullPointerException
- if either of v1 or v2 is nullpublic final Vector3D add(float x, float y, float z)
x
- the x component of the vector.y
- the y component of the vector.z
- the z component of the vector.public final Vector3D add(Vector3D p) throws java.lang.NullPointerException
p
- the Vector3D to be added to this one; unmodified by this operationjava.lang.NullPointerException
- if p==nullpublic static Vector3D add(Vector3D v1, Vector3D v2) throws java.lang.NullPointerException
v1
- one Vector3D, unmodified by this operationv2
- the other Vector3D, unmodified by this operationjava.lang.NullPointerException
- if either v1 or v2 is nullpublic static Vector3D add(Vector3D v1, Vector3D v2, Vector3D target) throws java.lang.NullPointerException
v1
- the first vector; unmodified by this operation (unless also the target)v2
- the second vector; unmodified by this operation (unless also the target)target
- the target vector, may be nulljava.lang.NullPointerException
- if either of v1 or v2 is nullpublic final float x()
public final float y()
public final float z()
public final float getX()
public final float getY()
public final float getZ()
public processing.core.PVector getLocation()
getLocation
in interface Locatable
public final Vector3D setX(float x)
x
- the new x component of this vector.public final Vector3D setY(float y)
y
- the new y component of this vector.public final Vector3D setZ(float z)
z
- the new z component of this vector.public final Vector3D set(float x, float y, float z)
x
- the desired x component of this vector.y
- the desired y component of this vector.z
- the desired z component of this vector.public final Vector3D set(Vector3D p) throws java.lang.NullPointerException
p
- the other Vector3D of this vector.java.lang.NullPointerException
- if p is nullpublic final Vector3D multiplyBy(float f)
f
.f
- the scaling factor.public static final Vector3D multiplyBy(Vector3D v, float f) throws java.lang.NullPointerException
v
- the original Vector3D; unmodified by this operation.f
- the scaling factor.java.lang.NullPointerException
- if v==null.public static final Vector3D multiplyBy(Vector3D v, float f, Vector3D target) throws java.lang.NullPointerException
v
- the source Vector3D; unmodified by this operation.f
- the scaling factor.target
- the target Vector3D; modified by this operation, may be null.java.lang.NullPointerException
- if v==null.public final Vector3D limit(float f)
f
. Calling this with f less than or equal to 0 sets length to 0.f
- the desired limit.public final Vector3D floor(float f)
limit(float)
: puts a lower bound on the length of f
. Calling this with
f less than or equal to 0 is a no-op.f
- the desired minimum lengthpublic final Vector3D normalize() throws java.lang.ArithmeticException
java.lang.ArithmeticException
- if this is a zero vector.public final float distanceTo(Vector3D v) throws java.lang.NullPointerException
subtract(Vector3D, Vector3D)
and length()
.v
- the other Vector3D; unmodified by this operationjava.lang.NullPointerException
- if p==nullpublic final float distanceSquaredTo(Vector3D v) throws java.lang.NullPointerException
distanceTo(Vector3D)
, only squaredv
- the other Vector3D; unmodified by this operationjava.lang.NullPointerException
- if p==nullpublic final float distanceTo(float x, float y, float z)
x
- The x component of the vector.y
- The y component of the vector.z
- The z component of the vector.public final Vector3D projectOnto(Vector3D p)
p
- the other Vector3Dpublic final float dot(Vector3D p) throws java.lang.NullPointerException
p
- the other Vector3Djava.lang.NullPointerException
- if p==nullpublic final float length()
public final Vector3D length(float f)
f
- Scale factor.public final float lengthSquared()
public final void clear()
public final java.lang.String toString()
toString
in class java.lang.Object
public final Vector3D cross(Vector3D v) throws java.lang.NullPointerException
v
- the other Vector3D in the cross-product.java.lang.NullPointerException
- if v is null.public boolean isZero()
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
protected boolean equals(Vector3D other)
other
- the other Vector3D.