Vector2
Class: Vector2 (ScVec2)
This class provides methods to perform operations on 2D vectors, such as addition, subtraction, multiplication, rotation, normalization, and more. It uses x and y components to represent the vector.
Functions
set
Vector2:set( x, y )
Sets the X and Y values of the vector.
Arguments:
- x [ number ] The new X value.
- y [ number ] The new Y value.
Returns:
- [ Vector2 ] The current vector instance with updated values.
add
Vector2:add( other )
Adds another vector to the current vector.
Arguments:
- other [ Vector2 ] Another vector to add.
Returns:
- [ Vector2 ] The resulting vector after addition.
sub
Vector2:sub( other )
Subtracts another vector from the current vector.
Arguments:
- other [ Vector2 ] Another vector to subtract.
Returns:
- [ Vector2 ] The resulting vector after subtraction.
mul
Vector2:mul( other )
Multiplies the current vector by another vector element-wise.
Arguments:
- other [ Vector2 ] Another vector to multiply.
Returns:
- [ Vector2 ] The resulting vector after multiplication.
div
Vector2:div( other )
Divides the current vector by another vector element-wise.
Arguments:
- other [ Vector2 ] Another vector to divide.
Returns:
- [ Vector2 ] The resulting vector after division.
normalize
Vector2:normalize()
Normalizes the vector, scaling it to unit length (magnitude = 1).
Returns:
- [ Vector2 ] The normalized vector.
safeNormalize
Vector2:safeNormalize()
Safely normalizes the vector, ensuring no division by zero. If the vector is a zero vector, returns (0, 0).
Returns:
- [ Vector2 ] The normalized vector or
(0, 0)if the vector is a zero vector.
length
Vector2:length()
Calculates the length (magnitude) of the vector.
Returns:
- [ number ] The length of the vector.
length2
Vector2:length2()
Calculates the squared length (magnitude) of the vector. This is often more efficient than calculating the length for comparison.
Returns:
- [ number ] The squared length of the vector.
dot
Vector2:dot( other )
Calculates the dot product of the current vector and another vector.
Arguments:
- other [ Vector2 ] Another vector.
Returns:
- [ number ] The dot product of the two vectors.
cross
Vector2:cross( other )
Calculates the cross product of the current vector and another vector.
Arguments:
- other [ Vector2 ] Another vector.
Returns:
- [ number ] The result of the cross product.
max
Vector2:max()
Finds the maximum value between the two vector components (x and y).
Returns:
- [ number ] The maximum value between x and y.
min
Vector2:min()
Finds the minimum value between the two vector components (x and y).
Returns:
- [ number ] The minimum value between x and y.
rotateX
Vector2:rotateX( angle )
Rotates the vector around the X-axis by a given angle (in degrees).
Arguments:
- angle [ number ] The angle in degrees to rotate.
Returns:
- [ Vector2 ] The rotated vector.
rotateY
Vector2:rotateY( angle )
Rotates the vector around the Y-axis by a given angle (in degrees).
Arguments:
- angle [ number ] The angle in degrees to rotate.
Returns:
- [ Vector2 ] The rotated vector.
tostring
Vector2:tostring()
Converts the vector to a string representation.
Returns:
- [ string ] A string representing the vector in the form
"(x, y)".
clone
Vector2:clone()
Creates a new vector that is a clone of the current one.
Returns:
- [ Vector2 ] A new vector that is a copy of the current one.
distance
Vector2:distance( other )
Calculates the distance between the current vector and another vector.
Arguments:
- other [ Vector2 ] Another vector to calculate distance to.
Returns:
- [ number ] The distance between the two vectors.