Package yearreview.app.animation
Class Vector2D
java.lang.Object
yearreview.app.animation.Vector2D
A class representing an immutable 2D-Vector of
Floats
.
Similar to Point2D
but allows for direct access of the x and y values.- Author:
- ColdStone37
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Vector2D
A constant Vector facing downwards: (0, 1)static final Vector2D
A constant Vector facing left: (-1, 0)static final Vector2D
A constant Vector facing right: (1, 0)static final Vector2D
A constant Vector facing upwards: (0, -1)final float
X-value of the Vector.final float
Y-value of the Vector.static final Vector2D
A constant zero Vector: (0, 0) -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionGets an inverted version of this Vector: (-x, -y)float
Gets the length of this Vector using the Pythagoras-Theorem: sqrt(x * x + y * y)Gets a normalized version of this Vector.getScaled
(float scale) Gets this Vector scaled by a passed value.Gets a new Vector which is the result of subtracting this Vector with a passed Vector.static Vector2D
Mixes the values of two Vectors.Gets a new Vector which is the result of adding this Vector to a passed Vector.setLength
(float l) Gets a Vector facing the same direction as this Vector with a given length.toString()
Formatted output of a Vector (e.g.: "(4.3, -1.2)")
-
Field Details
-
UP
A constant Vector facing upwards: (0, -1) -
RIGHT
A constant Vector facing right: (1, 0) -
DOWN
A constant Vector facing downwards: (0, 1) -
LEFT
A constant Vector facing left: (-1, 0) -
ZERO
A constant zero Vector: (0, 0) -
x
public final float xX-value of the Vector. -
y
public final float yY-value of the Vector.
-
-
Constructor Details
-
Vector2D
public Vector2D(float x, float y) Constructs a new Vector from an x- and y-value.- Parameters:
x
- x-value of the Vectory
- y-value of the Vector
-
-
Method Details
-
mix
Mixes the values of two Vectors. Result: (v1 * mix + v2 * (1-mix))- Parameters:
mix
- percentage of the mixv1
- first Vectorv2
- second Vector- Returns:
- mixed Vector
-
toString
Formatted output of a Vector (e.g.: "(4.3, -1.2)") -
getNormalized
Gets a normalized version of this Vector. If this Vectors x and y values are zero a Vector facing to the right is returned.- Returns:
- Vector of length 1 facing the same direction
-
setLength
Gets a Vector facing the same direction as this Vector with a given length. Works by first normalizing the Vector and then scaling it.- Parameters:
l
- length of the new Vector- Returns:
- Vector with length l
-
getScaled
Gets this Vector scaled by a passed value.- Parameters:
scale
- value to scale the Vector by- Returns:
- scaled Vector
-
getLength
public float getLength()Gets the length of this Vector using the Pythagoras-Theorem: sqrt(x * x + y * y)- Returns:
- length of the Vector
-
getInverted
Gets an inverted version of this Vector: (-x, -y)- Returns:
- inverted Vector
-
plus
Gets a new Vector which is the result of adding this Vector to a passed Vector.- Parameters:
other
- Vector to add- Returns:
- sum of the Vectors
-
minus
Gets a new Vector which is the result of subtracting this Vector with a passed Vector.- Parameters:
other
- Vector to subtract- Returns:
- difference between the Vectors
-