# Class Vector2d

java.lang.Object
All Implemented Interfaces:
`Vectord<Vector2d>`, `Serializable`

public class Vector2d extends Object implements Serializable, Vectord<Vector2d>
Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
• ## Field Summary

Fields
Modifier and Type
Field
Description
`double`
`x`
the x-component of this vector
`static final Vector2d`
`X`

`double`
`y`
the y-component of this vector
`static final Vector2d`
`Y`

`static final Vector2d`
`Zero`

• ## Constructor Summary

Constructors
Constructor
Description
`Vector2d()`
Constructs a new vector at (0,0)
```Vector2d(double x, double y)```
Constructs a vector with the given components
`Vector2d(Vector2d v)`
Constructs a vector from the given vector
• ## Method Summary

Modifier and Type
Method
Description
`Vector2d`
```add(double x, double y)```
Adds the given components to this vector
`Vector2d`
`add(Vector2d v)`
Adds the given vector to this vector
`double`
`angle()`

`Vector2d`
```clamp(double min, double max)```
Clamps this vector's length to given min and max values
`Vector2d`
`cpy()`

`double`
```crs(double x, double y)```
Calculates the 2D cross product between this and the given vector.
`double`
`crs(Vector2d v)`
Calculates the 2D cross product between this and the given vector.
`double`
```dot(double ox, double oy)```

`static double`
```dot(double x1, double y1, double x2, double y2)```

`double`
`dot(Vector2d v)`

`double`
```dst(double x, double y)```

`static double`
```dst(double x1, double y1, double x2, double y2)```

`double`
`dst(Vector2d v)`

`double`
```dst2(double x, double y)```

`static double`
```dst2(double x1, double y1, double x2, double y2)```

`double`
`dst2(Vector2d v)`
This method is faster than `Vectord.dst(Vectord)` because it avoids calculating a square root.
`boolean`
```epsilonEquals(double x, double y, double epsilon)```
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
`boolean`
```epsilonEquals(Vector2d other, double epsilon)```
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
`double`
`getAngleRad()`

`boolean`
`hasOppositeDirection(Vector2d vector)`

`boolean`
`hasSameDirection(Vector2d vector)`

`Vector2d`
```interpolate(Vector2d target, double alpha, Interpolationd interpolation)```
Interpolates between this vector and the given target vector by alpha (within range [0,1]) using the given Interpolation method.
`boolean`
`isCollinear(Vector2d other)`

`boolean`
```isCollinear(Vector2d other, double epsilon)```

`boolean`
`isCollinearOpposite(Vector2d other)`

`boolean`
```isCollinearOpposite(Vector2d other, double epsilon)```

`boolean`
`isOnLine(Vector2d other)`

`boolean`
```isOnLine(Vector2d other, double epsilon)```

`boolean`
`isPerpendicular(Vector2d vector)`

`boolean`
```isPerpendicular(Vector2d vector, double epsilon)```

`boolean`
`isUnit()`

`boolean`
`isUnit(double margin)`

`boolean`
`isZero()`

`boolean`
`isZero(double margin)`

`double`
`len()`

`static double`
```len(double x, double y)```

`double`
`len2()`
This method is faster than `Vectord.len()` because it avoids calculating a square root.
`static double`
```len2(double x, double y)```

`Vector2d`
```lerp(Vector2d target, double alpha)```
Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].
`Vector2d`
`limit(double limit)`
Limits the length of this vector, based on the desired maximum length.
`Vector2d`
`limit2(double limit2)`
Limits the length of this vector, based on the desired maximum length squared.
`Vector2d`
`mul(com.badlogic.gdx.math.Matrix3 mat)`
Left-multiplies this vector by the given matrix
`Vector2d`
```mulAdd(Vector2d vec, double scalar)```
First scale a supplied vector, then add it to this vector.
`Vector2d`
```mulAdd(Vector2d vec, Vector2d mulVec)```
First scale a supplied vector, then add it to this vector.
`Vector2d`
`nor()`
Normalizes this vector.
`Vector2d`
`rotate(double degrees)`
Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.
`Vector2d`
`rotate90(int dir)`
Rotates the Vector2d by 90 degrees in the specified direction, where >= 0 is counter-clockwise and invalid input: '<' 0 is clockwise.
`Vector2d`
`rotateRad(double radians)`
Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.
`Vector2d`
`scl(double scalar)`
Scales this vector by a scalar
`Vector2d`
```scl(double x, double y)```
Multiplies this vector by a scalar
`Vector2d`
`scl(Vector2d v)`
Scales this vector by another vector
`Vector2d`
`set(double[] vals)`

