Module org.dyn4j

Class Vector3

  • All Implemented Interfaces:
    Copyable<Vector3>

    public class Vector3
    extends Object
    implements Copyable<Vector3>
    This class represents a vector or point in 3D space.

    Used to solve 3x3 systems of equations.

    Since:
    1.0.0
    Version:
    4.0.0
    Author:
    William Bittle
    See Also:
    Vector2
    • Field Detail

      • x

        public double x
        The magnitude of the x component of this Vector3
      • y

        public double y
        The magnitude of the y component of this Vector3
      • z

        public double z
        The magnitude of the z component of this Vector3
    • Constructor Detail

      • Vector3

        public Vector3()
        Default constructor.
      • Vector3

        public Vector3​(Vector3 vector)
        Copy constructor.
        Parameters:
        vector - the Vector3 to copy from
      • Vector3

        public Vector3​(double x,
                       double y,
                       double z)
        Optional constructor.
        Parameters:
        x - the x component
        y - the y component
        z - the z component
      • Vector3

        public Vector3​(double x1,
                       double y1,
                       double z1,
                       double x2,
                       double y2,
                       double z2)
        Creates a Vector3 from the first point to the second point.
        Parameters:
        x1 - the x coordinate of the first point
        y1 - the y coordinate of the first point
        z1 - the z coordinate of the first point
        x2 - the x coordinate of the second point
        y2 - the y coordinate of the second point
        z2 - the z coordinate of the second point
      • Vector3

        public Vector3​(Vector3 p1,
                       Vector3 p2)
        Creates a Vector3 from the first point to the second point.
        Parameters:
        p1 - the first point
        p2 - the second point
    • Method Detail

      • copy

        public Vector3 copy()
        Description copied from interface: Copyable
        Returns a deep copy of this object.
        Specified by:
        copy in interface Copyable<Vector3>
        Returns:
        T
      • distance

        public double distance​(double x,
                               double y,
                               double z)
        Returns the distance from this point to the given point.
        Parameters:
        x - the x coordinate of the point
        y - the y coordinate of the point
        z - the z coordinate of the point
        Returns:
        double
      • distance

        public double distance​(Vector3 point)
        Returns the distance from this point to the given point.
        Parameters:
        point - the point
        Returns:
        double
      • distanceSquared

        public double distanceSquared​(double x,
                                      double y,
                                      double z)
        Returns the distance from this point to the given point squared.
        Parameters:
        x - the x coordinate of the point
        y - the y coordinate of the point
        z - the z coordinate of the point
        Returns:
        double
      • distanceSquared

        public double distanceSquared​(Vector3 point)
        Returns the distance from this point to the given point squared.
        Parameters:
        point - the point
        Returns:
        double
      • tripleProduct

        public static Vector3 tripleProduct​(Vector3 a,
                                            Vector3 b,
                                            Vector3 c)
        The triple product of Vector3s is defined as:
         a x (b x c)
         
        However, this method performs the following triple product:
         (a x b) x c
         
        this can be simplified to:
         -a * (b · c) + b * (a · c)
         
        or:
         b * (a · c) - a * (b · c)
         
        Parameters:
        a - the a Vector3 in the above equation
        b - the b Vector3 in the above equation
        c - the c Vector3 in the above equation
        Returns:
        Vector3
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Vector3 vector)
        Returns true if the x and y components of this Vector3 are the same as the given Vector3.
        Parameters:
        vector - the Vector3 to compare to
        Returns:
        boolean
      • equals

        public boolean equals​(double x,
                              double y,
                              double z)
        Returns true if the x, y and z components of this Vector3 are the same as the given x, y and z components.
        Parameters:
        x - the x coordinate of the Vector3 to compare to
        y - the y coordinate of the Vector3 to compare to
        z - the z coordinate of the Vector3 to compare to
        Returns:
        boolean
      • getXComponent

        public Vector3 getXComponent()
        Returns the x component of this Vector3.
        Returns:
        Vector3
      • getYComponent

        public Vector3 getYComponent()
        Returns the y component of this Vector3.
        Returns:
        Vector3
      • getZComponent

        public Vector3 getZComponent()
        Returns the z component of this Vector3.
        Returns:
        Vector3
      • getMagnitude

        public double getMagnitude()
        Returns the magnitude of this Vector3.
        Returns:
        double
      • getMagnitudeSquared

        public double getMagnitudeSquared()
        Returns the magnitude of this Vector3 squared.
        Returns:
        double
      • setMagnitude

        public Vector3 setMagnitude​(double magnitude)
        Sets the magnitude of the Vector3.
        Parameters:
        magnitude - the magnitude
        Returns:
        Vector3 this vector
      • add

        public Vector3 add​(double x,
                           double y,
                           double z)
        Adds the given Vector3 to this Vector3.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        Vector3 this vector
      • sum

        public Vector3 sum​(double x,
                           double y,
                           double z)
        Adds this Vector3 and the given Vector3 returning a new Vector3 containing the result.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        Vector3
      • subtract

        public Vector3 subtract​(double x,
                                double y,
                                double z)
        Subtracts the given Vector3 from this Vector3.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        Vector3 this vector
      • difference

        public Vector3 difference​(double x,
                                  double y,
                                  double z)
        Subtracts the given Vector3 from this Vector3 returning a new Vector3 containing the result.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        Vector3
      • multiply

        public Vector3 multiply​(double scalar)
        Multiplies this Vector3 by the given scalar.
        Parameters:
        scalar - the scalar
        Returns:
        Vector3 this vector
      • product

        public Vector3 product​(double scalar)
        Multiplies this Vector3 by the given scalar returning a new Vector3 containing the result.
        Parameters:
        scalar - the scalar
        Returns:
        Vector3
      • dot

        public double dot​(Vector3 vector)
        Returns the dot product of the given Vector3 and this Vector3.
        Parameters:
        vector - the Vector3
        Returns:
        double
      • dot

        public double dot​(double x,
                          double y,
                          double z)
        Returns the dot product of the given Vector3 and this Vector3.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        double
      • cross

        public Vector3 cross​(double x,
                             double y,
                             double z)
        Returns the cross product of the this Vector3 and the given Vector3.
        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        Vector3
      • isOrthogonal

        public boolean isOrthogonal​(Vector3 vector)
        Returns true if the given Vector3 is orthogonal (perpendicular) to this Vector3.

        If the dot product of this vector and the given vector is zero then we know that they are perpendicular

        Parameters:
        vector - the Vector3
        Returns:
        boolean
      • isOrthogonal

        public boolean isOrthogonal​(double x,
                                    double y,
                                    double z)
        Returns true if the given Vector3 is orthogonal (perpendicular) to this Vector3.

        If the dot product of this vector and the given vector is zero then we know that they are perpendicular

        Parameters:
        x - the x component of the Vector3
        y - the y component of the Vector3
        z - the z component of the Vector3
        Returns:
        boolean
      • isZero

        public boolean isZero()
        Returns true if this Vector3 is the zero Vector3.
        Returns:
        boolean
      • normalize

        public double normalize()
        Converts this Vector3 into a unit Vector3 and returns the magnitude before normalization.

        This method requires the length of this Vector3 is not zero.

        Returns:
        double