Write a function that creates a rotation matrix on LUA
- User
Write a function that creates a rotation matrix on LUA
- Assistant
Write a function that creates a rotation matrix on LUA
Certainly! Below is a Lua implementation of functions that create rotation matrices for both 2D and 3D space. These functions can be used to rotate vectors in graphical applications, simulations, or any computational geometry tasks.
The 2D rotation matrix rotates points in the 2D Cartesian coordinate system counterclockwise through an angle θ about the origin.
The rotation matrix R for an angle θ is:
[ R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \ \sin\theta & \cos\theta \end{pmatrix} ]
Here's the Lua function to create a 2D rotation matrix:
-- Function to create a 2D rotation matrix
function createRotationMatrix2D(theta)
local cos_theta = math.cos(theta)
local sin_theta = math.sin(theta)
local rotationMatrix = {
{cos_theta, -sin_theta},
{sin_theta, cos_theta}
}
return rotationMatrix
end
-- Example usage:
local theta = math.rad(45) -- Convert 45 degrees to radians
local rotationMatrix2D = createRotationMatrix2D(theta)
print("2D Rotation Matrix:")
for i = 1, 2 do
for j = 1, 2 do
io.write(string.format("%.4f\t", rotationMatrix2D[i][j]))
end
io.write("\n")
end
Output:
2D Rotation Matrix:
0.7071 -0.7071
0.7071 0.7071
In 3D space, rotation matrices can rotate points around the X, Y, or Z axes.
[ R_x(\theta) = \begin{pmatrix} 1 & 0 & 0 \ 0 & \cos\theta & -\sin\theta \ 0 & \sin\theta & \cos\theta \end{pmatrix} ]
-- Function to create a 3D rotation matrix around the X-axis
function createRotationMatrix3DX(theta)
local cos_theta = math.cos(theta)
local sin_theta = math.sin(theta)
local rotationMatrix = {
{1, 0, 0},
{0, cos_theta, -sin_theta},
{0, sin_theta, cos_theta}
}
return rotationMatrix
end
[ R_y(\theta) = \begin{pmatrix} \cos\theta & 0 & \sin\theta \ 0 & 1 & 0 \ -\sin\theta & 0 & \cos\theta \end{pmatrix} ]
-- Function to create a 3D rotation matrix around the Y-axis
function createRotationMatrix3DY(theta)
local cos_theta = math.cos(theta)
local sin_theta = math.sin(theta)
local rotationMatrix = {
{ cos_theta, 0, sin_theta},
{ 0, 1, 0},
{-sin_theta, 0, cos_theta}
}
return rotationMatrix
end
[ R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 \ \sin\theta & \cos\theta & 0 \ 0 & 0 & 1 \end{pmatrix} ]
-- Function to create a 3D rotation matrix around the Z-axis
function createRotationMatrix3DZ(theta)
local cos_theta = math.cos(theta)
local sin_theta = math.sin(theta)
local rotationMatrix = {
{cos_theta, -sin_theta, 0},
{sin_theta, cos_theta, 0},
{ 0, 0, 1}
}
return rotationMatrix
end
local theta = math.rad(30) -- Convert 30 degrees to radians
local rotationMatrixX = createRotationMatrix3DX(theta)
local rotationMatrixY = createRotationMatrix3DY(theta)
local rotationMatrixZ = createRotationMatrix3DZ(theta)
-- Function to print a 3x3 matrix
local function printMatrix3D(matrix, label)
print("\n" .. label)
for i = 1, 3 do
for j = 1, 3 do
io.write(string.format("%.4f\t", matrix[i][j]))
end
io.write("\n")
end
end
printMatrix3D(rotationMatrixX, "3D Rotation Matrix around X-axis:")
printMatrix3D(rotationMatrixY, "3D Rotation Matrix around Y-axis:")
printMatrix3D(rotationMatrixZ, "3D Rotation Matrix around Z-axis:")
Output:
3D Rotation Matrix around X-axis:
1.0000 0.0000 0.0000
0.0000 0.8660 -0.5000
0.0000 0.5000 0.8660
3D Rotation Matrix around Y-axis:
0.8660 0.0000 0.5000
0.0000 1.0000 0.0000
-0.5000 0.0000 0.8660
3D Rotation Matrix around Z-axis:
0.8660 -0.5000 0.0000
0.5000 0.8660 0.0000
0.0000 0.0000 1.0000
To rotate a vector using the rotation matrix, perform matrix multiplication between the rotation matrix and the vector.
-- Function to multiply a matrix by a vector
function multiplyMatrixVector(matrix, vector)
local result = {}
for i = 1, #matrix do
result[i] = 0
for j = 1, #vector do
result[i] = result[i] + matrix[i][j] * vector[j]
end
end
return result
end
-- Example 2D vector
local vector2D = {1, 0}
-- Rotate the vector
local rotatedVector2D = multiplyMatrixVector(rotationMatrix2D, vector2D)
print("\nRotated 2D Vector:")
for i = 1, #rotatedVector2D do
print(string.format("%.4f", rotatedVector2D[i]))
end
Output:
Rotated 2D Vector:
0.7071
0.7071
-- Example 3D vector
local vector3D = {1, 0, 0}
-- Rotate the vector around the Z-axis
local rotatedVector3D = multiplyMatrixVector(rotationMatrixZ, vector3D)
print("\nRotated 3D Vector around Z-axis:")
for i = 1, #rotatedVector3D do
print(string.format("%.4f", rotatedVector3D[i]))
end
Output:
Rotated 3D Vector around Z-axis:
0.8660
0.5000
0.0000
math.cos() and math.sin() functions expect angles in radians. Use math.rad(degrees) to convert degrees to radians.multiplyMatrixVector() function multiplies a matrix by a vector, which is essential for applying rotations.For rotations around an arbitrary axis in 3D, you can use Rodrigues' rotation formula or quaternions. However, for simplicity and common use cases, rotations around the principal axes (X, Y, Z) are often sufficient.
Feel free to integrate these functions into your code and modify them according to your specific requirements. If you have any questions or need further assistance with applying these rotation matrices, let me know!
Not yet included in a published release.