Class Vector2D


public class Vector2D
A simple mutable 2D vector with basic linear algebra operations (addition, subtraction, scaling, dot product, length and unit/normal vector).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
    The x component of the vector.
    protected double
    The y component of the vector.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new zero vector.
    Vector2D(double dX, double dY)
    Creates a new vector with the supplied components.
    Creates a new vector pointing from p1 to p2.
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds another vector to this one and returns the resulting new vector.
    double
    Computes the dot product of this vector with another.
    double
    Returns the x component of the vector.
    double
    Returns the y component of the vector.
    double
    Returns the Euclidean length of the vector.
    Returns a vector perpendicular to this one, obtained by rotating it 90 degrees clockwise.
    scale(double scaleFactor)
    Returns this vector scaled by the supplied factor.
    Subtracts another vector from this one and returns the resulting new vector.
     
    Returns the unit vector pointing in the same direction as this vector, or the zero vector if this vector has length zero.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • dX

      protected double dX
      The x component of the vector.
    • dY

      protected double dY
      The y component of the vector.
  • Constructor Details

    • Vector2D

      public Vector2D()
      Creates a new zero vector.
    • Vector2D

      public Vector2D(double dX, double dY)
      Creates a new vector with the supplied components.
      Parameters:
      dX - the x component
      dY - the y component
    • Vector2D

      public Vector2D(Point2D p1, Point2D p2)
      Creates a new vector pointing from p1 to p2.
      Parameters:
      p1 - the start point
      p2 - the end point
  • Method Details

    • add

      public Vector2D add(Vector2D v1)
      Adds another vector to this one and returns the resulting new vector.
      Parameters:
      v1 - the vector to add
      Returns:
      the sum vector
    • dotProduct

      public double dotProduct(Vector2D v1)
      Computes the dot product of this vector with another.
      Parameters:
      v1 - the other vector
      Returns:
      the dot product
    • getX

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

      public double getY()
      Returns the y component of the vector.
      Returns:
      the y component
    • length

      public double length()
      Returns the Euclidean length of the vector.
      Returns:
      the length
    • normalVector

      public Vector2D normalVector()
      Returns a vector perpendicular to this one, obtained by rotating it 90 degrees clockwise.
      Returns:
      the perpendicular vector
    • scale

      public Vector2D scale(double scaleFactor)
      Returns this vector scaled by the supplied factor.
      Parameters:
      scaleFactor - the scale factor
      Returns:
      the scaled vector
    • sub

      public Vector2D sub(Vector2D v1)
      Subtracts another vector from this one and returns the resulting new vector.
      Parameters:
      v1 - the vector to subtract
      Returns:
      the difference vector
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • unitVector

      public Vector2D unitVector()
      Returns the unit vector pointing in the same direction as this vector, or the zero vector if this vector has length zero.
      Returns:
      the unit vector