Guest User

matrix

a guest
Jun 17th, 2013
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local matrix = {
  2.     add = function(self, o)
  3.         n = { } for i=1,4 do
  4.             n[i] = {}
  5.             for j=1,4 do
  6.                 n[i][j] = self[i][j] + o[i][j]
  7.             end
  8.         end
  9.         return new(n)
  10.     end;
  11.    
  12.     mul = function(self, o)
  13.         n = { }
  14.         if not o then error("value is nil!") end
  15.         --Scalar multiplication
  16.         if type(o) == "number" then
  17.             for i=1,4 do
  18.                 n[i] = {}
  19.                 for j=1,4 do
  20.                     n[i][j] = self[i][j] * o
  21.                 end
  22.             end
  23.         --Matrix multiplication
  24.         elseif type(o) == "table" then
  25.             for i=1,4 do
  26.                 n[i] = {}
  27.                 for j=1,4 do
  28.                     n[i][j] = self[i][1] * o[1][j] +
  29.                               self[i][2] * o[2][j] +
  30.                               self[i][3] * o[3][j] +
  31.                               self[i][4] * o[4][j]
  32.                 end
  33.             end
  34.         end
  35.         return new(n)
  36.     end;
  37.    
  38.     --Transforms vectors by a matrix
  39.     apply = function(self, vertex)
  40.         local x = vertex.x * self[1][1] +
  41.                   vertex.y * self[2][1] +
  42.                   vertex.z * self[3][1] +
  43.                     1 * self[4][1]  --where w == 1 for moves in space
  44.         local y = vertex.x * self[1][2] +
  45.                   vertex.y * self[2][2] +
  46.                   vertex.z * self[3][2] +
  47.                     1 * self[4][2]
  48.         local z = vertex.x * self[1][3] +
  49.                   vertex.y * self[2][3] +
  50.                   vertex.z * self[3][3] +
  51.                     1 * self[4][3]
  52.         return vector.new(x,y,z)
  53.     end;
  54.    
  55.     tostring = function(self)
  56.         str = ""
  57.         for i=1,4 do
  58.             str = str.."[ "
  59.             for j=1,4 do
  60.                 str = str..self[i][j].." "
  61.             end
  62.             str = str.."]"
  63.             if i < 4 then str = str.."\n" end
  64.         end
  65.         return str
  66.     end;
  67. }
  68.  
  69. local matmeta = {
  70.     __index = matrix;
  71.     __add = matrix.add;
  72.     __mul = matrix.mul;
  73.     __unm = function(self)
  74.         return self:mul(-1)
  75.     end;
  76.     __tostring = matrix.tostring;
  77. }
  78.  
  79. identity = function()
  80.     n = {
  81.         {1, 0, 0, 0},
  82.         {0, 1, 0, 0},
  83.         {0, 0, 1, 0},
  84.         {0, 0, 0, 1}
  85.     }
  86.     return new(n)
  87. end
  88.  
  89. createTranslation = function(p1,p2,p3)
  90.     x,y,z = 0,p2,p3
  91.     if type(p1) == "table" then
  92.         x = p1.x
  93.         y = p1.y
  94.         z = p1.z
  95.     else x = p1 end
  96.  
  97.     n = {
  98.         {1, 0, 0, 0},
  99.         {0, 1, 0, 0},
  100.         {0, 0, 1, 0},
  101.         {x, y, z, 1}
  102.     }
  103.     return new(n)
  104. end
  105.  
  106. createScale = function(s)
  107.     n = {
  108.         {s, 0, 0, 0},
  109.         {0, s, 0, 0},
  110.         {0, 0, s, 0},
  111.         {0, 0, 0, 1}
  112.     }
  113.     return new(n)
  114. end
  115.  
  116. --TODO: Trigonometry Lookup Tables
  117. createRotationX = function(r)
  118.     n = {
  119.         {1, 0, 0, 0},
  120.         {0, math.cos(r), math.sin(r), 0},
  121.         {0,-math.sin(r), math.cos(r), 0},
  122.         {0, 0, 0, 1}
  123.     }
  124.     return new(n)
  125. end
  126.  
  127. createRotationY = function(r)
  128.     n = {
  129.         {math.cos(r), 0,-math.sin(r), 0},
  130.         {0, 1, 0, 0},
  131.         {math.sin(r), 0, math.cos(r), 0},
  132.         {0, 0, 0, 1}
  133.     }
  134.     return new(n)
  135. end
  136.  
  137. createRotationZ = function(r)
  138.     n = {
  139.         { math.cos(r), math.sin(r), 0, 0},
  140.         {-math.sin(r), math.cos(r), 0, 0},
  141.         {0, 0, 1, 0},
  142.         {0, 0, 0, 1}
  143.     }
  144.     return new(n)
  145. end
  146.  
  147. --More an internal function- I think most people will just work
  148. --with the identity matrix.
  149. function new(n)
  150.     if not n then
  151.         n = { }
  152.         for i=1,4 do n[i] = {}
  153.             for j=1,4 do n[i][j] = 0 end
  154.         end
  155.     end
  156.     setmetatable(n, matmeta)
  157.     return n
  158. end
Advertisement
Add Comment
Please, Sign In to add comment