Class Vector2D
java.lang.Object
gaiasky.util.math.Vector2D
- All Implemented Interfaces:
VectorDouble<Vector2D>
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(double x, double y) Adds the given components to this vectorAdds the given vector to this vectordoubleangle()clamp(double min, double max) Clamps this vector's length to given min and max valuescpy()doublecrs(double x, double y) Calculates the 2D cross product between this and the given vector.doubleCalculates 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) doubledoubledst(double x, double y) static doubledst(double x1, double y1, double x2, double y2) doubledoubledst2(double x, double y) static doubledst2(double x1, double y1, double x2, double y2) doubleThis method is faster thanVectorDouble.dst(VectorDouble)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.doublebooleanhasOppositeDirection(Vector2D vector) booleanhasSameDirection(Vector2D vector) interpolate(Vector2D target, double alpha, InterpolationDouble 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) booleanbooleanbooleanisPerpendicular(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 thanVectorDouble.len()because it avoids calculating a square root.static doublelen2(double x, double y) Linearly interpolates between this vector and the target vector by alpha which is in the range [0,1].limit(double limit) Limits the length of this vector, based on the desired maximum length.limit2(double limit2) Limits the length of this vector, based on the desired maximum length squared.mul(com.badlogic.gdx.math.Matrix3 mat) Left-multiplies this vector by the given matrixFirst scale a supplied vector, then add it to this vector.First scale a supplied vector, then add it to this vector.nor()Normalizes this vector.rotate(double degrees) Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.rotate90(int dir) Rotates theVector2Dby 90 degrees in the specified direction, where ≥0 is counter-clockwise and <0 is clockwise.rotateRad(double radians) Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.scl(double scalar) Scales this vector by a scalarscl(double x, double y) Multiplies this vector by a scalarScales this vector by another vectorset(double[] vals) set(double x, double y) Sets the components of this vectorSets this vector from the given vectorsetAngle(double degrees) Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).setAngleRad(double radians) Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).setLength(double len) Sets the length of this vector.setLength2(double 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 directionsetZero()Sets the components of this vector to 0.sub(double x, double y) Substracts the other vector from this vector.Subtracts the given vector from this vector.toString()double[]values()doublex()doubley()
-
Field Details
-
X
-
Y
-
Zero
-
x
public double xthe x-component of this vector -
y
public double ythe 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-componenty- The y-component
-
Vector2D
-
-
Method Details
-
len
public static double len(double x, double y) -
len2
public static double len2(double x, double y) -
dot
public static double dot(double x1, double y1, double x2, double y2) -
dst
public static double dst(double x1, double y1, double x2, double y2) -
dst2
public static double dst2(double x1, double y1, double x2, double y2) -
x
public double x() -
y
public double y() -
cpy
- Specified by:
cpyin interfaceVectorDouble<Vector2D>- Returns:
- a copy of this vector
-
len
public double len()- Specified by:
lenin interfaceVectorDouble<Vector2D>- Returns:
- The euclidean length
-
len2
public double len2()Description copied from interface:VectorDoubleThis method is faster thanVectorDouble.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:
len2in interfaceVectorDouble<Vector2D>- Returns:
- The squared euclidean length
-
set
Description copied from interface:VectorDoubleSets this vector from the given vector- Specified by:
setin interfaceVectorDouble<Vector2D>- Parameters:
v- The vector- Returns:
- This vector for chaining
-
set
-
set
Sets the components of this vector- Parameters:
x- The x-componenty- The y-component- Returns:
- This vector for chaining
-
sub
Description copied from interface:VectorDoubleSubtracts the given vector from this vector.- Specified by:
subin interfaceVectorDouble<Vector2D>- Parameters:
v- The vector- Returns:
- This vector for chaining
-
sub
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
Description copied from interface:VectorDoubleNormalizes this vector. Does nothing if it is zero.- Specified by:
norin interfaceVectorDouble<Vector2D>- Returns:
- This vector for chaining
-
add
Description copied from interface:VectorDoubleAdds the given vector to this vector- Specified by:
addin interfaceVectorDouble<Vector2D>- Parameters:
v- The vector- Returns:
- This vector for chaining
-
add
Adds the given components to this vector- Parameters:
x- The x-componenty- The y-component- Returns:
- This vector for chaining
-
dot
- Specified by:
dotin interfaceVectorDouble<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
Description copied from interface:VectorDoubleScales this vector by a scalar- Specified by:
sclin interfaceVectorDouble<Vector2D>- Parameters:
scalar- The scalar- Returns:
- This vector for chaining
-
scl
Multiplies this vector by a scalar- Returns:
- This vector for chaining
-
scl
Description copied from interface:VectorDoubleScales this vector by another vector- Specified by:
sclin interfaceVectorDouble<Vector2D>- Returns:
- This vector for chaining
-
mulAdd
Description copied from interface:VectorDoubleFirst scale a supplied vector, then add it to this vector.- Specified by:
mulAddin interfaceVectorDouble<Vector2D>- Parameters:
vec- addition vector.scalar- for scaling the addition vector.
-
mulAdd
Description copied from interface:VectorDoubleFirst scale a supplied vector, then add it to this vector.- Specified by:
mulAddin interfaceVectorDouble<Vector2D>- Parameters:
vec- addition vector.mulVec- vector by whose values the addition vector will be scaled.
-
setZero
Description copied from interface:VectorDoubleSets the components of this vector to 0.- Specified by:
setZeroin interfaceVectorDouble<Vector2D>- Returns:
- This vector for chaining.
-
dst
- Specified by:
dstin interfaceVectorDouble<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 vectory- The y-component of the other vector- Returns:
- the distance between this and the other vector
-
dst2
Description copied from interface:VectorDoubleThis method is faster thanVectorDouble.dst(VectorDouble)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:
dst2in interfaceVectorDouble<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 vectory- The y-component of the other vector- Returns:
- the squared distance between this and the other vector
-
limit
Description copied from interface:VectorDoubleLimits the length of this vector, based on the desired maximum length.- Specified by:
limitin interfaceVectorDouble<Vector2D>- Parameters:
limit- desired maximum length for this vector- Returns:
- this vector for chaining
-
limit2
Description copied from interface:VectorDoubleLimits the length of this vector, based on the desired maximum length squared.
This method is slightly faster than limit().- Specified by:
limit2in interfaceVectorDouble<Vector2D>- Parameters:
limit2- squared desired maximum length for this vector- Returns:
- this vector for chaining
- See Also:
-
setLength
Description copied from interface:VectorDoubleSets the length of this vector. Does nothing is this vector is zero.- Specified by:
setLengthin interfaceVectorDouble<Vector2D>- Parameters:
len- desired length for this vector- Returns:
- this vector for chaining
-
setLength2
Description copied from interface:VectorDoubleSets 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 interfaceVectorDouble<Vector2D>- Parameters:
len2- desired square of the length for this vector.- Returns:
- this vector for chaining.
- See Also:
-
clamp
Description copied from interface:VectorDoubleClamps this vector's length to given min and max values- Specified by:
clampin interfaceVectorDouble<Vector2D>- Parameters:
min- Min lengthmax- Max length- Returns:
- This vector for chaining
-
toString
-
mul
Left-multiplies this vector by the given matrix- Parameters:
mat- the matrix- Returns:
- this vector
-
crs
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)
-
setAngleRad
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.
-
setAngle
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.
-
rotate
Rotates the Vector2d by the given angle, counter-clockwise assuming the y-axis points up.- Parameters:
degrees- the angle in degrees
-
rotateRad
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
-
lerp
Description copied from interface:VectorDoubleLinearly 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:
lerpin interfaceVectorDouble<Vector2D>- Parameters:
target- The target vectoralpha- The interpolation coefficient- Returns:
- This vector for chaining.
-
interpolate
Description copied from interface:VectorDoubleInterpolates 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 interfaceVectorDouble<Vector2D>- Parameters:
target- The target vectoralpha- The interpolation coefficientinterpolation- An Interpolation object describing the used interpolation method- Returns:
- This vector for chaining.
-
setToRandomDirection
Description copied from interface:VectorDoubleSets this vector to the unit vector with a random direction- Specified by:
setToRandomDirectionin interfaceVectorDouble<Vector2D>- Returns:
- This vector for chaining
-
epsilonEquals
Description copied from interface:VectorDoubleCompares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.- Specified by:
epsilonEqualsin interfaceVectorDouble<Vector2D>- Parameters:
other- The other vector.epsilon- The maximum deviation.- 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:
isUnitin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector is a unit length vector
-
isUnit
public boolean isUnit(double margin) - Specified by:
isUnitin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector is a unit length vector within the given margin.
-
isZero
public boolean isZero()- Specified by:
isZeroin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector is a zero vector
-
isZero
public boolean isZero(double margin) - Specified by:
isZeroin interfaceVectorDouble<Vector2D>- Returns:
- Whether the length of this vector is smaller than the given margin
-
isOnLine
- Specified by:
isOnLinein interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is in line with the other vector (either in the same or the opposite direction)
-
isOnLine
- Specified by:
isOnLinein interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is in line with the other vector (either in the same or the opposite direction)
-
isCollinear
- Specified by:
isCollinearin interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is collinear with the other vector (
VectorDouble.isOnLine(VectorDouble, double)andVectorDouble.hasSameDirection(VectorDouble)).
-
isCollinear
- Specified by:
isCollinearin interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is collinear with the other vector (
VectorDouble.isOnLine(VectorDouble)andVectorDouble.hasSameDirection(VectorDouble)).
-
isCollinearOpposite
- Specified by:
isCollinearOppositein interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is opposite collinear with the other vector (
VectorDouble.isOnLine(VectorDouble, double)andVectorDouble.hasOppositeDirection(VectorDouble)).
-
isCollinearOpposite
- Specified by:
isCollinearOppositein interfaceVectorDouble<Vector2D>- Returns:
- true if this vector is opposite collinear with the other vector (
VectorDouble.isOnLine(VectorDouble)andVectorDouble.hasOppositeDirection(VectorDouble)).
-
isPerpendicular
- Specified by:
isPerpendicularin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector is perpendicular with the other vector. True if the dot product is 0.
-
isPerpendicular
- Specified by:
isPerpendicularin interfaceVectorDouble<Vector2D>- Parameters:
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
- Specified by:
hasSameDirectionin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector has similar direction compared to the other vector. True if the normalized dot product is >0.
-
hasOppositeDirection
- Specified by:
hasOppositeDirectionin interfaceVectorDouble<Vector2D>- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is <0.
-