`Vector2d`
```set(double x, double y)```
Sets the components of this vector
`Vector2d`
`set(Vector2d v)`
Sets this vector from the given vector
`Vector2d`
`setAngle(double degrees)`
Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
`Vector2d`
`setAngleRad(double radians)`
Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
`Vector2d`
`setLength(double len)`
Sets the length of this vector.
`Vector2d`
`setLength2(double len2)`
Sets the length of this vector, based on the square of the desired length.
`Vector2d`
`setToRandomDirection()`
Sets this vector to the unit vector with a random direction
`Vector2d`
`setZero()`
Sets the components of this vector to 0
`Vector2d`
```sub(double x, double y)```
Substracts the other vector from this vector.
`Vector2d`
`sub(Vector2d v)`
Subtracts the given vector from this vector.
`String`
`toString()`

`double[]`
`values()`

`double`
`x()`

`double`
`y()`

### Methods inherited from class java.lang.Object

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

• ### X

public static final Vector2d X
• ### Y

public static final Vector2d Y
• ### Zero

public static final Vector2d Zero
• ### x

public double x
the x-component of this vector
• ### y

public double y
the y-component of this vector
• ## Constructor Details

• ### Vector2d

public Vector2d()
Constructs a new vector at (0,0)
• ### Vector2d

public Vector2d(double x, double y)
Constructs a vector with the given components
Parameters:
`x` - The x-component
`y` - The y-component
• ### Vector2d

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

• ### x

public double x()
• ### y

public double y()
• ### cpy

public Vector2d cpy()
Specified by:
`cpy` in interface `Vectord<Vector2d>`
Returns:
a copy of this vector
• ### len

public static double len(double x, double y)
• ### len

public double len()
Specified by:
`len` in interface `Vectord<Vector2d>`
Returns:
The euclidean length
• ### len2

public static double len2(double x, double y)
• ### len2

