Package arc.math.geom

Class Vec2

java.lang.Object
arc.math.geom.Vec2
All Implemented Interfaces:
Position, Vector<Vec2>

public class Vec2 extends Object implements Vector<Vec2>, Position
Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
    the x-component of this vector
    static final Vec2
     
    float
    the y-component of this vector
    static final Vec2
     
    static final Vec2
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new vector at (0,0)
    Vec2(float x, float y)
    Constructs a vector with the given components
    Constructs a vector from the given vector
  • Method Summary

    Modifier and Type
    Method
    Description
    add(float x, float y)
    Adds the given components to this vector
     
    add(Vec2 v)
    Adds the given vector to this vector
    add(Vec2 vec, float scl)
     
    float
     
    float
    angle(Vec2 reference)
     
    float
     
    float
    angleRad(Vec2 reference)
     
    approach(Vec2 target, float alpha)
     
    approachDelta(Vec2 target, float alpha)
     
    clamp(float min, float max)
    Clamps this vector's length to given min and max values
    clamp(float minx, float miny, float maxx, float maxy)
     
    clampLength(float min, float max)
     
    cpy()
     
    float
    crs(float x, float y)
    Calculates the 2D cross product between this and the given vector.
    float
    crs(Vec2 v)
    Calculates the 2D cross product between this and the given vector.
    div(Vec2 other)
    Inverse of scl()
    float
    dot(float ox, float oy)
     
    float
    dot(Vec2 v)
     
    float
    dst(float x, float y)
     
    float
    dst(Vec2 v)
     
    float
    dst2(float x, float y)
     
    float
    This method is faster than Vector.dst(Vector) because it avoids calculating a square root.
    boolean
    epsilonEquals(float x, float y)
    Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
    boolean
    epsilonEquals(float x, float y, float epsilon)
    Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
    boolean
    Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
    boolean
    epsilonEquals(Vec2 other, float epsilon)
    Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
    boolean
     
    Sets this Vec2 to the value represented by the specified string according to the format of toString().
    float
     
    float
     
    int
     
    boolean
     
    boolean
     
    interpolate(Vec2 target, float alpha, Interp interpolation)
    Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method.
    inv()
     
    boolean
     
    boolean
    isCollinear(Vec2 other, float epsilon)
     
    boolean
     
    boolean
    isCollinearOpposite(Vec2 other, float epsilon)
     
    boolean
     
    boolean
     
    boolean
    isOnLine(Vec2 other)
     
    boolean
    isOnLine(Vec2 other, float epsilon)
     
    boolean
     
    boolean
    isPerpendicular(Vec2 vector, float epsilon)
     
    boolean
     
    boolean
    isUnit(float margin)
     
    boolean
     
    boolean
    isZero(float margin)
     
    float
    len()
     
    float
    This method is faster than Vector.len() because it avoids calculating a square root.
    lerp(float tx, float ty, float alpha)
     
    lerp(Position target, float alpha)
     
    lerp(Vec2 target, float alpha)
    Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
    lerpDelta(float tx, float ty, float alpha)
     
    lerpDelta(Position target, float alpha)
     
    lerpPast(Vec2 target, float alpha)
     
    limit(float limit)
    Limits the length of this vector, based on the desired maximum length.
    limit2(float limit2)
    Limits the length of this vector, based on the desired maximum length squared.
    mul(Mat mat)
    Left-multiplies this vector by the given matrix
    mulAdd(Vec2 vec, float scalar)
    First scale a supplied vector, then add it to this vector.
    mulAdd(Vec2 vec, Vec2 mulVec)
    First scale a supplied vector, then add it to this vector.
    nor()
    Normalizes this vector.
    rnd(float length)
    Sets this vector to a random direction with the specified length.
    rotate(float degrees)
    Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.
    rotate90(int dir)
    Rotates the Vec2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.
    rotateAround(Vec2 reference, float degrees)
    Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
    rotateAroundRad(Vec2 reference, float radians)
    Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
    rotateRad(float radians)
    Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.
    rotateRadExact(float radians)
     
    rotateTo(float angle, float speed)
     
    scl(float scalar)
    Scales this vector by a scalar
    scl(float x, float y)
    Multiplies this vector by a scalar
    scl(Vec2 v)
    Scales this vector by another vector
    set(float x, float y)
    Sets the components of this vector
     
    set(Vec2 v)
    Sets this vector from the given vector
    set(Vec3 other)
     
    setAngle(float degrees)
    Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
    setAngleRad(float radians)
    Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
    setLength(float len)
    Sets the length of this vector.
    setLength2(float len2)
    Sets the length of this vector, based on the square of the desired length.
    Sets this vector to the unit vector with a random direction
     
    Sets the components of this vector to 0
    Snaps this vector's coordinates to integers.
    sub(float x, float y)
    Substracts the other vector from this vector.
     
    sub(Vec2 v)
    Subtracts the given vector from this vector.
    sub(Vec3 v)
     
    Converts this Vec2 to a string in the format (x,y).
    trns(float angle, float amount)
     
    trns(float angle, float x, float y)
     
    trnsExact(float angle, float amount)
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface arc.math.geom.Position

    angleTo, angleTo, dst, dst2, within, within

    Methods inherited from interface arc.math.geom.Vector

    minus, plus, times, unaryMinus
  • Field Details

    • X

      public static final Vec2 X
    • Y

      public static final Vec2 Y
    • ZERO

      public static final Vec2 ZERO
    • x

      public float x
      the x-component of this vector
    • y

      public float y
      the y-component of this vector
  • Constructor Details

    • Vec2

      public Vec2()
      Constructs a new vector at (0,0)
    • Vec2

      public Vec2(float x, float y)
      Constructs a vector with the given components
      Parameters:
      x - The x-component
      y - The y-component
    • Vec2

      public Vec2(Vec2 v)
      Constructs a vector from the given vector
      Parameters:
      v - The vector
  • Method Details

    • trns

      public Vec2 trns(float angle, float amount)
    • trns

      public Vec2 trns(float angle, float x, float y)
    • trnsExact

      public Vec2 trnsExact(float angle, float amount)
    • snap

      public Vec2 snap()
      Snaps this vector's coordinates to integers.
    • div

      public Vec2 div(Vec2 other)
      Description copied from interface: Vector
      Inverse of scl()
      Specified by:
      div in interface Vector<Vec2>
    • cpy

      public Vec2 cpy()
      Specified by:
      cpy in interface Vector<Vec2>
      Returns:
      a copy of this vector
    • len

      public float len()
      Specified by:
      len in interface Vector<Vec2>
      Returns:
      The euclidean length
    • len2

      public float len2()
      Description copied from interface: Vector
      This method is faster than Vector.len() because it avoids calculating a square root. It is useful for comparisons, but not for getting exact lengths, as the return value is the square of the actual length.
      Specified by:
      len2 in interface Vector<Vec2>
      Returns:
      The squared euclidean length
    • set

      public Vec2 set(Vec2 v)
      Description copied from interface: Vector
      Sets this vector from the given vector
      Specified by:
      set in interface Vector<Vec2>
      Parameters:
      v - The vector
      Returns:
      This vector for chaining
    • set

      public Vec2 set(Position v)
    • set

      public Vec2 set(float x, float y)
      Sets the components of this vector
      Parameters:
      x - The x-component
      y - The y-component
      Returns:
      This vector for chaining
    • set

      public Vec2 set(Vec3 other)
    • sub

      public Vec2 sub(Position v)
    • sub

      public Vec2 sub(Vec2 v)
      Description copied from interface: Vector
      Subtracts the given vector from this vector.
      Specified by:
      sub in interface Vector<Vec2>
      Parameters:
      v - The vector
      Returns:
      This vector for chaining
    • sub

      public Vec2 sub(float x, float y)
      Substracts the other vector from this vector.
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      Returns:
      This vector for chaining
    • sub

      public Vec2 sub(Vec3 v)
    • nor

      public Vec2 nor()
      Description copied from interface: Vector
      Normalizes this vector. Does nothing if it is zero.
      Specified by:
      nor in interface Vector<Vec2>
      Returns:
      This vector for chaining
    • add

      public Vec2 add(Vec2 v)
      Description copied from interface: Vector
      Adds the given vector to this vector
      Specified by:
      add in interface Vector<Vec2>
      Parameters:
      v - The vector
      Returns:
      This vector for chaining
    • add

      public Vec2 add(Position pos)
    • add

      public Vec2 add(Vec2 vec, float scl)
    • add

      public Vec2 add(float x, float y)
      Adds the given components to this vector
      Parameters:
      x - The x-component
      y - The y-component
      Returns:
      This vector for chaining
    • dot

      public float dot(Vec2 v)
      Specified by:
      dot in interface Vector<Vec2>
      Parameters:
      v - The other vector
      Returns:
      The dot product between this and the other vector
    • dot

      public float dot(float ox, float oy)
    • scl

      public Vec2 scl(float scalar)
      Description copied from interface: Vector
      Scales this vector by a scalar
      Specified by:
      scl in interface Vector<Vec2>
      Parameters:
      scalar - The scalar
      Returns:
      This vector for chaining
    • inv

      public Vec2 inv()
    • scl

      public Vec2 scl(float x, float y)
      Multiplies this vector by a scalar
      Returns:
      This vector for chaining
    • scl

      public Vec2 scl(Vec2 v)
      Description copied from interface: Vector
      Scales this vector by another vector
      Specified by:
      scl in interface Vector<Vec2>
      Returns:
      This vector for chaining
    • mulAdd

      public Vec2 mulAdd(Vec2 vec, float scalar)
      Description copied from interface: Vector
      First scale a supplied vector, then add it to this vector.
      Specified by:
      mulAdd in interface Vector<Vec2>
      Parameters:
      vec - addition vector
      scalar - for scaling the addition vector
    • mulAdd

      public Vec2 mulAdd(Vec2 vec, Vec2 mulVec)
      Description copied from interface: Vector
      First scale a supplied vector, then add it to this vector.
      Specified by:
      mulAdd in interface Vector<Vec2>
      Parameters:
      vec - addition vector
      mulVec - vector by whose values the addition vector will be scaled
    • dst

      public float dst(Vec2 v)
      Specified by:
      dst in interface Vector<Vec2>
      Parameters:
      v - The other vector
      Returns:
      the distance between this and the other vector
    • dst

      public float dst(float x, float y)
      Specified by:
      dst in interface Position
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      Returns:
      the distance between this and the other vector
    • dst2

      public float dst2(Vec2 v)
      Description copied from interface: Vector
      This method is faster than Vector.dst(Vector) because it avoids calculating a square root. It is useful for comparisons, but not for getting accurate distances, as the return value is the square of the actual distance.
      Specified by:
      dst2 in interface Vector<Vec2>
      Parameters:
      v - The other vector
      Returns:
      the squared distance between this and the other vector
    • dst2

      public float dst2(float x, float y)
      Specified by:
      dst2 in interface Position
      Parameters:
      x - The x-component of the other vector
      y - The y-component of the other vector
      Returns:
      the squared distance between this and the other vector
    • clampLength

      public Vec2 clampLength(float min, float max)
    • limit

      public Vec2 limit(float limit)
      Description copied from interface: Vector
      Limits the length of this vector, based on the desired maximum length.
      Specified by:
      limit in interface Vector<Vec2>
      Parameters:
      limit - desired maximum length for this vector
      Returns:
      this vector for chaining
    • limit2

      public Vec2 limit2(float limit2)
      Description copied from interface: Vector
      Limits the length of this vector, based on the desired maximum length squared.

      This method is slightly faster than limit().

      Specified by:
      limit2 in interface Vector<Vec2>
      Parameters:
      limit2 - squared desired maximum length for this vector
      Returns:
      this vector for chaining
      See Also:
    • clamp

      public Vec2 clamp(float min, float max)
      Description copied from interface: Vector
      Clamps this vector's length to given min and max values
      Specified by:
      clamp in interface Vector<Vec2>
      Parameters:
      min - Min length
      max - Max length
      Returns:
      This vector for chaining
    • setLength

      public Vec2 setLength(float len)
      Description copied from interface: Vector
      Sets the length of this vector. Does nothing if this vector is zero.
      Specified by:
      setLength in interface Vector<Vec2>
      Parameters:
      len - desired length for this vector
      Returns:
      this vector for chaining
    • setLength2

      public Vec2 setLength2(float len2)
      Description copied from interface: Vector
      Sets the length of this vector, based on the square of the desired length. Does nothing if this vector is zero.

      This method is slightly faster than setLength().

      Specified by:
      setLength2 in interface Vector<Vec2>
      Parameters:
      len2 - desired square of the length for this vector
      Returns:
      this vector for chaining
      See Also:
    • toString

      public String toString()
      Converts this Vec2 to a string in the format (x,y).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object.
    • clamp

      public Vec2 clamp(float minx, float miny, float maxx, float maxy)
    • tryFromString

      public Vec2 tryFromString(String v)
    • fromString

      public Vec2 fromString(String v)
      Sets this Vec2 to the value represented by the specified string according to the format of toString().
      Parameters:
      v - the string.
      Returns:
      this vector for chaining
    • mul

      public Vec2 mul(Mat mat)
      Left-multiplies this vector by the given matrix
      Parameters:
      mat - the matrix
      Returns:
      this vector
    • crs

      public float crs(Vec2 v)
      Calculates the 2D cross product between this and the given vector.
      Parameters:
      v - the other vector
      Returns:
      the cross product
    • crs

      public float crs(float x, float y)
      Calculates the 2D cross product between this and the given vector.
      Parameters:
      x - the x-coordinate of the other vector
      y - the y-coordinate of the other vector
      Returns:
      the cross product
    • angle

      public float angle()
      Returns:
      the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis (typically counter-clockwise) and between 0 and 360.
    • angle

      public float angle(Vec2 reference)
      Returns:
      the angle in degrees of this vector (point) relative to the given vector. Angles are towards the positive y-axis (typically counter-clockwise.) between -180 and +180
    • rnd

      public Vec2 rnd(float length)
      Sets this vector to a random direction with the specified length.
    • angleRad

      public float angleRad()
      Returns:
      the angle in radians of this vector (point) relative to the x-axis. Angles are towards the positive y-axis. (typically counter-clockwise)
    • angleRad

      public float angleRad(Vec2 reference)
      Returns:
      the angle in radians of this vector (point) relative to the given vector. Angles are towards the positive y-axis. (typically counter-clockwise.)
    • setAngle

      public Vec2 setAngle(float degrees)
      Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
      Parameters:
      degrees - The angle in degrees to set.
    • setAngleRad

      public Vec2 setAngleRad(float radians)
      Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
      Parameters:
      radians - The angle in radians to set.
    • rotateTo

      public Vec2 rotateTo(float angle, float speed)
    • rotate

      public Vec2 rotate(float degrees)
      Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.
      Parameters:
      degrees - the angle in degrees
    • rotateAround

      public Vec2 rotateAround(Vec2 reference, float degrees)
      Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
      Parameters:
      degrees - the angle in degrees
      reference - center Vec2
    • rotateRad

      public Vec2 rotateRad(float radians)
      Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.
      Parameters:
      radians - the angle in radians
    • rotateRadExact

      public Vec2 rotateRadExact(float radians)
    • rotateAroundRad

      public Vec2 rotateAroundRad(Vec2 reference, float radians)
      Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
      Parameters:
      radians - the angle in radians
      reference - center Vec2
    • rotate90

      public Vec2 rotate90(int dir)
      Rotates the Vec2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.
    • approachDelta

      public Vec2 approachDelta(Vec2 target, float alpha)
    • approach

      public Vec2 approach(Vec2 target, float alpha)
    • lerpPast

      public Vec2 lerpPast(Vec2 target, float alpha)
    • lerpDelta

      public Vec2 lerpDelta(float tx, float ty, float alpha)
    • lerpDelta

      public Vec2 lerpDelta(Position target, float alpha)
    • lerp

      public Vec2 lerp(Position target, float alpha)
    • lerp

      public Vec2 lerp(float tx, float ty, float alpha)
    • lerp

      public Vec2 lerp(Vec2 target, float alpha)
      Description copied from interface: Vector
      Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
      Specified by:
      lerp in interface Vector<Vec2>
      Parameters:
      target - The target vector
      alpha - The interpolation coefficient
      Returns:
      This vector for chaining.
    • interpolate

      public Vec2 interpolate(Vec2 target, float alpha, Interp interpolation)
      Description copied from interface: Vector
      Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method. the result is stored in this vector.
      Specified by:
      interpolate in interface Vector<Vec2>
      Parameters:
      target - The target vector
      alpha - The interpolation coefficient
      interpolation - An Interpolation object describing the used interpolation method
      Returns:
      This vector for chaining.
    • setToRandomDirection

      public Vec2 setToRandomDirection()
      Description copied from interface: Vector
      Sets this vector to the unit vector with a random direction
      Specified by:
      setToRandomDirection in interface Vector<Vec2>
      Returns:
      This vector for chaining
    • setToRandomDirection

      public Vec2 setToRandomDirection(Rand rand)
    • hashCode

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

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

      public boolean epsilonEquals(Vec2 other, float epsilon)
      Description copied from interface: Vector
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      Specified by:
      epsilonEquals in interface Vector<Vec2>
      Returns:
      whether the vectors have fuzzy equality.
    • epsilonEquals

      public boolean epsilonEquals(float x, float y, float epsilon)
      Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
      Returns:
      whether the vectors are the same.
    • epsilonEquals

      public boolean epsilonEquals(Vec2 other)
      Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      Parameters:
      other - other vector to compare
      Returns:
      true if vector are equal, otherwise false
    • epsilonEquals

      public boolean epsilonEquals(float x, float y)
      Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testing
      Parameters:
      x - x component of the other vector to compare
      y - y component of the other vector to compare
      Returns:
      true if vector are equal, otherwise false
    • isNaN

      public boolean isNaN()
    • isInfinite

      public boolean isInfinite()
    • isUnit

      public boolean isUnit()
      Specified by:
      isUnit in interface Vector<Vec2>
      Returns:
      Whether this vector is a unit length vector
    • isUnit

      public boolean isUnit(float margin)
      Specified by:
      isUnit in interface Vector<Vec2>
      Returns:
      Whether this vector is a unit length vector within the given margin.
    • isZero

      public boolean isZero()
      Specified by:
      isZero in interface Vector<Vec2>
      Returns:
      Whether this vector is a zero vector
    • isZero

      public boolean isZero(float margin)
      Specified by:
      isZero in interface Vector<Vec2>
      Returns:
      Whether the length of this vector is smaller than the given margin
    • isOnLine

      public boolean isOnLine(Vec2 other)
      Specified by:
      isOnLine in interface Vector<Vec2>
      Returns:
      true if this vector is in line with the other vector (either in the same or the opposite direction)
    • isOnLine

      public boolean isOnLine(Vec2 other, float epsilon)
      Specified by:
      isOnLine in interface Vector<Vec2>
      Returns:
      true if this vector is in line with the other vector (either in the same or the opposite direction)
    • isCollinear

      public boolean isCollinear(Vec2 other, float epsilon)
      Specified by:
      isCollinear in interface Vector<Vec2>
      Returns:
      true if this vector is collinear with the other vector (Vector.isOnLine(Vector, float) && Vector.hasSameDirection(Vector)).
    • isCollinear

      public boolean isCollinear(Vec2 other)
      Specified by:
      isCollinear in interface Vector<Vec2>
      Returns:
      true if this vector is collinear with the other vector (Vector.isOnLine(Vector) && Vector.hasSameDirection(Vector)).
    • isCollinearOpposite

      public boolean isCollinearOpposite(Vec2 other, float epsilon)
      Specified by:
      isCollinearOpposite in interface Vector<Vec2>
      Returns:
      true if this vector is opposite collinear with the other vector (Vector.isOnLine(Vector, float) && Vector.hasOppositeDirection(Vector)).
    • isCollinearOpposite

      public boolean isCollinearOpposite(Vec2 other)
      Specified by:
      isCollinearOpposite in interface Vector<Vec2>
      Returns:
      true if this vector is opposite collinear with the other vector (Vector.isOnLine(Vector) && Vector.hasOppositeDirection(Vector)).
    • isPerpendicular

      public boolean isPerpendicular(Vec2 vector)
      Specified by:
      isPerpendicular in interface Vector<Vec2>
      Returns:
      Whether this vector is perpendicular with the other vector. True if the dot product is 0.
    • isPerpendicular

      public boolean isPerpendicular(Vec2 vector, float epsilon)
      Specified by:
      isPerpendicular in interface Vector<Vec2>
      epsilon - a positive small number close to zero
      Returns:
      Whether this vector is perpendicular with the other vector. True if the dot product is 0.
    • hasSameDirection

      public boolean hasSameDirection(Vec2 vector)
      Specified by:
      hasSameDirection in interface Vector<Vec2>
      Returns:
      Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
    • hasOppositeDirection

      public boolean hasOppositeDirection(Vec2 vector)
      Specified by:
      hasOppositeDirection in interface Vector<Vec2>
      Returns:
      Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
    • setZero

      public Vec2 setZero()
      Description copied from interface: Vector
      Sets the components of this vector to 0
      Specified by:
      setZero in interface Vector<Vec2>
      Returns:
      This vector for chaining
    • getX

      public float getX()
      Specified by:
      getX in interface Position
    • getY

      public float getY()
      Specified by:
      getY in interface Position