Overview
Thevec2d class is a 2D vector implementation that supports both vector and scalar operations, along with a comprehensive set of high-level vector mathematics functions.
Constructor
__init__()
Creates a new 2D vector.Either the x-coordinate value, or a tuple/list containing (x, y) coordinates
The y-coordinate value. If None, x_or_pair must be a tuple/list
Arithmetic Operators
Addition
Supports addition with other vectors, tuples, or scalars.Subtraction
Supports subtraction with other vectors, tuples, or scalars.Multiplication
Supports multiplication with other vectors, tuples, or scalars.Division
Supports division operations (regular, floor, and true division).Other Arithmetic
Comparison Operators
Equality
Compares vectors for equality.Indexing and Length
Vectors support indexing and length operations.Vector Properties
length
Gets or sets the magnitude (length) of the vector.angle
Gets or sets the angle of the vector in degrees.Length and Distance Methods
get_length()
Returns the magnitude (length) of the vector.get_length_sqrd()
Returns the squared length of the vector (faster than get_length).get_distance()
Calculates the distance to another vector.The other vector to measure distance to
get_dist_sqrd()
Calculates the squared distance to another vector (faster than get_distance).The other vector to measure distance to
Rotation Methods
rotate()
Rotates the vector in-place by the specified angle.The angle to rotate by, in degrees
rotated()
Returns a new vector rotated by the specified angle.The angle to rotate by, in degrees
vec2d rotated by the specified angle
get_angle()
Returns the angle of the vector in degrees.get_angle_between()
Calculates the angle between this vector and another.The other vector to measure angle to
Normalization Methods
normalized()
Returns a normalized (unit) version of the vector.vec2d with length 1, or (0, 0) if the original length is 0
normalize_return_length()
Normalizes the vector in-place and returns the original length.Perpendicular Methods
perpendicular()
Returns a vector perpendicular to this one.vec2d perpendicular to this vector (rotated 90 degrees)
perpendicular_normal()
Returns a normalized perpendicular vector.vec2d perpendicular to this vector
Vector Mathematics
dot()
Calculates the dot product with another vector.The other vector for the dot product
cross()
Calculates the cross product with another vector (returns scalar in 2D).The other vector for the cross product
projection()
Projects this vector onto another vector.The vector to project onto
vec2d representing the projection
Interpolation and Conversion
interpolate_to()
Interpolates between this vector and another.The target vector to interpolate towards
Interpolation factor (0.0 = this vector, 1.0 = other vector)
vec2d representing the interpolated position
convert_to_basis()
Converts the vector to a different coordinate basis.The basis vector for the x-axis
The basis vector for the y-axis
vec2d in the specified basis