Skip to main content

Overview

The vec2d 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.
def __init__(self, x_or_pair, y=None):
x_or_pair
float or tuple
Either the x-coordinate value, or a tuple/list containing (x, y) coordinates
y
float
default:"None"
The y-coordinate value. If None, x_or_pair must be a tuple/list
Usage Examples:
v1 = vec2d(10, 20)        # Create from x, y
v2 = vec2d((10, 20))      # Create from tuple
v3 = vec2d([10, 20])      # Create from list

Arithmetic Operators

Addition

Supports addition with other vectors, tuples, or scalars.
v1 + v2          # Vector addition
v1 + (5, 10)     # Add tuple
v1 + 5           # Add scalar to both components
v1 += v2         # In-place addition

Subtraction

Supports subtraction with other vectors, tuples, or scalars.
v1 - v2          # Vector subtraction
v1 - (5, 10)     # Subtract tuple
v1 - 5           # Subtract scalar from both components
v1 -= v2         # In-place subtraction

Multiplication

Supports multiplication with other vectors, tuples, or scalars.
v1 * v2          # Component-wise multiplication
v1 * (2, 3)      # Multiply by tuple
v1 * 5           # Scale by scalar
v1 *= 2          # In-place multiplication

Division

Supports division operations (regular, floor, and true division).
v1 / v2          # Component-wise division
v1 / 2           # Divide by scalar
v1 // 2          # Floor division
v1 /= 2          # In-place division

Other Arithmetic

v1 ** 2          # Exponentiation
v1 % 5           # Modulo
-v1              # Negation
+v1              # Positive
abs(v1)          # Absolute value

Comparison Operators

Equality

Compares vectors for equality.
v1 == v2         # True if both x and y are equal
v1 != v2         # True if x or y differ
v1 == (5, 10)    # Can compare with tuples

Indexing and Length

Vectors support indexing and length operations.
v = vec2d(10, 20)
v[0]             # Returns 10 (x component)
v[1]             # Returns 20 (y component)
v[0] = 5         # Set x component
len(v)           # Returns 2

Vector Properties

length

Gets or sets the magnitude (length) of the vector.
v = vec2d(3, 4)
print(v.length)  # Returns 5.0
v.length = 10    # Scales vector to length 10

angle

Gets or sets the angle of the vector in degrees.
v = vec2d(1, 0)
print(v.angle)   # Returns 0.0
v.angle = 90     # Rotates to point upward

Length and Distance Methods

get_length()

Returns the magnitude (length) of the vector.
def get_length(self):
Returns: The length of the vector as a float

get_length_sqrd()

Returns the squared length of the vector (faster than get_length).
def get_length_sqrd(self):
Returns: The squared length as a float

get_distance()

Calculates the distance to another vector.
def get_distance(self, other):
other
vec2d or tuple
The other vector to measure distance to
Returns: The distance between the vectors as a float

get_dist_sqrd()

Calculates the squared distance to another vector (faster than get_distance).
def get_dist_sqrd(self, other):
other
vec2d or tuple
The other vector to measure distance to
Returns: The squared distance as a float

Rotation Methods

rotate()

Rotates the vector in-place by the specified angle.
def rotate(self, angle_degrees):
angle_degrees
float
The angle to rotate by, in degrees

rotated()

Returns a new vector rotated by the specified angle.
def rotated(self, angle_degrees):
angle_degrees
float
The angle to rotate by, in degrees
Returns: A new vec2d rotated by the specified angle

get_angle()

Returns the angle of the vector in degrees.
def get_angle(self):
Returns: The angle in degrees (0-360)

get_angle_between()

Calculates the angle between this vector and another.
def get_angle_between(self, other):
other
vec2d or tuple
The other vector to measure angle to
Returns: The angle between the vectors in degrees

Normalization Methods

normalized()

Returns a normalized (unit) version of the vector.
def normalized(self):
Returns: A new 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.
def normalize_return_length(self):
Returns: The original length of the vector before normalization

Perpendicular Methods

perpendicular()

Returns a vector perpendicular to this one.
def perpendicular(self):
Returns: A new vec2d perpendicular to this vector (rotated 90 degrees)

perpendicular_normal()

Returns a normalized perpendicular vector.
def perpendicular_normal(self):
Returns: A new normalized vec2d perpendicular to this vector

Vector Mathematics

dot()

Calculates the dot product with another vector.
def dot(self, other):
other
vec2d or tuple
The other vector for the dot product
Returns: The dot product as a float

cross()

Calculates the cross product with another vector (returns scalar in 2D).
def cross(self, other):
other
vec2d or tuple
The other vector for the cross product
Returns: The scalar cross product

projection()

Projects this vector onto another vector.
def projection(self, other):
other
vec2d or tuple
The vector to project onto
Returns: A new vec2d representing the projection

Interpolation and Conversion

interpolate_to()

Interpolates between this vector and another.
def interpolate_to(self, other, range):
other
vec2d or tuple
The target vector to interpolate towards
range
float
Interpolation factor (0.0 = this vector, 1.0 = other vector)
Returns: A new vec2d representing the interpolated position

convert_to_basis()

Converts the vector to a different coordinate basis.
def convert_to_basis(self, x_vector, y_vector):
x_vector
vec2d
The basis vector for the x-axis
y_vector
vec2d
The basis vector for the y-axis
Returns: A new vec2d in the specified basis

Bitwise Operators

The vec2d class also supports bitwise operations:
v1 << 2          # Left shift
v1 >> 2          # Right shift
v1 & v2          # Bitwise AND
v1 | v2          # Bitwise OR
v1 ^ v2          # Bitwise XOR
~v1              # Invert (returns -x, -y)

Build docs developers (and LLMs) love