Gets the y-coordinate of the vector.
Static
downStatic
leftStatic
rightStatic
topCalculates the angle of the vector relative to the positive x-axis.
The angle is computed using the atan2
function, which returns the angle in radians between the positive x-axis and the vector.
The result is in the range [-π, π].
The angle in radians between the vector and the positive x-axis.
Calculates the angle in radians between this vector and another vector.
The angle is calculated using the dot product formula:
cos(theta) = (this · v) / (|this| * |v|)
, where this
and v
are the two vectors.
The result is the smallest angle between the two vectors, in the range [0, π].
The vector to calculate the angle to.
The angle in radians between this vector and the other vector.
Calculates the distance between this vector and another vector.
The distance is computed as the length of the vector difference:
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)
, where x1, y1
are the components of this vector and x2, y2
are the components of the other vector.
The vector to calculate the distance to.
The distance between the two vectors.
Computes the dot product of this vector and another vector.
The dot product is calculated as x1 * x2 + y1 * y2
, where x1, y1
are the components of this vector and x2, y2
are the components of the other vector.
The vector to compute the dot product with.
The dot product as a scalar value.
Performs linear interpolation (lerp) between this vector and another vector.
The result is a vector that is a blend between the two vectors, based on the parameter t
.
If t
is 0, the result is this vector. If t
is 1, the result is the vector v
. Values of t
between 0 and 1
interpolate linearly between the two vectors.
The target vector to interpolate towards.
The interpolation factor, where 0 <= t <= 1
. A value of 0 returns the current vector, and a value of 1 returns the target vector.
A new Vec2
instance representing the interpolated vector.
Returns a new vector with the maximum values of each component from this vector and another vector.
This method compares the corresponding x and y components of both vectors and creates a new vector where each component is the greater of the two. This can be useful for clamping or finding bounds.
The vector to compare against.
A new Vec2
instance with the maximum x and y components.
Returns a new vector with the minimum values of each component from this vector and another vector.
This method compares the corresponding x and y components of both vectors and creates a new vector where each component is the lesser of the two. This can be useful for clamping or finding bounds.
The vector to compare against.
A new Vec2
instance with the minimum x and y components.
Moves this vector towards a target vector by a maximum distance delta.
The method computes the vector difference between the target and this vector. It then scales this difference
so that the resulting vector moves towards the target by no more than maxDistanceDelta
. If the distance
to the target is less than or equal to the maxDistanceDelta
, the method will return the target vector.
The target vector to move towards.
The maximum distance to move towards the target.
A new Vec2
instance representing the new position after moving towards the target.
Reflects this vector across a given normal vector.
The reflection is calculated using the formula:
reflected = this - 2 * (this · normal) * normal
, where this
is the original vector,
normal
is the vector across which to reflect, and ·
represents the dot product.
The result is a new vector that is the reflection of the original vector.
The normal vector to reflect across.
A new Vec2
instance representing the reflected vector.
Calculates the squared length (magnitude) of the vector.
The squared length is computed using the formula x^2 + y^2
, where x
and y
are the components of the vector.
This is often used when comparing the lengths of vectors, as it avoids the computational cost of calculating the square root.
The squared length of the vector.
Returns a string representation of the vector.
This method returns a string that represents the vector in the format "Vector2(x, y)"
, where x
and y
are the
components of the vector. This is useful for debugging and logging purposes.
A string representation of the vector, e.g., "Vector2(3, 4)"
.
Unpacks the vector into an array of its x and y components.
This method returns a tuple containing the x and y values of the vector, allowing for easy access to both components in a format that can be destructured or used in other operations.
A tuple [x, y]
representing the x and y components of the vector.
Gets the x-coordinate of the vector.