Skip to content

Vector2D

A 2D vector implementation for handling positions, velocities, and other 2D quantities.

Vector2D

@dataclass
class Vector2D:
    x: float = 0.0
    y: float = 0.0

Methods

add()

def __add__(self, other: Vector2D) -> Vector2D
Add two vectors together.

sub()

def __sub__(self, other: Vector2D) -> Vector2D
Subtract one vector from another.

mul()

def __mul__(self, scalar: float) -> Vector2D
Multiply vector by a scalar.

truediv()

def __truediv__(self, scalar: float) -> Vector2D
Divide vector by a scalar.

magnitude()

def magnitude(self) -> float
Calculate the magnitude (length) of the vector.

normalize()

def normalize(self) -> Vector2D
Return a normalized copy of this vector.

clamp()

def clamp(self, max_magnitude: float) -> Vector2D
Clamp the vector's magnitude to a maximum value.

dot()

def dot(self, other: Vector2D) -> float
Calculate dot product with another vector.

Example Usage

# Create vectors
pos = Vector2D(100.0, 200.0)
velocity = Vector2D(5.0, -3.0)

# Vector arithmetic
new_pos = pos + velocity * dt

# Normalization
direction = velocity.normalize()

# Magnitude
speed = velocity.magnitude()

# Clamping
clamped_velocity = velocity.clamp(max_speed)

# Dot product
angle_cos = direction.dot(Vector2D(1.0, 0.0))

Best Practices

  1. Performance:
  2. Cache vector calculations when possible
  3. Use in-place operations for frequent updates
  4. Consider using integers for pixel-perfect positioning

  5. Precision:

  6. Be aware of floating-point precision issues
  7. Use appropriate epsilon values for comparisons
  8. Consider rounding for pixel alignment

  9. Memory:

  10. Reuse vector objects when possible
  11. Use pooling for frequently created/destroyed vectors
  12. Clear vector references when no longer needed

Common Issues

Precision

  • Floating-point rounding errors
  • Integer vs. float position mismatches
  • Accumulating errors in physics calculations

Performance

  • Unnecessary vector allocations
  • Frequent magnitude calculations
  • Redundant normalizations

Logic

  • Division by zero in normalization
  • Incorrect vector math operations
  • Missing vector cleanup