Package primitives

Class Vector

java.lang.Object
primitives.Triple
primitives.Vector
Direct Known Subclasses:
NonZeroVector

public class Vector
extends Triple
The Vector class represents a NonZeroVector with the single difference that the VectorBase can be the zero vector. Let me repeat that. This class can have values of (0,0,0).
  • Constructor Details

    • Vector

      public Vector​(double x, double y, double z)
      This constructor accepts 3 doubles and returns the appropriate Vector
      Parameters:
      x - The value of the x coordinate.
      y - The value of the y coordinate.
      z - The value of the z coordinate.
      Throws:
      ZeroVectorException - if the Vector is the zero vector.
  • Method Details

    • add

      public <T extends Triple> T add​(Triple triple, DoubleTriFunction<T> creator)
      Adds a Triple to this Vector and returns a new Triple of the same type returned by creator.
      Type Parameters:
      T - The type of Triple to return.
      Parameters:
      triple - The Vector which is to be added to this Vector.
      creator - A function which receives three doubles and returns a new Triple.
      Returns:
      The sum of the two Vectors.
    • add

      public Vector add​(Triple triple)
      Adds two Vectors and returns a new Vector.
      Parameters:
      triple - The Triple which is to be added to this Vector.
      Returns:
      The sum of the two Vectors.
    • subtract

      public <T extends Vector> T subtract​(Vector vector, DoubleTriFunction<T> creator)
      Subtracts two Vectors and returns a new Vector of the type returned by creator.
      Type Parameters:
      T - The type of Triple to return.
      Parameters:
      vector - The Vector to be subtracted from this Vector.
      creator - A function which receives three doubles and returns a new Vector.
      Returns:
      The sum of this Vector and the negation of the given Vector.
    • subtract

      public Vector subtract​(Vector vector)
      Subtracts two Vectors and returns a new Vector.
      Parameters:
      vector - The Vector to be subtracted from this Vector.
      Returns:
      The sum of this Vector and the negation of the given Vector.
    • scale

      public <T extends Vector> T scale​(double factor, DoubleTriFunction<T> creator)
      Constructs a new Vector which is a scalar multiplication of this Vector by a scalar.
      Type Parameters:
      T - The type of Triple to return.
      Parameters:
      factor - The scalar by which to multiply this Vector
      creator - A function which receives three doubles and returns a new Vector.
      Returns:
      New scaled Vector, of the concrete type as returned by creator.
    • scale

      public Vector scale​(double factor)
      Constructs a new Vector which is a scalar multiplication of this Vector by a scalar.
      Parameters:
      factor - The scalar by which to multiply this Vector
      Returns:
      New scaled Vector
      Throws:
      ZeroVectorException - if the scale factor is zero.
    • reversed

      public Vector reversed()
      An alias for scale(double) with factor -1.
      Returns:
      New reversed Vector
    • cross

      public <T extends Vector> T cross​(Vector v, DoubleTriFunction<T> creator)
      Calculates the cross product of two Vectors.
      Type Parameters:
      T - The type of Triple to return.
      Parameters:
      v - The Vector by which to multiply this Vector
      creator - A function which receives three doubles and returns a new Vector
      Returns:
      The resulting Vector which is the cross product of the two Vectors, but of the concrete type as returned by creator
    • cross

      public Vector cross​(Vector v)
      Calculates the cross product of two Vectors.
      Parameters:
      v - The Vector by which to multiply this Vector
      Returns:
      The resulting Vector which is the cross product of the two Vectors
      Throws:
      ZeroVectorException - if the result vector is the zero vector.
    • dot

      public double dot​(Triple v)
      Calculates the dot product of two Vectors
      Parameters:
      v - The Vector to dot product with this Vector
      Returns:
      The dot product of the two Vectors
    • length

      public double length()
      Calculates the length of this Vector.
      Returns:
      The length of this Vector.
    • squareLength

      public double squareLength()
      Calculates the square of the length of this Vector.
      Returns:
      The square of the length of this Vector.
    • toString

      public java.lang.String toString()
      Description copied from class: Triple
      Returns the Triple as a string in the cartesian representation, e.g. "(0, 0, 0)"
      Overrides:
      toString in class Triple