Module org.dyn4j

Class Vector2


  • public class Vector2
    extends java.lang.Object
    This class represents a vector or point in 2D space.

    The operations setMagnitude(double), getNormalized(), project(Vector2), and normalize() require the Vector2 to be non-zero in length.

    Some methods also return the vector to facilitate chaining. For example:

     Vector a = new Vector();
     a.zero().add(1, 2).multiply(2);
     
    Since:
    1.0.0
    Version:
    3.4.0
    Author:
    William Bittle
    • Field Detail

      • X_AXIS

        static final Vector2 X_AXIS
        A vector representing the x-axis; this vector should not be changed at runtime; used internally
      • Y_AXIS

        static final Vector2 Y_AXIS
        A vector representing the y-axis; this vector should not be changed at runtime; used internally
      • INV_X_AXIS

        static final Vector2 INV_X_AXIS
        A vector representing the inverse x-axis; this vector should not be changed at runtime; used internally
      • INV_Y_AXIS

        static final Vector2 INV_Y_AXIS
        A vector representing the inverse y-axis; this vector should not be changed at runtime; used internally
      • x

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

        public double y
        The magnitude of the y component of this Vector2
    • Constructor Detail

      • Vector2

        public Vector2()
        Default constructor.
      • Vector2

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

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

        public Vector2​(double x1,
                       double y1,
                       double x2,
                       double y2)
        Creates a Vector2 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
        x2 - the x coordinate of the second point
        y2 - the y coordinate of the second point
      • Vector2

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

        public Vector2​(double direction)
        Creates a unit length vector in the given direction.
        Parameters:
        direction - the direction in radians
        Since:
        3.0.1
    • Method Detail

      • create

        public static Vector2 create​(double magnitude,
                                     double direction)
        Returns a new Vector2 given the magnitude and direction.
        Parameters:
        magnitude - the magnitude of the Vector2
        direction - the direction of the Vector2 in radians
        Returns:
        Vector2
      • distance

        public double distance​(double x,
                               double y)
        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
        Returns:
        double
      • distance

        public double distance​(Vector2 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)
        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
        Returns:
        double
      • distanceSquared

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

        public static Vector2 tripleProduct​(Vector2 a,
                                            Vector2 b,
                                            Vector2 c)
        The triple product of Vector2s 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 Vector2 in the above equation
        b - the b Vector2 in the above equation
        c - the c Vector2 in the above equation
        Returns:
        Vector2
      • hashCode

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

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • equals

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

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

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

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

        public Vector2 getYComponent()
        Returns the y component of this Vector2.
        Returns:
        Vector2
      • getMagnitude

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

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

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

        public double getDirection()
        Returns the direction of this Vector2 as an angle in radians.
        Returns:
        double angle in radians [-π, π]
      • setDirection

        public Vector2 setDirection​(double angle)
        Sets the direction of this Vector2.
        Parameters:
        angle - angle in radians
        Returns:
        Vector2 this vector
      • subtract

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

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

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

        public Vector2 divide​(double scalar)
        Divides this Vector2 by the given scalar.
        Parameters:
        scalar - the scalar
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • product

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

        public Vector2 quotient​(double scalar)
        Divides this Vector2 by the given scalar returning a new Vector2 containing the result.
        Parameters:
        scalar - the scalar
        Returns:
        Vector2
        Since:
        3.4.0
      • dot

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

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

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

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

        public Vector2 cross​(double z)
        Returns the cross product of this Vector2 and the z value of the right Vector2.
        Parameters:
        z - the z component of the Vector2
        Returns:
        Vector2
      • isOrthogonal

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

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

        Parameters:
        vector - the Vector2
        Returns:
        boolean
      • isOrthogonal

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

        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 Vector2
        y - the y component of the Vector2
        Returns:
        boolean
      • isZero

        public boolean isZero()
        Returns true if this Vector2 is the zero Vector2.
        Returns:
        boolean
      • rotate

        Vector2 rotate​(double cos,
                       double sin)
        Internal helper method that rotates about the origin by an angle θ.
        Parameters:
        cos - cos(θ)
        sin - sin(θ)
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • rotate

        public Vector2 rotate​(double theta)
        Rotates about the origin.
        Parameters:
        theta - the rotation angle in radians
        Returns:
        Vector2 this vector
      • rotate

        public Vector2 rotate​(Rotation rotation)
        Rotates about the origin.
        Parameters:
        rotation - the Rotation
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(double theta)
        Rotates about the origin by the inverse angle -ϑ.
        Parameters:
        theta - the rotation angle in radians
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(Rotation rotation)
        Rotates about the origin by the inverse angle -ϑ.
        Parameters:
        rotation - the Rotation
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • rotate

        Vector2 rotate​(double cos,
                       double sin,
                       double x,
                       double y)
        Internal helper method that rotates about the given coordinates by an angle θ.
        Parameters:
        cos - cos(θ)
        sin - sin(θ)
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • rotate

        public Vector2 rotate​(double theta,
                              double x,
                              double y)
        Rotates the Vector2 about the given coordinates.
        Parameters:
        theta - the rotation angle in radians
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
        Returns:
        Vector2 this vector
      • rotate

        public Vector2 rotate​(Rotation rotation,
                              double x,
                              double y)
        Rotates the Vector2 about the given coordinates.
        Parameters:
        rotation - the Rotation
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(double theta,
                                     double x,
                                     double y)
        Rotates about the given coordinates by the inverse angle -ϑ.
        Parameters:
        theta - the rotation angle in radians
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(Rotation rotation,
                                     double x,
                                     double y)
        Rotates about the given coordinates by the inverse angle -ϑ.
        Parameters:
        rotation - the Rotation
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • rotate

        public Vector2 rotate​(double theta,
                              Vector2 point)
        Rotates the Vector2 about the given point.
        Parameters:
        theta - the rotation angle in radians
        point - the point to rotate about
        Returns:
        Vector2 this vector
      • rotate

        public Vector2 rotate​(Rotation rotation,
                              Vector2 point)
        Rotates the Vector2 about the given point.
        Parameters:
        rotation - the Rotation
        point - the point to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(double theta,
                                     Vector2 point)
        Rotates the Vector2 about the given point by the inverse angle -ϑ.
        Parameters:
        theta - the rotation angle in radians
        point - the point to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • inverseRotate

        public Vector2 inverseRotate​(Rotation rotation,
                                     Vector2 point)
        Rotates the Vector2 about the given point by the inverse angle -ϑ.
        Parameters:
        rotation - the Rotation
        point - the point to rotate about
        Returns:
        Vector2 this vector
        Since:
        3.4.0
      • getRightHandOrthogonalVector

        public Vector2 getRightHandOrthogonalVector()
        Returns the right-handed normal of this vector.
        Returns:
        Vector2 the right hand orthogonal Vector2
      • getLeftHandOrthogonalVector

        public Vector2 getLeftHandOrthogonalVector()
        Returns the left-handed normal of this vector.
        Returns:
        Vector2 the left hand orthogonal Vector2
      • normalize

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

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

        Returns:
        double
      • getAngleBetween

        public double getAngleBetween​(Vector2 vector)
        Returns the smallest angle between the given Vector2s.

        Returns the angle in radians in the range -π to π.

        Parameters:
        vector - the Vector2
        Returns:
        angle in radians [-π, π]
      • getAngleBetween

        public double getAngleBetween​(double otherAngle)
        Returns the smallest angle between the given Vector2 and the given angle.

        Returns the angle in radians in the range -π to π.

        Parameters:
        otherAngle - the angle. Must be in the range -π to π
        Returns:
        angle in radians [-π, π]
        Since:
        3.4.0