# 2DobjCalculations

Oct 18th, 2021
1,695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local function makeRot(rot)
3.     return { math.cos(rot), -math.sin(rot), 0,
4.             math.sin(rot), math.cos(rot), 0,
5.             0, 0, 1, }
6. end
7.
8. local function makeIdentity()
9.     return { 1, 0, 0,
10.             0, 1, 0,
11.             0, 0, 1, }
12. end
13.
14. local function makeScale(scale)
15.     return { scale.x, 0, 0,
16.             0, scale.y, 0,
17.             0, 0, 1, }
18. end
19.
20. local function makeLoc(loc)
21.     return { 1, 0, loc.x,
22.             0, 1, loc.y,
23.             0, 0, loc.z, }
24. end
25.
26. local function matMul(m,vL)
27.     local result = {}
28.     --print(textutils.serialise(m))
29.     for i = 1, #vL, 3 do
30.         local i1,i2 = i+1,i+2
31.         result[i]  = vL[i]*m[1]+vL[i1]*m[4]+vL[i2]*m[7]
32.         result[i1] = vL[i]*m[2]+vL[i1]*m[5]+vL[i2]*m[8]
33.         result[i2] = vL[i]*m[3]+vL[i1]*m[6]+vL[i2]*m[9]
34.     end
35.     return result
36. end
37.
38. local function makeTri()
39.     local data = {
40.         loc = vector.new( 0, 0, 1),
41.         rot = 0,
42.         scale = vector.new( 5, 5 ),
43.         vL = {
44.             -1/3, 0, 1,
45.             0,    1, 1,
46.             1/3,  0, 1,
47.         },
48.         iL = {
49.             1, 2, 3,
50.         },
51.         cL = {
52.             0xee6c4d,
53.         }
54.     }
55.     for i,val in ipairs(data.iL) do
56.         data.iL[i] = val*3-2
57.     end
58.     return data
59. end
60.
61. --center to screen, grab vertices from index list and load them into final vertice list
62. local function postProcess(vL,iL,display)
63.     local hw = display.x * 0.5
64.     local hh = display.y * 0.5
65.     local result = {}
66.     for i = 1, #iL, 9 do
67.         i1,i2 = i+1,i+2
68.         result[i]   = vL[ iL[i] ]   + hw
69.         result[i+1] = vL[ iL[i]+1 ] + hh
70.         result[i+2] = vL[ iL[i]+2 ]
71.
72.         result[i+3] = vL[ iL[i1] ]   + hw
73.         result[i+4] = vL[ iL[i1]+1 ] + hh
74.         result[i+5] = vL[ iL[i1]+2 ]
75.
76.         result[i+6] = vL[ iL[i2] ]   + hw
77.         result[i+7] = vL[ iL[i2]+1 ] + hh
78.         result[i+8] = vL[ iL[i2]+2 ]
79.     end
80.     return result
81. end
82.
83. local function processVertices(oL,display)
84.     local result = {}
85.     for name,obj in pairs(oL) do
86.         result[name] = {}
87.         result[name] = matMul(makeLoc(obj.loc), matMul(makeRot(obj.rot), matMul(makeScale(obj.scale), obj.vL)))
88.         result[name] = postProcess(result[name],obj.iL,display)
89.         result[name].cL = obj.cL
90.     end
91.     return result
92. end
93.
94. return {
95.     processVertices = processVertices,
96. }
RAW Paste Data