Mojo doesn’t have a built-in vector type in its standard library, but you can easily define your own Vector2 struct with all the math operations you need. This allows you to bundle vector operations (addition, subtraction, rotation, etc.) into a single type. Here’s how you can do it:
1. Define a Vector2 Struct
Create a struct to represent 2D vectors with x and y components. Add methods for common operations like addition, subtraction, scaling, rotation, and normalization.
struct Vector2:
var x: Float64
var y: Float64
# Constructor
fn new(x: Float64, y: Float64) -> Vector2:
return Vector2(x, y)
# Operator overloads
fn __add__(self, other: Vector2) -> Vector2:
return Vector2(self.x + other.x, self.y + other.y)
fn __sub__(self, other: Vector2) -> Vector2:
return Vector2(self.x - other.x, self.y - other.y)
fn __mul__(self, scalar: Float64) -> Vector2:
return Vector2(self.x * scalar, self.y * scalar)
# Useful methods
fn length(self) -> Float64:
return sqrt(self.x * self.x + self.y * self.y)
fn normalize(self) -> Vector2:
let len = self.length()
return self * (1.0 / len) if len != 0.0 else self
fn rotate(self, angle: Float64) -> Vector2:
# Rotate the vector by 'angle' radians (counter-clockwise)
let c = cos(angle)
let s = sin(angle)
return Vector2(
self.x * c - self.y * s,
self.x * s + self.y * c
)
# Convenience method to create a unit vector from an angle
static fn from_angle(angle: Float64) -> Vector2:
return Vector2(cos(angle), sin(angle))
# String representation for debugging
fn __str__(self) -> String:
return f"({self.x:.2f}, {self.y:.2f})"
2. Example Usage
Here’s how you’d use this Vector2 struct:
let v1 = Vector2.new(1.0, 0.0) # Unit vector along the x-axis
let v2 = Vector2.from_angle(3.14159 / 4) # 45-degree unit vector
# Basic operations
let sum = v1 + v2
let difference = v1 - v2
let scaled = v1 * 2.0
print("Sum:", sum) # Output: (1.70, 0.71)
print("Difference:", difference) # Output: (0.30, -0.71)
print("Scaled:", scaled) # Output: (2.00, 0.00)
# Rotation
let rotated_v1 = v1.rotate(3.14159 / 2) # Rotate 90 degrees
print("Rotated 90°:", rotated_v1) # Output: (0.00, 1.00)
# Normalization
let unnormalized = Vector2.new(3.0, 4.0)
let normalized = unnormalized.normalize()
print("Normalized:", normalized) # Output: (0.60, 0.80)
3. Key Features of This Implementation
- Operator Overloading: Use
+, -, * for vector addition, subtraction, and scalar multiplication.
- Rotation: Built-in
rotate() method using the rotation matrix.
- Normalization:
normalize() ensures the vector has length 1.
- Factory Method:
from_angle() creates unit vectors pointing in a given direction.
- String Representation: Easy debugging with
__str__.
4. Extending the Vector Class
If you need more features (e.g., dot product, cross product, or 3D vectors), you can add them:
# Add a dot product method
fn dot(self, other: Vector2) -> Float64:
return self.x * other.x + self.y * other.y
# Add a static method for dot product between two vectors
static fn dot(a: Vector2, b: Vector2) -> Float64:
return a.x * b.x + a.y * b.y
5. Third-Party Libraries
If you want something more robust, check if there are any community-made vector math libraries for Mojo. If not, writing your own is straightforward (as shown above).
This approach gives you a clean, object-oriented way to work with 2D vectors while keeping all operations bundled in a single struct. Let me know if you need help expanding this! 🎮