-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformationMatrix.lua
78 lines (67 loc) · 1.71 KB
/
transformationMatrix.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
local inspect = require 'lib/inspect'
REFLECT_MATRIX = {
{1, 0, 0},
{0, -1, 0},
{0, 0, 1}
}
TransformationMatrix = {
result = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}
}
function TransformationMatrix:new (...)
local arg={...}
o = {}
setmetatable(o, self)
self.__index = self
for i, v in ipairs(arg) do
o.result = multiply_matrix(o.result, v)
end
return o
end
function TransformationMatrix:translate(x, y)
trans_matrix = {
{1, 0, x},
{0, 1, y},
{0, 0, 1}
}
self.result = multiply_matrix(self.result, trans_matrix)
end
function TransformationMatrix:reflect_y()
self.result = multiply_matrix(self.result, REFLECT_MATRIX)
end
function TransformationMatrix:rotate(angle)
rotate_matrix = {
{math.cos(angle), -math.sin(angle), 0},
{math.sin(angle), math.cos(angle), 0},
{0, 0, 1}
}
self.result = multiply_matrix(self.result, rotate_matrix)
end
function TransformationMatrix:transform(matrix)
return multiply_matrix(self.result, matrix)
end
function TransformationMatrix:transform_to_point(matrix)
matrix = multiply_matrix(self.result, matrix)
return {x = matrix[1][1], y = matrix[2][1]}
end
function multiply_matrix(m1, m2)
local res = {}
--print(inspect(m1))
--print(inspect(m2))
for i = 1, #m1 do
res[i] = {}
for j = 1, #m2[1] do
res[i][j] = 0
for k = 1, #m2 do
--print("res[i][j]: " .. res[i][j])
--print("m1[i][k]: " .. m1[i][k])
--print("m2[k][j]: " .. m2[k][j])
res[i][j] = res[i][j] + m1[i][k] * m2[k][j]
end
end
end
return res
end