Vector
The Vector
class provides a simple implementation of a two-dimensional vector. It supports common operations such as addition, subtraction, scalar multiplication and division, normalization, and more. The class makes use of Python’s operator overloading to enable intuitive arithmetic with vectors, and it also offers properties for accessing and modifying the vector’s components, magnitude, and direction.
dreamplet.math.Vector
Initializes a new 2D vector with the given x and y coordinates.
Parameters
x
(float): The x-coordinate.y
(float): The y-coordinate.
set
Updates the x and y coordinates of the vector.
copy
Returns a duplicate of the vector.
dot
Calculates the dot product of the current vector with another vector.
v1 = Vector(1.0, 2.0)
v2 = Vector(3.0, 4.0)
result = v1.dot(v2)
print(result) # Output: 11.0 (1*3 + 2*4)
normalize
Returns a new vector that is the normalized version of the current vector (i.e., with a magnitude of 1). Raises a ValueError if the vector is zero.
limit
Limits the magnitude of the vector to limit_scalar
. If the current magnitude exceeds the limit, the vector is scaled down to the specified maximum.
x
and y
Get or set the individual x and y components of the vector.
v = Vector(1.0, 2.0)
print(v.x, v.y) # Output: 1.0 2.0
v.x = 10.0
v.y = 20.0
print(v.xy) # Output: (10.0, 20.0)
xy
Returns a tuple containing the x and y coordinates of the vector.
direction
Getter: Returns the angle (in degrees) of the vector relative to the positive x-axis, calculated using atan2
.
Setter: Sets the vector’s direction (angle in degrees) while preserving its magnitude.
v = Vector(3.0, 4.0)
v.direction = 90 # Set direction to 90 degrees
print(v) # Output: Vector(x≈0.0, y=5.0)
magnitude
Getter: Returns the length of the vector.
Setter: Sets the vector’s magnitude while preserving its direction.