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

    Fields
    Modifier and Type Field Description
    private double x
    The vector's x-coordinate
    private double y
    The vector's y-coordinate
    private double z
    The vector's z-coordinate
  • 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 coordinates
    Vector3​(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 vector
    Vector3 add​(Vector3 v2)
    Gives the resultant of adding two vectors together
    Vector3 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 radians
    double angleToXZ()
    Gives the angle between the vector and the XZ plane, in radians
    double angleToYZ()
    Gives the angle between the vector and the YZ plane, in radians
    Vector3 clone()
    Clones the current vector
    Vector3 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 vectors
    double dotProduct​(Vector3 v2)
    Gives the dot product between two vectors
    boolean equals​(double x, double y, double z)
    Checks if the current vectors is equal to the vector with the given coordinates
    boolean equals​(Vector3 v2)
    Checks if two vectors are equal to each other
    double findFactor​(Vector3 v2)
    Finds the factor by which a scalar multiplication must be applied to the first vector in order to get the passed vector
    Vector3 getUnitVector()
    Gives the unit vector for the current vector.
    double getX()
    Gives the current x-coordinate of the vector
    double getY()
    Gives the current y-coordinate of the vector
    double getZ()
    Gives the current z-coordinate of the vector
    double 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 vector
    void 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-axis
    Vector3 rotateY​(double angle)
    Give the result vector of a Euler rotation about the y-axis
    Vector3 rotateZ​(double angle)
    Give the result vector of a Euler rotation about the z-axis
    double 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 vector
    void setY​(double y)
    Gives the current y-coordinate of the vector
    void setZ​(double z)
    Gives the current z-coordinate of the vector
    Vector3 subtract​(Vector3 v2)
    Gives the resultant vector of subtracting two vectors
    Vector3 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 x
      The vector's x-coordinate
    • y

      private double y
      The vector's y-coordinate
    • z

      private double z
      The vector's z-coordinate
  • Constructor Details

    • Vector3

      public Vector3()
      Creates a default zero Vector3 at (0, 0, 0)
    • Vector3

      public Vector3​(Vector3 unitVector, double length)
      Creates a Vector3 in the direction of the given unit vector and with the given lentgh (magnitude)
      Parameters:
      unitVector - The unit vector for the Vector3
      length - 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 coordinate
      y - The y-axis coordinate
      z - 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

      public void replace​(Vector3 newVector)
      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 vector
      y - The new y-coordinate of the vector
      z - 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

      public Vector3 multiply​(double factor)
      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

      public Vector3 scale​(double factor)
      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

      public Vector3 abs()
      Returns the absolute value of the current vector
      Returns:
      the current vector, but with no negative coordinates
    • getUnitVector

      public Vector3 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

      public Vector3 multiplyByVector​(Vector3 factor)
      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

      public Vector3 scaleByVector​(Vector3 factor)
      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

      public double findFactor​(Vector3 v2)
      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

      public Vector3 add​(Vector3 v2)
      Gives the resultant of adding two vectors together
      Parameters:
      v2 - The vector to be added to the first vector
      Returns:
      The resultant Vector3
    • addInPlace

      public Vector3 addInPlace​(Vector3 v2)
      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

      public Vector3 subtract​(Vector3 v2)
      Gives the resultant vector of subtracting two vectors
      Parameters:
      v2 - The vector to be subtracted from the first vector
      Returns:
      The resultant Vector3
    • subtractInPlace

      public Vector3 subtractInPlace​(Vector3 v2)
      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

      public Vector3 translate​(double x, double y, double z)
      Translates the current vector by the given values (Changes the current vector)
      Parameters:
      x - The amount to translate by in the x-axis
      y - The amount to translate by in the y-axis
      z - The amount to translate by in the z-axis
      Returns:
      The new current vector
    • differenceVector

      public Vector3 differenceVector​(Vector3 v2)
      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

      public double distance​(Vector3 v2)
      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

      public double dotProduct​(Vector3 v2)
      Gives the dot product between two vectors
      Parameters:
      v2 - The second vector to use
      Returns:
      The dot product result
    • crossProduct

      public Vector3 crossProduct​(Vector3 v2)
      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

      public double angleBetween​(Vector3 v2)
      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

      public Vector3 projection​(Vector3 v2)
      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

      public double scalarProjection​(Vector3 v2)
      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

      public Vector3 rotateX​(double angle)
      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

      public Vector3 rotateInPlaceX​(double angle)
      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

      public Vector3 rotateY​(double angle)
      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

      public Vector3 rotateInPlaceY​(double angle)
      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

      public Vector3 rotateZ​(double angle)
      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

      public Vector3 rotateInPlaceZ​(double angle)
      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

      public boolean equals​(Vector3 v2)
      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 to
      y - The y-coordinate of the second vector to compare to
      z - The z-coordinate of the second vector to compare to
      Returns:
      Whether or not the two vectors are equal
    • clone

      public Vector3 clone()
      Clones the current vector
      Overrides:
      clone in class java.lang.Object
      Returns:
      a copy of the current vector
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object