Class Vector2

java.lang.Object
org.openpatch.scratch.extensions.math.Vector2

public class Vector2 extends Object
The Vector2 class represents a 2D vector with x and y coordinates. It provides various methods for vector operations such as addition, subtraction, scalar multiplication, dot product, rotation, and more.

Instances of this class are immutable.

Example usage:


 Vector2 v1 = new Vector2(3, 4);
 Vector2 v2 = new Vector2(1, 2);
 Vector2 sum = v1.add(v2);
 double length = v1.length();
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the null vector.
    Vector2(double x, double y)
    Constructor for a 2D vector.
    Copy constructor for copying a vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds another vector and returns the result
    double
    Returns the angle of the vector.
    Creates a clone of the vector
    double
    Calculates the distance to another vector.
    double
    Calculates the squared distance to another vector.
    double
    Calculates the dot product.
    boolean
     
    static Vector2
    fromPolar(double magnitude, double angle)
    Constructor method for constructing a vector based on polar coordinates.
    double
    Returns the x coordinate of the vector
    double
    Returns the y coordinate of the vector
    int
     
    double
    Calculates the length of the vector.
    double
    Calculates the squared length of the vector.
    multiply(double scalar)
    Multiplies a scalar and return the result.
    Returns the normal vector, which is perpendicular to the vector.
    Reverses a vector and returns the result.
    rotateBy(double angle)
    Rotates the vector by an angle and returns the result.
    rotateTo(double angle)
    Rotates the vector to a given angle and returns the result.
    Substracts another vector and returns the result
     
    Returns the unit vector.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Vector2

      public Vector2()
      Constructor for the null vector.
    • Vector2

      public Vector2(double x, double y)
      Constructor for a 2D vector.
      Parameters:
      x - x-coordinate
      y - y-coordinate
    • Vector2

      public Vector2(Vector2 v)
      Copy constructor for copying a vector.
      Parameters:
      v - a vector
  • Method Details

    • fromPolar

      public static Vector2 fromPolar(double magnitude, double angle)
      Constructor method for constructing a vector based on polar coordinates.
      Parameters:
      magnitude - of the vector
      angle - of the vector
      Returns:
      a vector
    • length

      public double length()
      Calculates the length of the vector.
      Returns:
      the length of the vector
    • lengthSq

      public double lengthSq()
      Calculates the squared length of the vector.
      Returns:
      the squared length of the vector
    • distanceSq

      public double distanceSq(Vector2 v)
      Calculates the squared distance to another vector.
      Parameters:
      v - a vector
      Returns:
      the squared distance
    • distance

      public double distance(Vector2 v)
      Calculates the distance to another vector.
      Parameters:
      v - a vector
      Returns:
      the distance
    • angle

      public double angle()
      Returns the angle of the vector.
      Returns:
      the angle
    • unitVector

      public Vector2 unitVector()
      Returns the unit vector.
      Returns:
      a unit vector
    • normalVector

      public Vector2 normalVector()
      Returns the normal vector, which is perpendicular to the vector.
      Returns:
      a normal vector
    • add

      public Vector2 add(Vector2 v)
      Adds another vector and returns the result
      Returns:
      the sum
    • sub

      public Vector2 sub(Vector2 v)
      Substracts another vector and returns the result
      Returns:
      the difference
    • multiply

      public Vector2 multiply(double scalar)
      Multiplies a scalar and return the result.
      Parameters:
      scalar - a scalar
      Returns:
      the vector
    • dot

      public double dot(Vector2 v)
      Calculates the dot product.
      Parameters:
      v - a vector
      Returns:
      the dot product
    • rotateBy

      public Vector2 rotateBy(double angle)
      Rotates the vector by an angle and returns the result.
      Parameters:
      angle - an angle between [0, ..., 360]
      Returns:
      the rotated vector
    • rotateTo

      public Vector2 rotateTo(double angle)
      Rotates the vector to a given angle and returns the result.
      Parameters:
      angle - an angle between [0,...,360]
      Returns:
      the rotated vector
    • reverse

      public Vector2 reverse()
      Reverses a vector and returns the result.
      Returns:
      the reversed vector
    • getX

      public double getX()
      Returns the x coordinate of the vector
      Returns:
      the x coordinate
    • getY

      public double getY()
      Returns the y coordinate of the vector
      Returns:
      the y coordinate
    • clone

      public Vector2 clone()
      Creates a clone of the vector
      Returns:
      the cloned vector
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object