Rotating a unit vector in Mojo (or any programming language) involves applying a rotation matrix or quaternion to the vector. Here's how you can do it in Mojo, assuming you're working with 2D or 3D vectors.
1. 2D Rotation (Counter-Clockwise)
To rotate a 2D unit vector by an angle θ (in radians), use the rotation matrix:
from math import sin, cos
fn rotate_2d(vec: (Float64, Float64), angle: Float64) -> (Float64, Float64):
let (x, y) = vec
let c = cos(angle)
let s = sin(angle)
return (x * c - y * s, x * s + y * c)
# Example usage:
let v = (1.0, 0.0) # Unit vector along x-axis
let rotated_v = rotate_2d(v, 3.14159 / 2) # Rotate 90 degrees (π/2 radians)
print(rotated_v) # Should be close to (0.0, 1.0)
2. 3D Rotation
For 3D, you can rotate around the X, Y, or Z axis using the respective rotation matrices.
fn rotate_z(vec: (Float64, Float64, Float64), angle: Float64) -> (Float64, Float64, Float64):
let (x, y, z) = vec
let c = cos(angle)
let s = sin(angle)
return (x * c - y * s, x * s + y * c, z)
# Example usage:
let v = (1.0, 0.0, 0.0) # Unit vector along x-axis
let rotated_v = rotate_z(v, 3.14159 / 2) # Rotate 90 degrees around Z
print(rotated_v) # Should be close to (0.0, 1.0, 0.0)
fn rotate_x(vec: (Float64, Float64, Float64), angle: Float64) -> (Float64, Float64, Float64):
let (x, y, z) = vec
let c = cos(angle)
let s = sin(angle)
return (x, y * c - z * s, y * s + z * c)
fn rotate_y(vec: (Float64, Float64, Float64), angle: Float64) -> (Float64, Float64, Float64):
let (x, y, z) = vec
let c = cos(angle)
let s = sin(angle)
return (x * c + z * s, y, -x * s + z * c)
3. Using Quaternions (Advanced)
For smoother 3D rotations, you can use quaternions. Here's a basic example:
mojo
from math import sin, cos
struct Quaternion:
var w: Float64
var x: Float64
var y: Float64
var z: Float64
fn rotate_vector(self, vec: (Float64, Float64, Float64)) -> (Float64, Float64, Float64):
let (vx, vy, vz) = vec
# Compute quaternion * vector * quaternion^-1
let q = self
let q_conj = Quaternion(q.w, -q.x, -q.y, -q.z)
let q_vec = Quaternion(0.0, vx, vy, vz)
# First multiplication: q * q_vec
let temp = Quaternion(
-q.x * vx - q.y * vy - q.z * vz,
q.w * vx + q.y * vz - q.z * vy,
q.w * vy - q.x * vz + q.z * vx,
q.w * vz + q.x * vy - q.y * vx
)
# Second multiplication: temp * q_conj
let result = Quaternion(
-temp.x * q_conj.x - temp.y * q_conj.y - temp.z * q_conj.z,
temp.w * q_conj.x + temp.y * q_conj.z - temp.z * q_conj.y,
temp.w * q_conj.y - temp.x * q_conj.z + temp.z * q_conj.x,
temp.w * q_conj.z + temp.x * q_conj.y - temp.y * q_conj.x
)
return (result.x, result.y, result.z)
# Example: Rotate 90 degrees around Z-axis
fn quaternion_from_axis_angle(axis: (Float64, Float64, Float64), angle: Float64) -> Quaternion:
let (ax, ay, az) = axis
let half_angle = angle / 2.0
let s = sin(half_angle)
return Quaternion(cos(half_angle), ax * s, ay * s, az * s)
let axis = (0.0, 0.0, 1.0) # Z-axis
let angle = 3.14159 / 2.0 # 90 degrees in radians
let q = quaternion_from_axis_angle(axis, angle)
let v = (1.0, 0.0, 0.0) # Unit vector along X-axis
let rotated_v = q.rotate_vector(v)
print(rotated_v) # Should be close to (0.0, 1.0, 0.0)
Summary:
For 2D, use a simple rotation matrix.
For 3D, use rotation matrices for X, Y, or Z axes.
For smooth 3D rotations, quaternions are preferred (avoids gimbal lock).
Let me know if you need further optimizations or explanations! 🚀