Class Vector3D


  • public class Vector3D
    extends Object
    Represents a vector in 3-dimensional Cartesian space.
    Author:
    mdavis
    • Constructor Detail

      • Vector3D

        public Vector3D​(Coordinate v)
        Creates a new 3D vector from a Coordinate. The coordinate should have the X,Y and Z ordinates specified.
        Parameters:
        v - the Coordinate to copy
      • Vector3D

        public Vector3D​(Coordinate from,
                        Coordinate to)
        Creates a new vector with the direction and magnitude of the difference between the to and from Coordinates.
        Parameters:
        from - the origin Coordinate
        to - the destination Coordinate
      • Vector3D

        public Vector3D​(double x,
                        double y,
                        double z)
        Creates a vector with the givne components.
        Parameters:
        x - the X component
        y - the Y component
        z - the Z component
    • Method Detail

      • dot

        public static double dot​(Coordinate A,
                                 Coordinate B,
                                 Coordinate C,
                                 Coordinate D)
        Computes the dot product of the 3D vectors AB and CD.
        Parameters:
        A - the start point of the first vector
        B - the end point of the first vector
        C - the start point of the second vector
        D - the end point of the second vector
        Returns:
        the dot product
      • create

        public static Vector3D create​(double x,
                                      double y,
                                      double z)
        Creates a new vector with given X, Y and Z components.
        Parameters:
        x - the X component
        y - the Y component
        z - the Z component
        Returns:
        a new vector
      • create

        public static Vector3D create​(Coordinate coord)
        Creates a vector from a 3D Coordinate. The coordinate should have the X,Y and Z ordinates specified.
        Parameters:
        coord - the Coordinate to copy
        Returns:
        a new vector
      • dot

        public static double dot​(Coordinate v1,
                                 Coordinate v2)
        Computes the 3D dot-product of two Coordinates.
        Parameters:
        v1 - the first vector
        v2 - the second vector
        Returns:
        the dot product of the vectors
      • getX

        public double getX()
        Gets the X component of this vector.
        Returns:
        the value of the X component
      • getY

        public double getY()
        Gets the Y component of this vector.
        Returns:
        the value of the Y component
      • getZ

        public double getZ()
        Gets the Z component of this vector.
        Returns:
        the value of the Z component
      • add

        public Vector3D add​(Vector3D v)
        Computes a vector which is the sum of this vector and the given vector.
        Parameters:
        v - the vector to add
        Returns:
        the sum of this and v
      • subtract

        public Vector3D subtract​(Vector3D v)
        Computes a vector which is the difference of this vector and the given vector.
        Parameters:
        v - the vector to subtract
        Returns:
        the difference of this and v
      • divide

        public Vector3D divide​(double d)
        Creates a new vector which has the same direction and with length equals to the length of this vector divided by the scalar value d.
        Parameters:
        d - the scalar divisor
        Returns:
        a new vector with divided length
      • dot

        public double dot​(Vector3D v)
        Computes the dot-product of two vectors
        Parameters:
        v - a vector
        Returns:
        the dot product of the vectors
      • length

        public double length()
        Computes the length of this vector.
        Returns:
        the length of the vector
      • length

        public static double length​(Coordinate v)
        Computes the length of a vector.
        Parameters:
        v - a coordinate representing a 3D vector
        Returns:
        the length of the vector
      • normalize

        public Vector3D normalize()
        Computes a vector having identical direction but normalized to have length 1.
        Returns:
        a new normalized vector
      • normalize

        public static Coordinate normalize​(Coordinate v)
        Computes a vector having identical direction but normalized to have length 1.
        Parameters:
        v - a coordinate representing a 3D vector
        Returns:
        a coordinate representing the normalized vector
      • toString

        public String toString()
        Gets a string representation of this vector
        Overrides:
        toString in class Object
        Returns:
        a string representing this vector
      • equals

        public boolean equals​(Object o)
        Tests if a vector o has the same values for the components.
        Overrides:
        equals in class Object
        Parameters:
        o - a Vector3D with which to do the comparison.
        Returns:
        true if other is a Vector3D with the same values for the x and y components.
      • hashCode

        public int hashCode()
        Gets a hashcode for this vector.
        Overrides:
        hashCode in class Object
        Returns:
        a hashcode for this vector