3D Rotations without matrices
# Rotate a 3D point (x, y, z) around each axis
# Rotation around X axis
function rotate_x(x, y, z, θ):
new_y = y * cos(θ) - z * sin(θ)
new_z = y * sin(θ) + z * cos(θ)
return (x, new_y, new_z)
# Rotation around Y axis
function rotate_y(x, y, z, θ):
new_x = x * cos(θ) + z * sin(θ)
new_z = -x * sin(θ) + z * cos(θ)
return (new_x, y, new_z)
# Rotation around Z axis
function rotate_z(x, y, z, θ):
new_x = x * cos(θ) - y * sin(θ)
new_y = x * sin(θ) + y * cos(θ)
return (new_x, new_y, z)
Notes: - Apply rotations in sequence; order matters. - Angles should be in radians. - Each function rotates around the origin (0,0,0).
To rotate around an arbitrary point (cx, cy, cz), you:
- Translate the point so the rotation center becomes the origin
- Apply the rotation
- Translate back