-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix.lua
58 lines (51 loc) · 1.54 KB
/
matrix.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
local pi = math.pi
local matrix = {}
local mt
mt = {
__call = function (self, x, y)
return self.v11*x + self.v12*y + self.v13,
self.v21*x + self.v22*y + self.v23
end;
__mul = function(a, b)
local t = setmetatable({
v11 = a.v11*b.v11 + a.v12*b.v21 + a.v13*b.v31;
v12 = a.v11*b.v12 + a.v12*b.v22 + a.v13*b.v32;
v13 = a.v11*b.v13 + a.v12*b.v23 + a.v13*b.v33;
v21 = a.v21*b.v11 + a.v22*b.v21 + a.v23*b.v31;
v22 = a.v21*b.v12 + a.v22*b.v22 + a.v23*b.v32;
v23 = a.v21*b.v13 + a.v22*b.v23 + a.v23*b.v33;
v31 = a.v31*b.v11 + a.v32*b.v21 + a.v33*b.v31;
v32 = a.v31*b.v12 + a.v32*b.v22 + a.v33*b.v32;
v33 = a.v31*b.v13 + a.v32*b.v23 + a.v33*b.v33;
}, mt)
return t
end;
}
matrix.identity = function()
return function(x, y)
return x, y
end
end
matrix.trans = function (dx,dy)
return setmetatable({
v11 = 1; v12 = 0; v13 = dx;
v21 = 0; v22 = 1; v23 = dy;
v31 = 0; v32 = 0; v33 = 1;
}, mt)
end
matrix.rotate = function(rot)
rot = rot / 180 * pi
return setmetatable({
v11 = math.cos(rot); v12 = -math.sin(rot); v13 = 0;
v21 = math.sin(rot); v22 = math.cos(rot); v23 = 0;
v31 = 0 ; v32 = 0 ; v33 = 1;
}, mt)
end
matrix.scale = function (sx,sy)
return setmetatable({
v11 = sx; v12 = 0; v13 = 0;
v21 = 0; v22 = sy; v23 = 0;
v31 = 0; v32 = 0; v33 = 1;
}, mt)
end
return matrix