Package raymarcher
Class Vector3
java.lang.Object
raymarcher.Vector3
public class Vector3
extends java.lang.Object
Represents a vector in 3D Euclidean space (x, y, z). Can be used to represent a
point in space or to represent a direction.
A left-hand coordinate system is used, with +x being rightwards, +y being upwards,
and +z being backwards.
-
Field Summary
-
Constructor Summary
Constructors Constructor Description Vector3()
Creates a default zero Vector3 at (0, 0, 0)Vector3(double x, double y, double z)
Creates a Vector3 with the passed coordinatesVector3(Vector3 unitVector, double length)
Creates a Vector3 in the direction of the given unit vector and with the given lentgh (magnitude) -
Method Summary
Modifier and Type Method Description Vector3
abs()
Returns the absolute value of the current vectorVector3
add(Vector3 v2)
Gives the resultant of adding two vectors togetherVector3
addInPlace(Vector3 v2)
Translates the current vector by adding a given vector to it.double
angleBetween(Vector3 v2)
Gives the angle between two vectors, in radians.double
angleToXY()
Gives the angle between the vector and the XY plane, in radiansdouble
angleToXZ()
Gives the angle between the vector and the XZ plane, in radiansdouble
angleToYZ()
Gives the angle between the vector and the YZ plane, in radiansVector3
clone()
Clones the current vectorVector3
crossProduct(Vector3 v2)
Gives the vector that results from the cross product of two vectors.Vector3
differenceVector(Vector3 v2)
Gives the vector difference from the first vector to the second vector.double
distance(Vector3 v2)
Gives the distance between two points represented by vectorsdouble
dotProduct(Vector3 v2)
Gives the dot product between two vectorsboolean
equals(double x, double y, double z)
Checks if the current vectors is equal to the vector with the given coordinatesboolean
equals(Vector3 v2)
Checks if two vectors are equal to each otherdouble
findFactor(Vector3 v2)
Finds the factor by which a scalar multiplication must be applied to the first vector in order to get the passed vectorVector3
getUnitVector()
Gives the unit vector for the current vector.double
getX()
Gives the current x-coordinate of the vectordouble
getY()
Gives the current y-coordinate of the vectordouble
getZ()
Gives the current z-coordinate of the vectordouble
length()
Gives the length of the vector.Vector3
multiply(double factor)
Performs a scalar multiplication on the vector.Vector3
multiplyByVector(Vector3 factor)
Multiplies each of the vector's components using scalar multiplication by their respective component in the factor vector.Vector3
projection(Vector3 v2)
Gives the vector result of a projection of the first vector onto the second vectorvoid
replace(double x, double y, double z)
Replaces the current vector with the values of a new Vector (Changes the current vector)void
replace(Vector3 newVector)
Replaces the current vector with the values of a new Vector (Changes the current vector)Vector3
rotateInPlaceX(double angle)
Performs a Euler rotation on the current vector about the x-axis (Changes the current vector)Vector3
rotateInPlaceY(double angle)
Performs a Euler rotation on the current vector about the y-axis (Changes the current vector)Vector3
rotateInPlaceZ(double angle)
Performs a Euler rotation on the current vector about the z-axis (Changes the current vector)Vector3
rotateX(double angle)
Give the result vector of a Euler rotation about the x-axisVector3
rotateY(double angle)
Give the result vector of a Euler rotation about the y-axisVector3
rotateZ(double angle)
Give the result vector of a Euler rotation about the z-axisdouble
scalarProjection(Vector3 v2)
Gives the scalar projection of the first vector onto the second vector.Vector3
scale(double factor)
Scales the current vector by performing a scalar multiplication on it.Vector3
scaleByVector(Vector3 factor)
Scales each of the current vector's components using scalar multiplication by their respective component in the factor vector.void
setX(double x)
Gives the current x-coordinate of the vectorvoid
setY(double y)
Gives the current y-coordinate of the vectorvoid
setZ(double z)
Gives the current z-coordinate of the vectorVector3
subtract(Vector3 v2)
Gives the resultant vector of subtracting two vectorsVector3
subtractInPlace(Vector3 v2)
Translates the current vector by subtracting a given vector from it.java.lang.String
toString()
Vector3
translate(double x, double y, double z)
Translates the current vector by the given values (Changes the current vector)Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
x
private double xThe vector's x-coordinate -
y
private double yThe vector's y-coordinate -
z
private double zThe vector's z-coordinate
-
-
Constructor Details
-
Vector3
public Vector3()Creates a default zero Vector3 at (0, 0, 0) -
Vector3
Creates a Vector3 in the direction of the given unit vector and with the given lentgh (magnitude)- Parameters:
unitVector
- The unit vector for the Vector3length
- The length (magnitude) of the Vector3 to be created
-
Vector3
public Vector3(double x, double y, double z)Creates a Vector3 with the passed coordinates- Parameters:
x
- The x-axis coordinatey
- The y-axis coordinatez
- The z-axis coordinate
-
-
Method Details
-
getX
public double getX()Gives the current x-coordinate of the vector- Returns:
- The current x-coordinate
-
getY
public double getY()Gives the current y-coordinate of the vector- Returns:
- The current y-coordinate
-
getZ
public double getZ()Gives the current z-coordinate of the vector- Returns:
- The current z-coordinate
-
setX
public void setX(double x)Gives the current x-coordinate of the vector- Parameters:
x
- The new x-coordinate
-
setY
public void setY(double y)Gives the current y-coordinate of the vector- Parameters:
y
- The new y-coordinate
-
setZ
public void setZ(double z)Gives the current z-coordinate of the vector- Parameters:
z
- The new z-coordinate
-
replace
Replaces the current vector with the values of a new Vector (Changes the current vector)- Parameters:
newVector
- The new Vector for which to match the current vector to
-
replace
public void replace(double x, double y, double z)Replaces the current vector with the values of a new Vector (Changes the current vector)- Parameters:
x
- The new x-coordinate of the vectory
- The new y-coordinate of the vectorz
- The new z-coordinate of the vector
-
length
public double length()Gives the length of the vector. This is the same as the vector's magnitude.- Returns:
- The length of the vector
-
multiply
Performs a scalar multiplication on the vector. Multiplying by negative numbers will negate the vector.- Parameters:
factor
- The constant by which to multiply the vector with- Returns:
- The new scaled Vector3
-
scale
Scales the current vector by performing a scalar multiplication on it. Scaling by negative numbers will negate the vector. (Changes the current vector)- Parameters:
factor
- The constant by which to multiply the vector with- Returns:
- The new current vector
-
abs
Returns the absolute value of the current vector- Returns:
- the current vector, but with no negative coordinates
-
getUnitVector
Gives the unit vector for the current vector. Unit vectors will maintain direction and have a length of 1 unit.- Returns:
- The unit vector for the current vector. If the vector has a length of 0, A Vector3 at (0,0,0) will be returned.
-
multiplyByVector
Multiplies each of the vector's components using scalar multiplication by their respective component in the factor vector. i.e. <1,2,3>.scaleByVector(<3,2,1>) = <3, 4, 3>- Parameters:
factor
- The Vector3 by which to scale the first vector- Returns:
- The new scaled Vector3
-
scaleByVector
Scales each of the current vector's components using scalar multiplication by their respective component in the factor vector. (Changes the current vector) i.e. <1,2,3>.scaleByVector(<3,2,1>) = <3, 4, 3>- Parameters:
factor
- The Vector3 by which to scale the first vector- Returns:
- The new current vector
-
findFactor
Finds the factor by which a scalar multiplication must be applied to the first vector in order to get the passed vector- Parameters:
v2
- The second vector to compare the first vector to.- Returns:
- The factor of times bigger (longer) the second vector is than the first. If the length of v2 > length of the first vector, the return value will be > 1, and if it is smaller, the a value between 0 and 1 will be returned. If the two vectors aren't parallel or aren't both non-zero in length, 0 will be returned.
-
add
Gives the resultant of adding two vectors together- Parameters:
v2
- The vector to be added to the first vector- Returns:
- The resultant Vector3
-
addInPlace
Translates the current vector by adding a given vector to it. (Changes the current vector)- Parameters:
v2
- The vector to add to the current vector- Returns:
- The new current vector
-
subtract
Gives the resultant vector of subtracting two vectors- Parameters:
v2
- The vector to be subtracted from the first vector- Returns:
- The resultant Vector3
-
subtractInPlace
Translates the current vector by subtracting a given vector from it. (Changes the current vector)- Parameters:
v2
- The vector to subtract from the current vector- Returns:
- The new current vector
-
translate
Translates the current vector by the given values (Changes the current vector)- Parameters:
x
- The amount to translate by in the x-axisy
- The amount to translate by in the y-axisz
- The amount to translate by in the z-axis- Returns:
- The new current vector
-
differenceVector
Gives the vector difference from the first vector to the second vector. i.e. to move from point A to point B, you'd translate by Vector C. Vector C = A.differenceVector(B)- Parameters:
v2
- The second vector- Returns:
- The vector of the difference, whose length is equal to the distance between the two vectors and is parallel to the direction of the first vector to the second.
-
distance
Gives the distance between two points represented by vectors- Parameters:
v2
- The second point to find the distance to- Returns:
- The distance to the second vector
-
dotProduct
Gives the dot product between two vectors- Parameters:
v2
- The second vector to use- Returns:
- The dot product result
-
crossProduct
Gives the vector that results from the cross product of two vectors. The cross product vector will be orthogonal to both initial vectors.- Parameters:
v2
- The second vector to use- Returns:
- The cross product resultant vector
-
angleBetween
Gives the angle between two vectors, in radians.- Parameters:
v2
- The second vector to form the angle- Returns:
- The angle, in radians, as a value in the range of [0.0, pi]. If either vector has a length of 0, 0 will be returned.
-
angleToXZ
public double angleToXZ()Gives the angle between the vector and the XZ plane, in radians- Returns:
- The angle, in radians, as a value in the range of [0.0, pi]. If the vector has a length of 0, 0 will be returned.
-
angleToXY
public double angleToXY()Gives the angle between the vector and the XY plane, in radians- Returns:
- The angle, in radians, as a value in the range of [0.0, pi]. If the vector has a length of 0, 0 will be returned.
-
angleToYZ
public double angleToYZ()Gives the angle between the vector and the YZ plane, in radians- Returns:
- The angle, in radians, as a value in the range of [0.0, pi]. If the vector has a length of 0, 0 will be returned.
-
projection
Gives the vector result of a projection of the first vector onto the second vector- Parameters:
v2
- The second vector on which the first vector is projected onto- Returns:
- The vector component of the first vector in the direction of the second vector. If the length of the second vector equals 0, a zero vector will be returned.
-
scalarProjection
Gives the scalar projection of the first vector onto the second vector. The unit vector of the second vector multiplied by the scalar projection yields the vector projection of the two vectors.- Parameters:
v2
- The second vector on which the first vector is projected onto- Returns:
- The scalar projection of the first vector in the direction of the second vector. If the length of the second vector equals 0, the length of the first vector will be reurned.
-
rotateX
Give the result vector of a Euler rotation about the x-axis- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The vector that results from this rotation
-
rotateInPlaceX
Performs a Euler rotation on the current vector about the x-axis (Changes the current vector)- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The new current vector
-
rotateY
Give the result vector of a Euler rotation about the y-axis- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The vector that results from this rotation
-
rotateInPlaceY
Performs a Euler rotation on the current vector about the y-axis (Changes the current vector)- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The new current vector
-
rotateZ
Give the result vector of a Euler rotation about the z-axis- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The vector that results from this rotation
-
rotateInPlaceZ
Performs a Euler rotation on the current vector about the z-axis (Changes the current vector)- Parameters:
angle
- The angle to rotate, in radians- Returns:
- The new current vector
-
equals
Checks if two vectors are equal to each other- Parameters:
v2
- The second vector to compare to- Returns:
- Whether or not the two vectors are equal
-
equals
public boolean equals(double x, double y, double z)Checks if the current vectors is equal to the vector with the given coordinates- Parameters:
x
- The x-coordinate of the second vector to compare toy
- The y-coordinate of the second vector to compare toz
- The z-coordinate of the second vector to compare to- Returns:
- Whether or not the two vectors are equal
-
clone
Clones the current vector- Overrides:
clone
in classjava.lang.Object
- Returns:
- a copy of the current vector
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-