Class Vector2D

Object
Point2D
Vector2D
All Implemented Interfaces:
Point

public class Vector2D extends Point2D
Utility class for 2D vector manipulations.
Author:
Christoph Hauert
  • Constructor Details

    • Vector2D

      public Vector2D()
      Create a new 2D vector (0,0).
    • Vector2D

      public Vector2D(Point2D p)
      Create a new 2D vector from point/vector p.
      Parameters:
      p - the point/vector to copy
    • Vector2D

      public Vector2D(double x, double y)
      Create a new 2D vector with coordinates (x,y).
      Parameters:
      x - the x-coordinate
      y - the y-coordinate
  • Method Details

    • set

      public Vector2D set(Point2D from, Point2D to)
      Set the 2D vector to pointing from from to to.
      Parameters:
      from - the starting point
      to - the end point
      Returns:
      the new vector
    • add

      public Vector2D add(double dx, double dy)
      Shift the coordinates of this 2D vector dx to the right and dy upwards.
      Parameters:
      dx - the shift in the x-coordinate
      dy - the shift in the y-coordinate
      Returns:
      the shifted vector (x+dx,y+dy)
      See Also:
    • add

      public Vector2D add(Vector2D add)
      Add the 2D vector add to this vector.
      Parameters:
      add - the vector to add
      Returns:
      the new vector
    • add

      public Vector2D add(Point2D a, Point2D b)
      Add the 2D vectors a and b and store the result in this vector.
      Parameters:
      a - the first vector
      b - the second vector
      Returns:
      the new vector a+b
      See Also:
    • sub

      public Vector2D sub(double dx, double dy)
      Subtract dx and dy from coordinates, i.e. shift x-coordinate to the left by dx and the y-coordinate down by dy. This yields the same result as add(-dx, -dy).
      Parameters:
      dx - the shift in the x-coordinate
      dy - the shift in the y-coordinate
      Returns:
      the new vector (x-dx,y-dy)
    • sub

      public Vector2D sub(Vector2D sub)
      Subtract the 2D vector sub from this vector.
      Parameters:
      sub - the vector to subtract
      Returns:
      the new vector
    • sub

      public Vector2D sub(Vector2D a, Vector2D b)
      Subtract 2D vectors b from a and store result in this 2D vector.
      Parameters:
      a - the first vector for subtraction
      b - the vector to subtract from a
      Returns:
      the vector a-b
      See Also:
    • dot

      public double dot(Vector2D d)
      Calculate the dot product of 2D vector d and this vector.
      Parameters:
      d - the vector to calculate the dot product with
      Returns:
      the dot product v.d
    • negate

      public Vector2D negate()
      Negates the coordinates of 2D vector v to obtain -v.
      Returns:
      the vector v=-v
    • normalize

      public Vector2D normalize()
      Normalizes the 2D vector v such that its length is |v|=1 while preserving the direction.
      Returns:
      the normalized vector
      See Also:
    • normalize

      public Vector2D normalize(double l)
      Normalizes the 2D vector v such that its length is l, i.e. |v|=l, while preserving the direction.
      Parameters:
      l - the new length of the vector
      Returns:
      the scaled vector
      See Also:
    • scaleAdd

      public Vector2D scaleAdd(double s, Vector2D a)
      Scales the 2D vector by the scalar factor s and adds the 2D vector a to obtain s*v+a. This is a shortcut for v.scale(s).add(a).
      Parameters:
      s - the scalar factor
      a - the vector to add
      Returns:
      the new vector s*v+a
      See Also:
    • length

      public double length()
      Calculate the length of the 2D vector.

      For computational efficiency the fairly expensive square-roots calculations should be avoided whenever possible.

      Returns:
      the length of vector |v|
      See Also:
    • length2

      public double length2()
      Calculate the length squared of the 2D vector, |v|2.
      Returns:
      the length squared
      See Also:
    • lengthSquared

      public double lengthSquared()
      Same as length2(). For (historical) compatibility with Vector2f from java3d.
      Returns:
      the length squared
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Point2D