Package arc.math.geom
Class Vec2
java.lang.Object
arc.math.geom.Vec2
Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(float x, float y) Adds the given components to this vectorAdds the given vector to this vectorfloat
angle()
float
float
angleRad()
float
approachDelta
(Vec2 target, float alpha) clamp
(float min, float max) Clamps this vector's length to given min and max valuesclamp
(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
Calculates the 2D cross product between this and the given vector.Inverse of scl()float
dot
(float ox, float oy) float
float
dst
(float x, float y) float
float
dst2
(float x, float y) float
This method is faster thanVector.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 testingboolean
epsilonEquals
(float x, float y, float epsilon) Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.boolean
epsilonEquals
(Vec2 other) Compares this vector with the other vector using Mathf.FLOAT_ROUNDING_ERROR for fuzzy equality testingboolean
epsilonEquals
(Vec2 other, float epsilon) Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.boolean
fromString
(String v) Sets thisVec2
to the value represented by the specified string according to the format oftoString()
.float
getX()
float
getY()
int
hashCode()
boolean
hasOppositeDirection
(Vec2 vector) boolean
hasSameDirection
(Vec2 vector) 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
isCollinear
(Vec2 other) boolean
isCollinear
(Vec2 other, float epsilon) boolean
isCollinearOpposite
(Vec2 other) boolean
isCollinearOpposite
(Vec2 other, float epsilon) boolean
boolean
isNaN()
boolean
boolean
boolean
isPerpendicular
(Vec2 vector) boolean
isPerpendicular
(Vec2 vector, float epsilon) boolean
isUnit()
boolean
isUnit
(float margin) boolean
isZero()
boolean
isZero
(float margin) float
len()
float
len2()
This method is faster thanVector.len()
because it avoids calculating a square root.lerp
(float tx, float ty, 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) 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.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.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 scalarscl
(float x, float y) Multiplies this vector by a scalarScales this vector by another vectorset
(float x, float y) Sets the components of this vectorSets this vector from the given vectorsetAngle
(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 directionsetToRandomDirection
(Rand rand) setZero()
Sets the components of this vector to 0snap()
Snaps this vector's coordinates to integers.sub
(float x, float y) Substracts the other vector from this vector.Subtracts the given vector from this vector.toString()
Converts thisVec2
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.Vector
minus, plus, times, unaryMinus
-
Field Details
-
X
-
Y
-
ZERO
-
x
public float xthe x-component of this vector -
y
public float ythe 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-componenty
- The y-component
-
Vec2
Constructs a vector from the given vector- Parameters:
v
- The vector
-
-
Method Details
-
trns
-
trns
-
trnsExact
-
snap
Snaps this vector's coordinates to integers. -
div
Description copied from interface:Vector
Inverse of scl() -
cpy
-
len
public float len() -
len2
public float len2()Description copied from interface:Vector
This method is faster thanVector.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
Description copied from interface:Vector
Sets this vector from the given vector -
set
-
set
Sets the components of this vector- Parameters:
x
- The x-componenty
- The y-component- Returns:
- This vector for chaining
-
set
-
sub
-
sub
Description copied from interface:Vector
Subtracts the given vector from this vector. -
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
-
sub
-
nor
Description copied from interface:Vector
Normalizes this vector. Does nothing if it is zero. -
add
Description copied from interface:Vector
Adds the given vector to this vector -
add
-
add
-
add
Adds the given components to this vector- Parameters:
x
- The x-componenty
- The y-component- Returns:
- This vector for chaining
-
dot
-
dot
public float dot(float ox, float oy) -
scl
Description copied from interface:Vector
Scales this vector by a scalar -
inv
-
scl
Multiplies this vector by a scalar- Returns:
- This vector for chaining
-
scl
Description copied from interface:Vector
Scales this vector by another vector -
mulAdd
Description copied from interface:Vector
First scale a supplied vector, then add it to this vector. -
mulAdd
Description copied from interface:Vector
First scale a supplied vector, then add it to this vector. -
dst
-
dst
public float dst(float x, float y) -
dst2
Description copied from interface:Vector
This method is faster thanVector.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. -
dst2
public float dst2(float x, float y) -
clampLength
-
limit
Description copied from interface:Vector
Limits the length of this vector, based on the desired maximum length. -
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(). -
clamp
Description copied from interface:Vector
Clamps this vector's length to given min and max values -
setLength
Description copied from interface:Vector
Sets the length of this vector. Does nothing if this vector is zero. -
setLength2
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 interfaceVector<Vec2>
- Parameters:
len2
- desired square of the length for this vector- Returns:
- this vector for chaining
- See Also:
-
toString
Converts thisVec2
to a string in the format(x,y)
. -
clamp
-
tryFromString
-
fromString
Sets thisVec2
to the value represented by the specified string according to the format oftoString()
.- Parameters:
v
- the string.- Returns:
- this vector for chaining
-
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 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 vectory
- 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
- 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
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
- 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
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
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
-
rotate
Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.- Parameters:
degrees
- the angle in degrees
-
rotateAround
Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.- Parameters:
degrees
- the angle in degreesreference
- center Vec2
-
rotateRad
Rotates the Vec2 by the given angle, counter-clockwise assuming the y-axis points up.- Parameters:
radians
- the angle in radians
-
rotateRadExact
-
rotateAroundRad
Rotates the Vec2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.- Parameters:
radians
- the angle in radiansreference
- center Vec2
-
rotate90
Rotates the Vec2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. -
approachDelta
-
approach
-
lerpPast
-
lerpDelta
-
lerpDelta
-
lerp
-
lerp
-
lerp
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. -
interpolate
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 interfaceVector<Vec2>
- 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:Vector
Sets this vector to the unit vector with a random direction- Specified by:
setToRandomDirection
in interfaceVector<Vec2>
- Returns:
- This vector for chaining
-
setToRandomDirection
-
hashCode
public int hashCode() -
equals
-
epsilonEquals
Description copied from interface:Vector
Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.- Specified by:
epsilonEquals
in interfaceVector<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
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 comparey
- 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() -
isUnit
public boolean isUnit(float margin) -
isZero
public boolean isZero() -
isZero
public boolean isZero(float margin) -
isOnLine
-
isOnLine
-
isCollinear
- Specified by:
isCollinear
in interfaceVector<Vec2>
- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector, float)
&&Vector.hasSameDirection(Vector)
).
-
isCollinear
- Specified by:
isCollinear
in interfaceVector<Vec2>
- Returns:
- true if this vector is collinear with the other vector (
Vector.isOnLine(Vector)
&&Vector.hasSameDirection(Vector)
).
-
isCollinearOpposite
- Specified by:
isCollinearOpposite
in interfaceVector<Vec2>
- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector, float)
&&Vector.hasOppositeDirection(Vector)
).
-
isCollinearOpposite
- Specified by:
isCollinearOpposite
in interfaceVector<Vec2>
- Returns:
- true if this vector is opposite collinear with the other vector (
Vector.isOnLine(Vector)
&&Vector.hasOppositeDirection(Vector)
).
-
isPerpendicular
- Specified by:
isPerpendicular
in interfaceVector<Vec2>
- Returns:
- Whether this vector is perpendicular with the other vector. True if the dot product is 0.
-
isPerpendicular
- Specified by:
isPerpendicular
in interfaceVector<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
- Specified by:
hasSameDirection
in interfaceVector<Vec2>
- Returns:
- Whether this vector has similar direction compared to the other vector. True if the normalized dot product is > 0.
-
hasOppositeDirection
- Specified by:
hasOppositeDirection
in interfaceVector<Vec2>
- Returns:
- Whether this vector has opposite direction compared to the other vector. True if the normalized dot product is < 0.
-
setZero
Description copied from interface:Vector
Sets the components of this vector to 0 -
getX
public float getX() -
getY
public float getY()
-