Class Vector2D

java.lang.Object
yearreview.app.animation.Vector2D

public class Vector2D extends Object
A class representing an immutable 2D-Vector of Floats. Similar to Point2D but allows for direct access of the x and y values.
Author:
ColdStone37
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Vector2D
    A constant Vector facing downwards: (0, 1)
    static final Vector2D
    A constant Vector facing left: (-1, 0)
    static final Vector2D
    A constant Vector facing right: (1, 0)
    static final Vector2D
    A constant Vector facing upwards: (0, -1)
    final float
    X-value of the Vector.
    final float
    Y-value of the Vector.
    static final Vector2D
    A constant zero Vector: (0, 0)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Vector2D(float x, float y)
    Constructs a new Vector from an x- and y-value.
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets an inverted version of this Vector: (-x, -y)
    float
    Gets the length of this Vector using the Pythagoras-Theorem: sqrt(x * x + y * y)
    Gets a normalized version of this Vector.
    getScaled(float scale)
    Gets this Vector scaled by a passed value.
    minus(Vector2D other)
    Gets a new Vector which is the result of subtracting this Vector with a passed Vector.
    static Vector2D
    mix(float mix, Vector2D v1, Vector2D v2)
    Mixes the values of two Vectors.
    plus(Vector2D other)
    Gets a new Vector which is the result of adding this Vector to a passed Vector.
    setLength(float l)
    Gets a Vector facing the same direction as this Vector with a given length.
    Formatted output of a Vector (e.g.: "(4.3, -1.2)")

    Methods inherited from class java.lang.Object

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

    • UP

      public static final Vector2D UP
      A constant Vector facing upwards: (0, -1)
    • DOWN

      public static final Vector2D DOWN
      A constant Vector facing downwards: (0, 1)
    • LEFT

      public static final Vector2D LEFT
      A constant Vector facing left: (-1, 0)
    • ZERO

      public static final Vector2D ZERO
      A constant zero Vector: (0, 0)
    • x

      public final float x
      X-value of the Vector.
    • y

      public final float y
      Y-value of the Vector.
  • Constructor Details

    • Vector2D

      public Vector2D(float x, float y)
      Constructs a new Vector from an x- and y-value.
      Parameters:
      x - x-value of the Vector
      y - y-value of the Vector
  • Method Details

    • mix

      public static Vector2D mix(float mix, Vector2D v1, Vector2D v2)
      Mixes the values of two Vectors. Result: (v1 * mix + v2 * (1-mix))
      Parameters:
      mix - percentage of the mix
      v1 - first Vector
      v2 - second Vector
      Returns:
      mixed Vector
    • toString

      public String toString()
      Formatted output of a Vector (e.g.: "(4.3, -1.2)")
      Overrides:
      toString in class Object
      Returns:
      formatted Vector
    • getNormalized

      public Vector2D getNormalized()
      Gets a normalized version of this Vector. If this Vectors x and y values are zero a Vector facing to the right is returned.
      Returns:
      Vector of length 1 facing the same direction
    • setLength

      public Vector2D setLength(float l)
      Gets a Vector facing the same direction as this Vector with a given length. Works by first normalizing the Vector and then scaling it.
      Parameters:
      l - length of the new Vector
      Returns:
      Vector with length l
    • getScaled

      public Vector2D getScaled(float scale)
      Gets this Vector scaled by a passed value.
      Parameters:
      scale - value to scale the Vector by
      Returns:
      scaled Vector
    • getLength

      public float getLength()
      Gets the length of this Vector using the Pythagoras-Theorem: sqrt(x * x + y * y)
      Returns:
      length of the Vector
    • getInverted

      public Vector2D getInverted()
      Gets an inverted version of this Vector: (-x, -y)
      Returns:
      inverted Vector
    • plus

      public Vector2D plus(Vector2D other)
      Gets a new Vector which is the result of adding this Vector to a passed Vector.
      Parameters:
      other - Vector to add
      Returns:
      sum of the Vectors
    • minus

      public Vector2D minus(Vector2D other)
      Gets a new Vector which is the result of subtracting this Vector with a passed Vector.
      Parameters:
      other - Vector to subtract
      Returns:
      difference between the Vectors