Package gaiasky.util.math
Class Vector2d
- java.lang.Object
-
- gaiasky.util.math.Vector2d
-
public class Vector2d extends java.lang.Object implements java.io.Serializable, Vectord<Vector2d>
Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector2dadd(double x, double y)Adds the given components to this vectorVector2dadd(Vector2d v)Adds the given vector to this vectordoubleangle()Vector2dclamp(double min, double max)Clamps this vector's length to given min and max valuesVector2dcpy()doublecrs(double x, double y)Calculates the 2D cross product between this and the given vector.doublecrs(Vector2d v)Calculates the 2D cross product between this and the given vector.doubledot(double ox, double oy)static doubledot(double x1, double y1, double x2, double y2)doubledot(Vector2d v)doubledst(double x, double y)static doubledst(double x1, double y1, double x2, double y2)doubledst(Vector2d v)doubledst2(double x, double y)static doubledst2(double x1, double y1, double x2, double y2)doubledst2(Vector2d v)This method is faster thanVectord.dst(Vectord)because it avoids calculating a square root.booleanepsilonEquals(double x, double y, double epsilon)Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.booleanepsilonEquals(Vector2d other, double epsilon)Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.doublegetAngleRad()booleanhasOppositeDirection(Vector2d vector)booleanhasSameDirection(Vector2d vector)Vector2dinterpolate(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.booleanisCollinear(Vector2d other)booleanisCollinear(Vector2d other, double epsilon)booleanisCollinearOpposite(Vector2d other)booleanisCollinearOpposite(Vector2d other, double epsilon)booleanisOnLine(Vector2d other)booleanisOnLine(Vector2d other, double epsilon)booleanisPerpendicular(Vector2d vector)booleanisPerpendicular(Vector2d vector, double epsilon)booleanisUnit()booleanisUnit(double margin)booleanisZero()booleanisZero(double margin)doublelen()static doublelen(double x, double y)doublelen2()This method is faster thanVectord.len()because it avoids calculating a square root.static doublelen2(double x, double y)Vector2dlerp(Vector2d target, double alpha)Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].Vector2dlimit(double limit)Limits the length of this vector, based on the desired maximum length.Vector2dlimit2(double limit2)Limits the length of this vector, based on the desired maximum length squared.Vector2dmul(com.badlogic.gdx.math.Matrix3 mat)Left-multiplies this vector by the given matrixVector2dmulAdd(Vector2d vec, double scalar)First scale a supplied vector, then add it to this vector.Vector2dmulAdd(Vector2d vec, Vector2d mulVec)First scale a supplied vector, then add it to this vector.Vector2dnor()Normalizes this vector.Vector2drotate(double degrees)Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.Vector2drotate90(int dir)Rotates the Vector2d by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise.Vector2drotateRad(double radians)Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.Vector2dscl(double scalar)Scales this vector by a scalarVector2dscl(double x, double y)Multiplies this vector by a scalarVector2dscl(Vector2d v)Scales this vector by another vectorVector2dset(double[] vals)Vector2dset(double x, double y)Sets the components of this vectorVector2dset(Vector2d v)Sets this vector from the given vectorVector2dsetAngle(double degrees)Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).Vector2dsetAngleRad(double radians)Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).Vector2dsetLength(double len)Sets the length of this vector.Vector2dsetLength2(double len2)Sets the length of this vector, based on the square of the desired length.Vector2dsetToRandomDirection()Sets this vector to the unit vector with a random directionVector2dsetZero()Sets the components of this vector to 0Vector2dsub(double x, double y)Substracts the other vector from this vector.Vector2dsub(Vector2d v)Subtracts the given vector from this vector.java.lang.StringtoString()double[]values()doublex()doubley()
-
-
-
Constructor Detail
-
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-componenty- The y-component
-
Vector2d
public Vector2d(Vector2d v)
Constructs a vector from the given vector- Parameters:
v- The vector
-
-
Method Detail
-
x
public double x()
-
y
public double y()
-
cpy
public Vector2d cpy()
-
len
public static double len(double x, double y)
-
len
public double len()
-
len2
public static double len2(double x, double y)
-
len2
public double len2()
Description copied from interface:VectordThis method is faster thanVectord.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.
-
set
public Vector2d set(Vector2d v)
Description copied from interface:VectordSets this vector from the given vector
-
set
public Vector2d set(double[] vals)
-
set
public Vector2d set(double x, double y)
Sets the components of this vector- Parameters:
x- The x-componenty- The y-component- Returns:
- This vector for chaining
-
sub
public Vector2d sub(Vector2d v)
Description copied from interface:VectordSubtracts the given vector from this vector.
-
sub
public Vector2d sub(double x, double y)
Substracts the other vector from this vector.- Parameters:
x- The x-component of the other vectory- The y-component of the other vector- Returns:
- This vector for chaining
-
nor
public Vector2d nor()
Description copied from interface:VectordNormalizes this vector. Does nothing if it is zero.
-
add
public Vector2d add(Vector2d v)
Description copied from interface:VectordAdds the given vector to this vector
-
add
public Vector2d add(double x, double y)
Adds the given components to this vector- Parameters:
x- The x-componenty- 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)
-
dot
public double dot(double ox, double oy)
-
scl
public Vector2d scl(double scalar)
Description copied from interface:VectordScales this vector by a scalar
-
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:VectordScales this vector by another vector
-
mulAdd
public Vector2d mulAdd(Vector2d vec, double scalar)
Description copied from interface:VectordFirst scale a supplied vector, then add it to this vector.
-
mulAdd
public Vector2d mulAdd(Vector2d vec, Vector2d mulVec)
Description copied from interface:VectordFirst scale a supplied vector, then add it to this vector.
-
setZero
public Vector2d setZero()
Description copied from interface:VectordSets the components of this vector to 0
-
dst
public static double dst(double x1, double y1, double x2, double y2)
-
dst
public double dst(Vector2d v)
-
dst
public double dst(double x, double y)- Parameters:
x- The x-component of the other vectory- 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:VectordThis method is faster thanVectord.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.
-
dst2
public double dst2(double x, double y)- Parameters:
x- The x-component of the other vectory- 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:VectordLimits the length of this vector, based on the desired maximum length.
-
limit2
public Vector2d limit2(double limit2)
Description copied from interface:VectordLimits the length of this vector, based on the desired maximum length squared. This method is slightly faster than limit().- Specified by:
limit2in interfaceVectord<Vector2d>- Parameters:
limit2- squared desired maximum length for this vector- Returns:
- this vector for chaining
- See Also:
Vectord.len2()
-
setLength
public Vector2d setLength(double len)
Description copied from interface:VectordSets the length of this vector. Does nothing is this vector is zero.
-
setLength2
public Vector2d setLength2(double len2)
Description copied from interface:VectordSets 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:
setLength2in interfaceVectord<Vector2d>- Parameters:
len2- desired square of the length for this vector- Returns:
- this vector for chaining
- See Also:
Vectord.len2()
-
clamp
public Vector2d clamp(double min, double max)
Description copied from interface:VectordClamps this vector's length to given min and max values
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
mul
public Vector2d mul(com.badlogic.gdx.math.Matrix3 mat)
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 vectory- 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.
-
getAngleRad
public double getAngleRad()
- 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.
-
setAngleRad
public 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).- 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
-
rotateRad
public Vector2d rotateRad(double radians)
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 < 0 is clockwise.
-
lerp
public Vector2d lerp(Vector2d target, double alpha)
Description copied from interface:VectordLinearly interpolates between this vector and the target vector by alpha which is in the range [0,1]. The result is stored in this vector.
-
interpolate
public Vector2d interpolate(Vector2d target, double alpha, Interpolationd interpolation)
Description copied from interface:VectordInterpolates 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:
interpolatein interfaceVectord<Vector2d>- Parameters:
target- The target vectoralpha- The interpolation coefficientinterpolation- An Interpolation object describing the used interpolation method- Returns:
- This vector for chaining.
-
setToRandomDirection
public Vector2d setToRandomDirection()
Description copied from interface:VectordSets this vector to the unit vector with a random direction- Specified by:
setToRandomDirectionin interfaceVectord<Vector2d>- Returns:
- This vector for chaining
-
epsilonEquals
public boolean epsilonEquals(Vector2d other, double epsilon)
Description copied from interface:VectordCompares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.- Specified by:
epsilonEqualsin interfaceVectord<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()
-
isUnit
public boolean isUnit(double margin)
-
isZero
public boolean isZero()
-
isZero
public boolean isZero(double margin)
-
isOnLine
public boolean isOnLine(Vector2d other)
-
isOnLine
public boolean isOnLine(Vector2d other, double epsilon)
-
isCollinear
public boolean isCollinear(Vector2d other, double epsilon)
- Specified by:
isCollinearin interfaceVectord<Vector2d>- Returns:
- true if this vector is collinear with the other vector (
Vectord.isOnLine(Vectord, double)&&Vectord.hasSameDirection(Vectord)).
-
isCollinear
public boolean isCollinear(Vector2d other)
- Specified by:
isCollinearin interfaceVectord<Vector2d>- Returns:
- true if this vector is collinear with the other vector (
Vectord.isOnLine(Vectord)&&Vectord.hasSameDirection(Vectord)).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector2d other, double epsilon)
- Specified by:
isCollinearOppositein interfaceVectord<Vector2d>- Returns:
- true if this vector is opposite collinear with the other vector (
Vectord.isOnLine(Vectord, double)&&Vectord.hasOppositeDirection(Vectord)).
-
isCollinearOpposite
public boolean isCollinearOpposite(Vector2d other)
- Specified by:
isCollinearOppositein interfaceVectord<Vector2d>- Returns:
- true if this vector is opposite collinear with the other vector (
Vectord.isOnLine(Vectord)&&Vectord.hasOppositeDirection(Vectord)).
-
isPerpendicular
public boolean isPerpendicular(Vector2d vector)
- Specified by:
isPerpendicularin interfaceVectord<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:
isPerpendicularin interfaceVectord<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:
hasSameDirectionin interfaceVectord<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:
hasOppositeDirectionin interfaceVectord<Vector2d>- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
-