public double len2()
Description copied from interface: `Vectord`
This method is faster than `Vectord.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 `Vectord<Vector2d>`
Returns:
The squared euclidean length
• ### set

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

public Vector2d set(double[] vals)
• ### set

public Vector2d set(double x, double y)
Sets the components of this vector
Parameters:
`x` - The x-component
`y` - The y-component
Returns:
This vector for chaining
• ### sub

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

public Vector2d sub(double x, double 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
• ### nor

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

Description copied from interface: `Vectord`
Adds the given vector to this vector
Specified by:
`add` in interface `Vectord<Vector2d>`
Parameters:
`v` - The vector
Returns:
This vector for chaining

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

public static double dot(double x1, double y1, double x2, double y2)
• ### dot

public double dot(Vector2d v)
Specified by:
`dot` in interface `Vectord<Vector2d>`
Parameters:
`v` - The other vector
Returns:
The dot product between this and the other vector
• ### dot

public double dot(double ox, double oy)
• ### scl

public Vector2d scl(double scalar)
Description copied from interface: `Vectord`
Scales this vector by a scalar
Specified by:
`scl` in interface `Vectord<Vector2d>`
Parameters:
`scalar` - The scalar
Returns:
This vector for chaining
• ### scl

public Vector2d scl(double x, double y)
Multiplies this vector by a scalar
Returns:
This vector for chaining
• ### scl

public Vector2d scl(Vector2d v)
Description copied from interface: `Vectord`
Scales this vector by another vector
Specified by:
`scl` in interface `Vectord<Vector2d>`
Returns:
This vector for chaining

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

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

public Vector2d setZero()
Description copied from interface: `Vectord`
Sets the components of this vector to 0
Specified by:
`setZero` in interface `Vectord<Vector2d>`
Returns:
This vector for chaining
• ### dst

public static double dst(double x1, double y1, double x2, double y2)
• ### dst

public double dst(Vector2d v)
Specified by:
`dst` in interface `Vectord<Vector2d>`
Parameters:
`v` - The other vector
Returns:
the distance between this and the other vector
• ### dst

public double dst(double x, double y)
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 static double dst2(double x1, double y1, double x2, double y2)
• ### dst2

public double dst2(Vector2d v)
Description copied from interface: `Vectord`
This method is faster than `Vectord.dst(Vectord)` 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 `Vectord<Vector2d>`
Parameters:
`v` - The other vector
Returns:
the squared distance between this and the other vector
• ### dst2

public double dst2(double x, double y)
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
• ### limit

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

public Vector2d limit2(double limit2)
Description copied from interface: `Vectord`
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 `Vectord<Vector2d>`
Parameters:
`limit2` - squared desired maximum length for this vector
Returns:
this vector for chaining
• ### setLength

public Vector2d setLength(double len)
Description copied from interface: `Vectord`
Sets the length of this vector. Does nothing is this vector is zero.
Specified by:
`setLength` in interface `Vectord<Vector2d>`
Parameters:
`len` - desired length for this vector
Returns:
this vector for chaining
• ### setLength2

public Vector2d setLength2(double len2)
Description copied from interface: `Vectord`
Sets the length of this vector, based on the square of the desired length. Does nothing is this vector is zero.

This method is slightly faster than setLength().

Specified by:
`setLength2` in interface `Vectord<Vector2d>`
Parameters:
`len2` - desired square of the length for this vector
Returns:
this vector for chaining
• ### clamp

public Vector2d clamp(double min, double max)
Description copied from interface: `Vectord`
Clamps this vector's length to given min and max values
Specified by:
`clamp` in interface `Vectord<Vector2d>`
Parameters:
`min` - Min length
`max` - Max length
Returns:
This vector for chaining
• ### toString

public String toString()
Overrides:
`toString` in class `Object`
• ### mul

Left-multiplies this vector by the given matrix
Parameters:
`mat` - the matrix
Returns:
this vector
• ### crs

public double crs(Vector2d v)
Calculates the 2D cross product between this and the given vector.
Parameters:
`v` - the other vector
Returns:
the cross product
• ### crs

public double crs(double x, double 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 double 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.

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

public Vector2d setAngle(double 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.

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.
• ### rotate

public Vector2d rotate(double degrees)
Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.
Parameters:
`degrees` - the angle in degrees

Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.
Parameters:
`radians` - the angle in radians
• ### values

public double[] values()
• ### rotate90

public Vector2d rotate90(int dir)
Rotates the Vector2d by 90 degrees in the specified direction, where >= 0 is counter-clockwise and invalid input: '<' 0 is clockwise.
• ### lerp

public Vector2d lerp(Vector2d target, double alpha)
Description copied from interface: `Vectord`
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 `Vectord<Vector2d>`
Parameters:
`target` - The target vector
`alpha` - The interpolation coefficient
Returns:
This vector for chaining.
• ### interpolate

public Vector2d interpolate(Vector2d target, double alpha, Interpolationd interpolation)
Description copied from interface: `Vectord`
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 `Vectord<Vector2d>`
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 Vector2d setToRandomDirection()
Description copied from interface: `Vectord`
Sets this vector to the unit vector with a random direction
Specified by:
`setToRandomDirection` in interface `Vectord<Vector2d>`
Returns:
This vector for chaining
• ### epsilonEquals

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

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

public boolean isUnit()
Specified by:
`isUnit` in interface `Vectord<Vector2d>`
Returns:
Whether this vector is a unit length vector
• ### isUnit

public boolean isUnit(double margin)
Specified by:
`isUnit` in interface `Vectord<Vector2d>`
Returns:
Whether this vector is a unit length vector within the given margin.
• ### isZero

public boolean isZero()
Specified by:
`isZero` in interface `Vectord<Vector2d>`
Returns:
Whether this vector is a zero vector
• ### isZero

public boolean isZero(double margin)
Specified by:
`isZero` in interface `Vectord<Vector2d>`
Returns:
Whether the length of this vector is smaller than the given margin
• ### isOnLine

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

public boolean isOnLine(Vector2d other, double epsilon)
Specified by:
`isOnLine` in interface `Vectord<Vector2d>`
Returns:
true if this vector is in line with the other vector (either in the same or the opposite direction)
• ### isCollinear

public boolean isCollinear(Vector2d other, double epsilon)
Specified by:
`isCollinear` in interface `Vectord<Vector2d>`
Returns:
true if this vector is collinear with the other vector (`Vectord.isOnLine(Vectord, double)` invalid input: '&'invalid input: '&' `Vectord.hasSameDirection(Vectord)`).
• ### isCollinear

public boolean isCollinear(Vector2d other)
Specified by:
`isCollinear` in interface `Vectord<Vector2d>`
Returns:
true if this vector is collinear with the other vector (`Vectord.isOnLine(Vectord)` invalid input: '&'invalid input: '&' `Vectord.hasSameDirection(Vectord)`).
• ### isCollinearOpposite

public boolean isCollinearOpposite(Vector2d other, double epsilon)
Specified by:
`isCollinearOpposite` in interface `Vectord<Vector2d>`
Returns:
true if this vector is opposite collinear with the other vector (`Vectord.isOnLine(Vectord, double)` invalid input: '&'invalid input: '&' `Vectord.hasOppositeDirection(Vectord)`).
• ### isCollinearOpposite

public boolean isCollinearOpposite(Vector2d other)
Specified by:
`isCollinearOpposite` in interface `Vectord<Vector2d>`
Returns:
true if this vector is opposite collinear with the other vector (`Vectord.isOnLine(Vectord)` invalid input: '&'invalid input: '&' `Vectord.hasOppositeDirection(Vectord)`).
• ### isPerpendicular

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

public boolean isPerpendicular(Vector2d vector, double epsilon)
Specified by:
`isPerpendicular` in interface `Vectord<Vector2d>`
`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(Vector2d vector)
Specified by:
`hasSameDirection` in interface `Vectord<Vector2d>`
Returns:
Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
• ### hasOppositeDirection

public boolean hasOppositeDirection(Vector2d vector)
Specified by:
`hasOppositeDirection` in interface `Vectord<Vector2d>`
Returns:
Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is invalid input: '<' 0.