minecraftwarlock

CCG Installer

Apr 26th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.00 KB | None | 0 0
  1. f = fs.open("CCG/CCG", "w")
  2. f.write([=[
  3. os.loadAPI("CCG/Mat2")
  4. os.loadAPI("CCG/Vec2")
  5. os.loadAPI("CCG/Mat3")
  6. os.loadAPI("CCG/Vec3")
  7. os.loadAPI("CCG/Stack")
  8.  
  9.  
  10. RADIANS = false
  11. DEGREES = true
  12.  
  13. POINTS = 0
  14. TRIANGLES = 1
  15. QUADS = 2
  16.  
  17. WHITE = 1
  18. ORANGE = 2
  19. MAGENTA = 4
  20. LIGHT_BLUE = 8
  21. YELLOW = 16
  22. LIME = 32
  23. PINK = 64
  24. GRAY = 128
  25. LIGHT_GRAY = 256
  26. CYAN = 512
  27. PURPLE = 1024
  28. BLUE = 2048
  29. BROWN = 4096
  30. GREEN = 8192
  31. RED = 16384
  32. BLACK = 32768
  33.  
  34. local Prototype = {}
  35. local mt = {__index = Prototype}
  36.  
  37. local function newPixel(self, pos, background, foreground, chr)
  38.     local x = pos.x
  39.     local y = pos.y
  40.     self.pixels[#self.pixels + 1] = {x, y, background, foreground, chr}
  41. end
  42.  
  43. local function round(x)
  44.     i, d = math.modf(x)
  45.     if d >= 0.5 then
  46.         return i + 1
  47.     end
  48.     return i
  49. end
  50.  
  51. local function drawPixel(self, pixel)
  52.     local x = round(pixel[1])
  53.     local y = round(pixel[2])
  54.     if x >= self.xMin and x <= self.xMax and y >= self.yMin and y <= self.yMax then
  55.         local mon = self.mon
  56.         mon.setCursorPos(self.originX + x, self.originY - y)
  57.         mon.setBackgroundColor(pixel[3])
  58.         mon.setTextColor(pixel[4])
  59.         mon.write(pixel[5])
  60.     end
  61. end
  62.  
  63. local function newVertex(self, pos)
  64.     self.prog[#self.prog + 1] = {pos, self.background, self.foreground, self.char}
  65. end
  66.  
  67. local function points(self)
  68.     for _, v in pairs(self.prog) do
  69.         newPixel(self, v[1], v[2], v[3], v[4])
  70.     end
  71. end
  72.  
  73. local function bottomFlatTriangle(v1, v2, v3)
  74.     local frags = {}
  75.     local invslope1 = (v2.x - v1.x) / (v2.y - v1.y)
  76.     local invslope2 = (v3.x - v1.x) / (v3.y - v1.y)
  77.  
  78.     local curx1 = v1.x
  79.     local curx2 = v1.x
  80.    
  81.     for scanlineY = v1.y, v2.y do
  82.         local inc = 1
  83.         if curx1 > curx2 then
  84.             inc = -1
  85.         end
  86.         for x = curx1, curx2, inc do
  87.             frags[#frags + 1] = Vec3.new(x, scanlineY, 1)
  88.         end
  89.         curx1 = curx1 + invslope1
  90.         curx2 = curx2 + invslope2
  91.     end
  92.     return frags
  93. end
  94.  
  95. local function topFlatTriangle(v1, v2, v3)
  96.     local frags = {}
  97.     local invslope1 = (v3.x - v1.x) / (v3.y - v1.y)
  98.     local invslope2 = (v3.x - v2.x) / (v3.y - v2.y)
  99.  
  100.     local curx1 = v3.x
  101.     local curx2 = v3.x
  102.  
  103.     for scanlineY = v3.y, v1.y, -1 do
  104.         local inc = 1
  105.         if curx1 > curx2 then
  106.             inc = -1
  107.         end
  108.         for x = curx1, curx2, inc do
  109.             frags[#frags + 1] = Vec3.new(x, scanlineY, 1)
  110.         end
  111.         curx1 = curx1 - invslope1
  112.         curx2 = curx2 - invslope2
  113.     end
  114.     return frags
  115. end
  116.  
  117. local function sortByY(a, b, c)
  118.     local v1 = a[1]
  119.     local v2 = b[1]
  120.     local v3 = c[1]
  121.    
  122.     if v1.y <= v2.y and v2.y <= v3.y then
  123.         return a, b, c
  124.     elseif v1.y <= v3.y and v3.y <= v2.y then
  125.         return a, c, b
  126.     elseif v2.y <= v1.y and v1.y <= v3.y then
  127.         return b, a, c
  128.     elseif v2.y <= v3.y and v3.y <= v1.y then
  129.         return b, c, a
  130.     elseif v3.y <= v1.y and v1.y <= v2.y then
  131.         return c, a, b
  132.     elseif v3.y <= v2.y and v2.y <= v1.y then
  133.         return c, b, a
  134.     end
  135. end
  136.  
  137. local function triangle(self, a2, b2, c2)
  138.     local vt1 = a2[1]
  139.     local vt2 = b2[1]
  140.     local vt3 = c2[1]
  141.     local a, b, c = sortByY(a2, b2, c2)
  142.     local v1 = a[1]
  143.     local v2 = b[1]
  144.     local v3 = c[1]
  145.    
  146.     if v2.y == v3.y then
  147.         for k, v in ipairs(bottomFlatTriangle(v1, v2, v3)) do
  148.             local dist1 = (v - vt1):magSquared()
  149.             local dist2 = (v - vt2):magSquared()
  150.             local dist3 = (v - vt3):magSquared()
  151.             local closest = math.min(dist1, math.min(dist2, dist3))
  152.             if closest == dist1 then
  153.                 newPixel(self, v, a2[2], a2[3], a2[4])
  154.             elseif closest == dist2 then
  155.                 newPixel(self, v, b2[2], b2[3], b2[4])
  156.             elseif closest == dist3 then
  157.                 newPixel(self, v, c2[2], c2[3], c2[4])
  158.             end
  159.         end
  160.     elseif v1.y == v2.y then
  161.         for k, v in ipairs(topFlatTriangle(v1, v2, v3)) do
  162.             local dist1 = (v - vt1):magSquared()
  163.             local dist2 = (v - vt2):magSquared()
  164.             local dist3 = (v - vt3):magSquared()
  165.             local closest = math.min(dist1, math.min(dist2, dist3))
  166.             if closest == dist1 then
  167.                 newPixel(self, v, a2[2], a2[3], a2[4])
  168.             elseif closest == dist2 then
  169.                 newPixel(self, v, b2[2], b2[3], b2[4])
  170.             elseif closest == dist3 then
  171.                 newPixel(self, v, c2[2], c2[3], c2[4])
  172.             end
  173.         end
  174.     else
  175.         local v4 = Vec3.new((v1.x + ((v2.y - v1.y) / (v3.y - v1.y)) * (v3.x - v1.x)), v2.y, 1)
  176.         local frags1 = bottomFlatTriangle(v1, v2, v4)
  177.         local frags2 = topFlatTriangle(v2, v4, v3)
  178.         for k, v in ipairs(frags2) do
  179.             local dist1 = (v - vt1):magSquared()
  180.             local dist2 = (v - vt2):magSquared()
  181.             local dist3 = (v - vt3):magSquared()
  182.             local closest = math.min(dist1, math.min(dist2, dist3))
  183.             if closest == dist1 then
  184.                 newPixel(self, v, a2[2], a2[3], a2[4])
  185.             elseif closest == dist2 then
  186.                 newPixel(self, v, b2[2], b2[3], b2[4])
  187.             elseif closest == dist3 then
  188.                 newPixel(self, v, c2[2], c2[3], c2[4])
  189.             end
  190.         end
  191.         for k, v in ipairs(frags1) do
  192.             local dist1 = (v - vt1):magSquared()
  193.             local dist2 = (v - vt2):magSquared()
  194.             local dist3 = (v - vt3):magSquared()
  195.             local closest = math.min(dist1, math.min(dist2, dist3))
  196.             if closest == dist1 then
  197.                 newPixel(self, v, a2[2], a2[3], a2[4])
  198.             elseif closest == dist2 then
  199.                 newPixel(self, v, b2[2], b2[3], b2[4])
  200.             elseif closest == dist3 then
  201.                 newPixel(self, v, c2[2], c2[3], c2[4])
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. local function triangles(self)
  208.     local l = #self.prog
  209.     local count = (l - (l % 3)) / 3
  210.     for i = 0, count - 1 do
  211.         triangle(self, self.prog[i * 3 + 1], self.prog[i * 3 + 2], self.prog[i * 3 + 3])
  212.     end
  213. end
  214.  
  215. local function quad(self, a2, b2, c2, d2)
  216.     local vt1 = a2[1]
  217.     local vt2 = b2[1]
  218.     local vt3 = c2[1]
  219.     local vt4 = d2[1]
  220.    
  221.     local a, b, c = sortByY(a2, b2, d2)
  222.     local v1 = a[1]
  223.     local v2 = b[1]
  224.     local v3 = c[1]
  225.    
  226.     if v2.y == v3.y then
  227.         for k, v in ipairs(bottomFlatTriangle(v1, v2, v3)) do
  228.             local dist1 = (v - vt1):magSquared()
  229.             local dist2 = (v - vt2):magSquared()
  230.             local dist3 = (v - vt3):magSquared()
  231.             local dist4 = (v - vt4):magSquared()
  232.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  233.             if closest == dist1 then
  234.                 newPixel(self, v, a2[2], a2[3], a2[4])
  235.             elseif closest == dist2 then
  236.                 newPixel(self, v, b2[2], b2[3], b2[4])
  237.             elseif closest == dist3 then
  238.                 newPixel(self, v, c2[2], c2[3], c2[4])
  239.             elseif closest == dist3 then
  240.                 newPixel(self, v, c2[2], c2[3], c2[4])
  241.             elseif closest == dist4 then
  242.                 newPixel(self, v, d2[2], d2[3], d2[4])
  243.             end
  244.         end
  245.     elseif v1.y == v2.y then
  246.         for k, v in ipairs(topFlatTriangle(v1, v2, v3)) do
  247.             local dist1 = (v - vt1):magSquared()
  248.             local dist2 = (v - vt2):magSquared()
  249.             local dist3 = (v - vt3):magSquared()
  250.             local dist4 = (v - vt4):magSquared()
  251.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  252.             if closest == dist1 then
  253.                 newPixel(self, v, a2[2], a2[3], a2[4])
  254.             elseif closest == dist2 then
  255.                 newPixel(self, v, b2[2], b2[3], b2[4])
  256.             elseif closest == dist3 then
  257.                 newPixel(self, v, c2[2], c2[3], c2[4])
  258.             elseif closest == dist3 then
  259.                 newPixel(self, v, c2[2], c2[3], c2[4])
  260.             elseif closest == dist4 then
  261.                 newPixel(self, v, d2[2], d2[3], d2[4])
  262.             end
  263.         end
  264.     else
  265.         local v4 = Vec3.new((v1.x + ((v2.y - v1.y) / (v3.y - v1.y)) * (v3.x - v1.x)), v2.y, 1)
  266.         local frags1 = bottomFlatTriangle(v1, v2, v4)
  267.         local frags2 = topFlatTriangle(v2, v4, v3)
  268.         for k, v in ipairs(frags2) do
  269.             local dist1 = (v - vt1):magSquared()
  270.             local dist2 = (v - vt2):magSquared()
  271.             local dist3 = (v - vt3):magSquared()
  272.             local dist4 = (v - vt4):magSquared()
  273.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  274.             if closest == dist1 then
  275.                 newPixel(self, v, a2[2], a2[3], a2[4])
  276.             elseif closest == dist2 then
  277.                 newPixel(self, v, b2[2], b2[3], b2[4])
  278.             elseif closest == dist3 then
  279.                 newPixel(self, v, c2[2], c2[3], c2[4])
  280.             elseif closest == dist3 then
  281.                 newPixel(self, v, c2[2], c2[3], c2[4])
  282.             elseif closest == dist4 then
  283.                 newPixel(self, v, d2[2], d2[3], d2[4])
  284.             end
  285.         end
  286.         for k, v in ipairs(frags1) do
  287.             local dist1 = (v - vt1):magSquared()
  288.             local dist2 = (v - vt2):magSquared()
  289.             local dist3 = (v - vt3):magSquared()
  290.             local dist4 = (v - vt4):magSquared()
  291.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  292.             if closest == dist1 then
  293.                 newPixel(self, v, a2[2], a2[3], a2[4])
  294.             elseif closest == dist2 then
  295.                 newPixel(self, v, b2[2], b2[3], b2[4])
  296.             elseif closest == dist3 then
  297.                 newPixel(self, v, c2[2], c2[3], c2[4])
  298.             elseif closest == dist3 then
  299.                 newPixel(self, v, c2[2], c2[3], c2[4])
  300.             elseif closest == dist4 then
  301.                 newPixel(self, v, d2[2], d2[3], d2[4])
  302.             end
  303.         end
  304.     end
  305.    
  306.    
  307.     local a, b, c = sortByY(b2, c2, d2)
  308.     local v1 = a[1]
  309.     local v2 = b[1]
  310.     local v3 = c[1]
  311.    
  312.     if v2.y == v3.y then
  313.         for k, v in ipairs(bottomFlatTriangle(v1, v2, v3)) do
  314.             local dist1 = (v - vt1):magSquared()
  315.             local dist2 = (v - vt2):magSquared()
  316.             local dist3 = (v - vt3):magSquared()
  317.             local dist4 = (v - vt4):magSquared()
  318.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  319.             if closest == dist1 then
  320.                 newPixel(self, v, a2[2], a2[3], a2[4])
  321.             elseif closest == dist2 then
  322.                 newPixel(self, v, b2[2], b2[3], b2[4])
  323.             elseif closest == dist3 then
  324.                 newPixel(self, v, c2[2], c2[3], c2[4])
  325.             elseif closest == dist3 then
  326.                 newPixel(self, v, c2[2], c2[3], c2[4])
  327.             elseif closest == dist4 then
  328.                 newPixel(self, v, d2[2], d2[3], d2[4])
  329.             end
  330.         end
  331.     elseif v1.y == v2.y then
  332.         for k, v in ipairs(topFlatTriangle(v1, v2, v3)) do
  333.             local dist1 = (v - vt1):magSquared()
  334.             local dist2 = (v - vt2):magSquared()
  335.             local dist3 = (v - vt3):magSquared()
  336.             local dist4 = (v - vt4):magSquared()
  337.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  338.             if closest == dist1 then
  339.                 newPixel(self, v, a2[2], a2[3], a2[4])
  340.             elseif closest == dist2 then
  341.                 newPixel(self, v, b2[2], b2[3], b2[4])
  342.             elseif closest == dist3 then
  343.                 newPixel(self, v, c2[2], c2[3], c2[4])
  344.             elseif closest == dist3 then
  345.                 newPixel(self, v, c2[2], c2[3], c2[4])
  346.             elseif closest == dist4 then
  347.                 newPixel(self, v, d2[2], d2[3], d2[4])
  348.             end
  349.         end
  350.     else
  351.         local v4 = Vec3.new((v1.x + ((v2.y - v1.y) / (v3.y - v1.y)) * (v3.x - v1.x)), v2.y, 1)
  352.         local frags1 = bottomFlatTriangle(v1, v2, v4)
  353.         local frags2 = topFlatTriangle(v2, v4, v3)
  354.         for k, v in ipairs(frags2) do
  355.             local dist1 = (v - vt1):magSquared()
  356.             local dist2 = (v - vt2):magSquared()
  357.             local dist3 = (v - vt3):magSquared()
  358.             local dist4 = (v - vt4):magSquared()
  359.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  360.             if closest == dist1 then
  361.                 newPixel(self, v, a2[2], a2[3], a2[4])
  362.             elseif closest == dist2 then
  363.                 newPixel(self, v, b2[2], b2[3], b2[4])
  364.             elseif closest == dist3 then
  365.                 newPixel(self, v, c2[2], c2[3], c2[4])
  366.             elseif closest == dist3 then
  367.                 newPixel(self, v, c2[2], c2[3], c2[4])
  368.             elseif closest == dist4 then
  369.                 newPixel(self, v, d2[2], d2[3], d2[4])
  370.             end
  371.         end
  372.         for k, v in ipairs(frags1) do
  373.             local dist1 = (v - vt1):magSquared()
  374.             local dist2 = (v - vt2):magSquared()
  375.             local dist3 = (v - vt3):magSquared()
  376.             local dist4 = (v - vt4):magSquared()
  377.             local closest = math.min(math.min(dist1, dist2), math.min(dist3, dist4))
  378.             if closest == dist1 then
  379.                 newPixel(self, v, a2[2], a2[3], a2[4])
  380.             elseif closest == dist2 then
  381.                 newPixel(self, v, b2[2], b2[3], b2[4])
  382.             elseif closest == dist3 then
  383.                 newPixel(self, v, c2[2], c2[3], c2[4])
  384.             elseif closest == dist3 then
  385.                 newPixel(self, v, c2[2], c2[3], c2[4])
  386.             elseif closest == dist4 then
  387.                 newPixel(self, v, d2[2], d2[3], d2[4])
  388.             end
  389.         end
  390.     end
  391. end
  392.  
  393. local function quads(self)
  394.     local l = #self.prog
  395.     local count = (l - (l % 4)) / 4
  396.     for i = 0, count - 1 do
  397.         quad(self, self.prog[i * 4 + 1], self.prog[i * 4 + 2], self.prog[i * 4 + 3], self.prog[i * 4 + 4])
  398.     end
  399. end
  400.  
  401. local function fragment(self)
  402.     if self.progID == POINTS then
  403.         points(self)
  404.     elseif self.progID == TRIANGLES then
  405.         triangles(self)
  406.     elseif self.progID == QUADS then
  407.         quads(self)
  408.     end
  409. end
  410.  
  411. local function angle(mode, i)
  412.     if mode then
  413.         if i >= 360 then
  414.             return angle(mode, i - 360)
  415.         elseif i < 0 then
  416.             return angle(mode, i + 360)
  417.         else
  418.             return math.rad(i)
  419.         end
  420.     else
  421.         if i >= 2 * math.pi then
  422.             return angle(mode, i - 2 * math.pi)
  423.         elseif i < 0 then
  424.             return angle(mode, i + 2 * math.pi)
  425.         else
  426.             return i
  427.         end
  428.     end
  429. end
  430.    
  431.    
  432. function Prototype:loadIdentity()
  433.     stack.current.value = Mat3.identity()
  434. end
  435.  
  436. function Prototype:pushMatrix(matrix)
  437.     if type(matrix) == "table" then
  438.         if matrix.type == "mat2" then
  439.             self.stack:push(matrix:mat3())
  440.         end
  441.     else
  442.         self.stack:push(Mat3.identity())
  443.     end
  444. end
  445.  
  446. function Prototype:popMatrix()
  447.     return self.stack:pop()
  448. end
  449.  
  450. function Prototype:vertex(x, y)
  451.     if type(x) == "number" and type(y) == "number" then
  452.         x = Vec3.new(x, y, 1)
  453.         local matrices = self.stack:toTable()
  454.         for i = #matrices, 1, -1 do
  455.             x = x * matrices[i]
  456.         end
  457.         newVertex(self, x)
  458.     elseif type(x) == "table" then
  459.         if x.type == "vec2" then
  460.             x = x:vec3(1)
  461.             local matrices = self.stack:toTable()
  462.             for i = #matrices, 1, -1 do
  463.                 x = x * matrices[i]
  464.             end
  465.             newVertex(self, x)
  466.         end
  467.     end
  468. end
  469.  
  470. function Prototype:draw()
  471.     fragment(self)
  472.     for k, v in ipairs(self.pixels) do
  473.         drawPixel(self, v)
  474.     end
  475. end
  476.    
  477.  
  478. function Prototype:rotate(i)
  479.     i = angle(self.angleMode, i)
  480.     self.stack.current.value = self.stack.current.value * createRotate(i)
  481. end
  482.  
  483. function Prototype:translate(x, y)
  484.     if type(x) == "number" and type(y) == "number" then
  485.         self.stack.current.value = stack.current.value * createTranslate(x, y)
  486.     elseif type(x) == "table" and x.type == "vec2" then
  487.         self.stack.current.value = stack.current.value * createTranslate(x.x, x.y)
  488.     end
  489. end
  490.  
  491. function Prototype:scale(x, y)
  492.     if type(x) == "number" and type(y) == "number" then
  493.         self.stack.current.value = stack.current.value * createScale(x, y)
  494.     elseif type(x) == "table" and x.type == "vec2" then
  495.         self.stack.current.value = stack.current.value * createScale(x.x, x.y)
  496.     end
  497. end
  498.  
  499. function Prototype:applyMatrix(matrix)
  500.     if type(matrix) == "table" and matrix.type == "mat2" then
  501.         self.stack.current.value = stack.current.value * matrix:mat3()
  502.     end
  503. end
  504.  
  505. function Prototype:setAngleMode(mode)
  506.     self.angleMode = mode
  507. end
  508.  
  509. function Prototype:setBackground(color)
  510.     self.background = color
  511. end
  512.  
  513. function Prototype:setForeground(color)
  514.     self.foreground = color
  515. end
  516.  
  517. function Prototype:setChar(char)
  518.     self.char = char
  519. end
  520.  
  521. function Prototype:setClearColor(color)
  522.     self.clear = color
  523. end
  524.  
  525. function Prototype:pixelClear()
  526.     self.pixels = {}
  527. end
  528.  
  529. function Prototype:matrixClear()
  530.     self.stack = Stack.new()
  531.     pushMatrix()
  532. end
  533.  
  534. function Prototype:screenClear()
  535.     self.mon.setBackgroundColor(self.clear)
  536.     for x = self.xMin, self.xMax do
  537.         for y = self.yMin, self.yMax do
  538.             self.mon.setCursorPos(self.originX + x, self.originY - y)
  539.             self.mon.write(" ")
  540.         end
  541.     end
  542. end
  543.  
  544. function Prototype:begin(prog)
  545.     fragment(self)
  546.     self.progID = prog
  547.     self.prog = {}
  548. end
  549.  
  550. function Prototype:finish()
  551.     self:begin(POINTS)
  552. end
  553.  
  554.  
  555. function createTranslate(x, y)
  556.     return Mat3.new(1, 0, x,
  557.                     0, 1, y,
  558.                     0, 0, 1)
  559. end
  560.  
  561. function createScale(x, y)
  562.     return Mat3.new(x, 0, 0,
  563.                     0, y, 0,
  564.                     0, 0, 1)
  565. end
  566.  
  567. function createRotate(radians)
  568.     s = math.sin(radians)
  569.     c = math.cos(radians)
  570.     return Mat3.new(c, s, 0,
  571.                     -s, c, 0,
  572.                     0, 0, 1)
  573. end
  574.  
  575. function createScale2(x, y)
  576.     return Mat2.new(x, 0,
  577.                     0, y)
  578. end
  579.  
  580. function createRotate2(radians)
  581.     s = math.sin(radians)
  582.     c = math.cos(radians)
  583.     return Mat2.new(c, s,
  584.                     -s, c)
  585. end
  586.  
  587.  
  588. function createContext(originX, originY, xMin, yMin, xMax, yMax, mon)
  589.     t = {["originX"] = originX, ["originY"] = originY,
  590.          ["xMin"] = xMin, ["yMin"] = yMin,
  591.          ["xMax"] = xMax, ["yMax"] = yMax,  
  592.          ["mon"] = mon, ["angleMode"] = DEGREES, ["char"] = " ", ["background"] = BLACK, ["foreground"] = WHITE, ["clear"] = BLACK, progID = POINTS,
  593.          pixels = {}, prog = {}, stack = Stack.new()}
  594.     setmetatable(t, mt)
  595.     t:pushMatrix()
  596.     return t
  597. end]=])
  598. f.close()
  599.  
  600. f = fs.open("CCG/Mat2", "w")
  601. f.write([=[
  602. local cofactorMap = {{1, -1},
  603.                      {-1, 1}}
  604.                      
  605. function add(a, b)
  606.     return new(a[1][1] + b[1][1], a[2][1] + b[2][1],
  607.                a[1][2] + b[1][2], a[2][2] + b[2][2])
  608. end
  609.  
  610. function sub(a, b)
  611.     return new(a[1][1] - b[1][1], a[2][1] - b[2][1],
  612.                a[1][2] - b[1][2], a[2][2] - b[2][2])
  613. end
  614.  
  615. function mult(a, b)
  616.     if type(b) == "number" then
  617.         return new(a[1][1] * b, a[2][1] * b,
  618.                    a[1][2] * b, a[2][2] * b)
  619.     elseif type(a) == "number" then
  620.         return new(b[1][1] * a, b[2][1] * a,
  621.                    b[1][2] * a, b[2][2] * a)
  622.     elseif type(a) == "table" and type(b) == "table" then
  623.         if a.type == "mat2" and b.type == "mat2" then
  624.             return new(a[1][1] * b[1][1] + a[2][1] * b[1][2], a[1][1] * b[2][1] + a[2][1] * b[2][2],
  625.                        a[1][2] * b[1][1] + a[2][2] * b[2][1], a[2][1] * b[1][2] + a[2][2] * b[2][2])
  626.         elseif a.type == "mat2" and b.type == "vec2" then
  627.             return b * a
  628.         end
  629.     end
  630.     error("cannot multiply matrix", 2)
  631. end
  632.  
  633. function div(a, b)
  634.     if type(b) == "number" then
  635.         return new(a[1][1] / b, a[2][1] / b,
  636.                    a[1][2] / b, a[2][2] / b)
  637.     elseif type(a) == "table" and type(b) == "table" then
  638.         if a.type == "mat2" and b.type == "mat2" then
  639.             return a * b:inv()
  640.         end
  641.     end
  642.     error("cannot divide matrix", 2)
  643. end
  644.  
  645. function transpose(a)
  646.     return new(a[1][1], a[1][2],
  647.                a[2][1], a[2][2])
  648. end
  649.  
  650. function minor(a, col, row)
  651.     local ret = 0
  652.     for x = 1, 2 do
  653.         for y = 1, 2 do
  654.             if x ~= col and y ~= row then
  655.                 ret = a[x][y]
  656.             end
  657.         end
  658.     end
  659. end
  660.  
  661. function cofactor(a, col, row)
  662.     return a:minor(col, row) * cofactorMap[col][row]
  663. end
  664.  
  665. function det(a)
  666.     return a[1][1] * a[2][2] - a[2][1] * a[1][2]
  667. end
  668.  
  669. function inv(a)
  670.     return 1 / a:det() * new(a[2][2], -a[2][1]
  671.                              -a[1][2], a[2][2])
  672. end
  673.  
  674. function mat3(a)
  675.     return Mat3.new(a[1][1], a[2][1], 0,
  676.                     a[2][1], a[2][2], 0,
  677.                     0, 0, 1)
  678. end
  679.  
  680. --[[
  681. To be implemented
  682. function Mat4(a)
  683.     return Mat4.new(a[1][1], a[2][1], 0, 0,  
  684.                     a[1][2], a[2][2], 0, 0,
  685.                     0, 0, 1, 0,
  686.                     0, 0, 0, 1)
  687. end]]
  688.  
  689.  
  690. function identity()
  691.     return new(1, 0,
  692.                0, 1)
  693. end
  694.  
  695. local Prototype = {["type"] = "mat2"}
  696.  
  697. local mt = {}
  698.  
  699. mt.__index = Prototype
  700.  
  701.  
  702. function new(a, b,
  703.              c, d)
  704.     t = {{a, c}, {b, d}}
  705.     setmetatable(t, mt)
  706.     return t
  707. end
  708.  
  709.  
  710. Prototype.add = add
  711. Prototype.div = div
  712. Prototype.mult = mult
  713. Prototype.sub = sub
  714. Prototype.cofactor = cofactor
  715. Prototype.minor = minor
  716. Prototype.inv = inv
  717. Prototype.det = det
  718. Prototype.transpose = transpose
  719. Prototype.mat3 = mat3
  720. --Prototype.Mat4 = Mat4
  721.  
  722. mt.__add = add
  723. mt.__mul = mult
  724. mt.__div = div
  725. mt.__sub = sub
  726. function mt.__unm(a)
  727.     return -1 * a
  728. end
  729. function mt.__tostring(a)
  730.     return a[1][1] .. ", " .. a[2][1] .."\n" ..
  731.            a[1][2] .. ", " .. a[2][2]
  732. end
  733.     ]=])
  734. f.close()
  735.  
  736. f = fs.open("CCG/Mat3", "w")
  737. f.write([=[
  738. local cofactorMap = {{1, -1, 1},
  739.                      {-1, 1, -1},
  740.                      {1, -1, 1}}
  741.  
  742.  
  743. function add(a, b)
  744.     return new(a[1][1] + b[1][1], a[2][1] + b[2][1], a[3][1] + b[3][1],
  745.                a[1][2] + b[1][2], a[2][2] + b[2][2], a[3][2] + b[3][2],
  746.                a[1][3] + b[1][3], a[2][3] + b[2][3], a[3][3] + b[3][3])
  747. end
  748.  
  749. function sub(a, b)
  750.     return new(a[1][1] - b[1][1], a[2][1] - b[2][1], a[3][1] - b[3][1],
  751.                a[1][2] - b[1][2], a[2][2] - b[2][2], a[3][2] - b[3][2],
  752.                a[1][3] - b[1][3], a[2][3] - b[2][3], a[3][3] - b[3][3])
  753. end
  754.  
  755. function mult(a, b)
  756.     if type(b) == "number" then
  757.         return new(a[1][1] * b, a[2][1] * b, a[3][1] * b,  
  758.                    a[1][2] * b, a[2][2] * b, a[3][2] * b,
  759.                    a[1][3] * b, a[2][3] * b, a[3][3] * b)
  760.     elseif type(a) == "number" then
  761.         return new(b[1][1] * a, b[2][1] * a, b[3][1] * a,  
  762.                    b[1][2] * a, b[2][2] * a, b[3][2] * a,
  763.                    b[1][3] * a, b[2][3] * a, b[3][3] * a)
  764.     elseif type(a) == "table" and type(b) == "table" then
  765.         if a.type == "mat3" and b.type == "mat3" then
  766.             local c = a[1][1] * b[1][1] + a[2][1] * b[1][2] + a[3][1] * b[1][3]
  767.             local d = a[1][1] * b[2][1] + a[2][1] * b[2][2] + a[3][1] * b[2][3]
  768.             local e = a[1][1] * b[3][1] + a[2][1] * b[3][2] + a[3][1] * b[3][3]
  769.            
  770.             local f = a[1][2] * b[1][1] + a[2][2] * b[1][2] + a[3][2] * b[1][3]
  771.             local g = a[1][2] * b[2][1] + a[2][2] * b[2][2] + a[3][2] * b[2][3]
  772.             local h = a[1][2] * b[3][1] + a[2][2] * b[3][2] + a[3][2] * b[3][3]
  773.            
  774.             local i = a[1][3] * b[1][1] + a[2][3] * b[1][2] + a[3][3] * b[1][3]
  775.             local j = a[1][3] * b[2][1] + a[2][3] * b[2][2] + a[3][3] * b[2][3]
  776.             local k = a[1][3] * b[3][1] + a[2][3] * b[3][2] + a[3][3] * b[3][3]
  777.            
  778.             return new(
  779.                 c, d, e,
  780.                 f, g, h,
  781.                 i, j, k
  782.             )
  783.         elseif a.type == "mat3" and b.type == "vec3" then
  784.             return b * a
  785.         end
  786.     end
  787.     error("cannot multiply matrix", 2)
  788. end
  789.  
  790. function div(a, b)
  791.     if type(b) == "number" then
  792.         return new(a[1][1] / b, a[2][1] / b, a[3][1] / b,  
  793.                    a[1][2] / b, a[2][2] / b, a[3][2] / b,
  794.                    a[1][3] / b, a[2][3] / b, a[3][3] / b)
  795.     elseif type(a) == "table" and type(b) == "table" then
  796.         if a.type == "mat3" and b.type == "mat3" then
  797.             return a * b:inv()
  798.         end
  799.     end
  800.     error("cannot divide matrix", 2)
  801. end
  802.  
  803. function transpose(a)
  804.     return new(a[1][1], a[1][2], a[1][3],
  805.                a[2][1], a[2][2], a[2][3],
  806.                a[3][1], a[3][2], a[3][3])
  807. end
  808.  
  809. function minor(a, col, row)
  810.     local ret = {}
  811.     local xx = 0
  812.     for x = 1, 3 do
  813.         if x ~= col then
  814.             xx = xx + 1
  815.         end
  816.         local yy = 0
  817.         for y = 1, 3 do
  818.             if y ~= col then
  819.                 yy = yy + 1
  820.             end
  821.             if x ~= col and y ~= col then
  822.                 ret[xx][yy] = a[x][y]
  823.             end
  824.         end
  825.     end
  826.     return Mat2.new(ret[1][1], ret[2][1],
  827.                     ret[1][2], ret[2][2]):det()
  828. end
  829.  
  830. function cofactor(a, col, row)
  831.     return a:minor(col, row) * cofactorMap[col][row]
  832. end
  833.  
  834. function det(a)
  835.     return a[1][1] * a[2][2] * a[3][3] +
  836.            a[2][1] * a[3][2] * a[1][3] +
  837.            a[3][1] * a[1][2] * a[2][3] -
  838.            a[3][1] * a[2][2] * a[1][3] -
  839.            a[2][1] * a[1][2] * a[3][3] -
  840.            a[1][1] * a[3][2] * a[2][3]
  841. end
  842.  
  843. function inv(a)
  844.     return 1 / a:det() * new((a[2][2] * a[3][3] - a[3][2] * a[2][3]), -(a[2][1] * a[3][3] - a[3][1] * a[2][3]), (a[2][1] * a[3][2] - a[3][1] * a[2][2]),
  845.                              -(a[1][2] * a[3][3] - a[3][2] * a[1][3]), (a[1][1] * a[3][3] - a[3][1] * a[1][3]), -(a[1][1] * a[3][2] - a[3][1] * a[1][2]),
  846.                              (a[1][2] * a[2][3] - a[2][2] * a[1][3]), -(a[1][1] * a[2][3] -a[2][1] * a[1][3]), (a[1][1] * a[2][2] - a[2][1] * a[1][2]))
  847. end
  848.  
  849. --[[
  850. To be implemented
  851. function Mat4(a)
  852.     return Mat4.new(a[1][1], a[2][1], a[3][1], 0,  
  853.                     a[1][2], a[2][2], a[3][2], 0,
  854.                     a[1][3], a[2][3], a[3][3], 0,
  855.                     0, 0, 0, 1)
  856. end]]
  857.  
  858.  
  859. function identity()
  860.     return new(1, 0, 0,
  861.                0, 1, 0,
  862.                0, 0, 1)
  863. end
  864.  
  865. local Prototype = {["type"] = "mat3"}
  866.  
  867. local mt = {}
  868.  
  869. mt.__index = Prototype
  870.  
  871.  
  872. function new(a, b, c,
  873.              d, e, f,
  874.              g, h, i)
  875.     t = {{a, d, g}, {b, e, h}, {c, f, i}}
  876.     setmetatable(t, mt)
  877.     return t
  878. end
  879.  
  880.  
  881. Prototype.add = add
  882. Prototype.div = div
  883. Prototype.mult = mult
  884. Prototype.sub = sub
  885. Prototype.cofactor = cofactor
  886. Prototype.minor = minor
  887. Prototype.inv = inv
  888. Prototype.det = det
  889. Prototype.transpose = transpose
  890. Prototype.cross = cross
  891. --Prototype.Mat4 = Mat4
  892.  
  893. mt.__add = add
  894. mt.__mul = mult
  895. mt.__div = div
  896. mt.__sub = sub
  897. mt.__unm = function(a)
  898.     return -1 * a
  899. end
  900. function mt.__tostring(a)
  901.     return a[1][1] .. ", " .. a[2][1] .. ", " .. a[3][1] .. "\n" ..
  902.            a[1][2] .. ", " .. a[2][2] .. ", " .. a[2][3] .. "\n" ..
  903.            a[1][3] .. ", " .. a[2][3] .. ", " .. a[3][3]
  904. end
  905.     ]=])
  906. f.close()
  907.  
  908. f = fs.open("CCG/Stack", "w")
  909. f.write([=[
  910. local function newEntry(nxt, val)
  911.     return {["next"] = nxt, ["value"] = val}
  912. end
  913.  
  914. function push(stack, value)
  915.     stack.current = newEntry(stack.current, value)
  916. end
  917.  
  918. function pop(stack)
  919.     if stack.current ~= nil then
  920.         local ret = stack.current.value
  921.         stack.current = stack.current.next
  922.         return ret
  923.     end
  924.     return nil
  925. end
  926.  
  927. function forEach(stack, func)
  928.     local v = stack.current
  929.     local k = 1
  930.     while v ~= nil do
  931.         func(k, v.value)
  932.         k = k + 1
  933.         v = v.next
  934.     end
  935. end
  936.  
  937. function toTable(stack)
  938.     local ret = {}
  939.     stack:forEach(function(k, v)
  940.         ret[k] = v
  941.     end)
  942.     return ret
  943. end
  944.  
  945. local Prototype = {}
  946.  
  947. Prototype.push = push
  948. Prototype.pop = pop
  949. Prototype.forEach = forEach
  950. Prototype.toTable = toTable
  951.  
  952. local mt = {}
  953. mt.__index = Prototype
  954.  
  955. function new()
  956.     t = {}
  957.     setmetatable(t, mt)
  958.     return t
  959. end]=])
  960. f.close()
  961.  
  962. f = fs.open("CCG/test", "w")
  963. f.write([=[
  964. os.loadAPI("CCG/CCG")
  965. x, y = term.getSize()
  966.  
  967. originX = math.floor(x / 2)
  968. originY = math.floor(y / 2)
  969.  
  970. xMin = 1 - originX
  971. yMin = originY - y
  972.  
  973. xMax = x - originX
  974. yMax = originY - 1
  975.  
  976. g = CCG.createContext(originX, originY, xMin, yMin, xMax, yMax, term)
  977. while true do
  978.     g:setBackground(CCG.GREEN)
  979.     g:begin(CCG.QUADS)
  980.     g:vertex(-5, 5)
  981.     g:setBackground(CCG.RED)
  982.     g:vertex(5, 5)
  983.     g:setBackground(CCG.BLUE)
  984.     g:vertex(5, -5)
  985.     g:setBackground(CCG.PURPLE)
  986.     g:vertex(-5, -5)
  987.     g:finish()
  988.     g:draw()
  989.     sleep(0.1)
  990.     g:pixelClear()
  991.     g:screenClear()
  992.     g:rotate(3)
  993. end
  994.  
  995.  
  996. --g:setBackground(CCG.RED)
  997. --g:vertex(0, 0)
  998. term.clear()
  999. g:draw()
  1000. term.setCursorPos(1, 1)
  1001. sleep(3)]=])
  1002. f.close()
  1003.  
  1004. f = fs.open("CCG/Vec2", "w")
  1005. f.write([=[
  1006. function add(a, b)
  1007.     return new(a.x + b.x, a.y + b.y)
  1008. end
  1009.  
  1010. local function sub(a, b)
  1011.     return new(a.x - b.x, a.y - b.y)
  1012. end
  1013.  
  1014. function mult(a, b)
  1015.     if type(b) == "number" then
  1016.         return new(a.x * b, a.y * b)
  1017.     elseif type(a) == "number" then
  1018.         return new(b.x * a, b.y * a)
  1019.     elseif type(a) == "table" and type(b) == "table" then
  1020.         if a.type == "vec2" and b.type == "vec2" then
  1021.             return a.x * b.x + a.y * b.y
  1022.         elseif a.type == "vec2" and b.type == "mat2" then
  1023.             return new(a.x * b[1][1] + a.y * b[2][1], a.x * b[1][2] + a.y * b[2][2])
  1024.         end
  1025.     end
  1026.     error("cannot multiply vector", 2)
  1027. end
  1028.  
  1029. function div(a, b)
  1030.     if type(b) == "number" then
  1031.         return new(a.x / b, a.y / b)
  1032.     elseif type(a) == "table" and type(b) == "table" then
  1033.         if a.type == "vec2" and b.type == "mat2" then
  1034.             return a * b:inv()
  1035.         end
  1036.     end
  1037.     error("cannot divide vector", 2)
  1038. end
  1039.  
  1040. function mag(a)
  1041.     return math.sqrt(magSquared(a))
  1042. end
  1043.  
  1044. function magSquared(a)
  1045.     return a.x * a.x + a.y * a.y
  1046. end
  1047.  
  1048. function reverse(a)
  1049.     return a * -1
  1050. end
  1051.  
  1052. function normalize(a)
  1053.     local m = mag(a)
  1054.     if m == 0 then
  1055.         error("cannot normalize a vector with length 0", 2)
  1056.     end
  1057.     return new(a.x / m, a.y / m)
  1058. end
  1059.  
  1060. function vec3(v, z)
  1061.     return Vec3.new(v.x, v.y, z)
  1062. end
  1063.  
  1064. --[[
  1065. To be implemented
  1066. function Vec4(v, z, a)
  1067.     return Vec4.new(v,x, v.y, z, a)
  1068. end]]
  1069.  
  1070.  
  1071.  
  1072. local Prototype = {["type"] = "vec2"}
  1073.  
  1074. local mt = {}
  1075.  
  1076. mt.__index = Prototype
  1077.  
  1078. function mt.__tostring(a)
  1079.     return a.x .. ", " .. a.y
  1080. end
  1081.  
  1082.  
  1083. function new(x, y)
  1084.     t = {["x"] = x, ["y"] = y}
  1085.     setmetatable(t, mt)
  1086.     return t
  1087. end
  1088.  
  1089.  
  1090. Prototype.add = add
  1091. Prototype.div = div
  1092. Prototype.mult = mult
  1093. Prototype.sub = sub
  1094. Prototype.mag = mag
  1095. Prototype.magSquared = magSquared
  1096. Prototype.reverse = reverse
  1097. Prototype.normalize = normalize
  1098. Prototype.vec3 = vec3
  1099. --Prototype.Vec4 = Vec4
  1100.  
  1101. mt.__add = add
  1102. mt.__mul = mult
  1103. mt.__div = div
  1104. mt.__sub = sub
  1105. mt.__unm = reverse]=])
  1106. f.close()
  1107.  
  1108. f = fs.open("CCG/Vec3", "w")
  1109. f.write([=[
  1110. function add(a, b)
  1111.     return new(a.x + b.x, a.y + b.y, a.z + b.z)
  1112. end
  1113.  
  1114. local function sub(a, b)
  1115.     return new(a.x - b.x, a.y - b.y, a.z - b.z)
  1116. end
  1117.  
  1118. function mult(a, b)
  1119.     if type(b) == "number" then
  1120.         return new(a.x * b, a.y * b, a.z * b)
  1121.     elseif type(a) == "number" then
  1122.         return new(b.x * a, b.y * a, b.z * a)
  1123.     elseif type(a) == "table" and type(b) == "table" then
  1124.         if a.type == "vec3" and b.type == "vec3" then
  1125.             return a.x * b.x + a.y * b.y + a.z * b.z
  1126.         elseif a.type == "vec3" and b.type == "mat3" then
  1127.             return new(a.x * b[1][1] +
  1128.             a.y * b[2][1] +
  1129.             a.z * b[3][1],
  1130.                        a.x * b[1][2] +
  1131.                        a.y * b[2][2] +
  1132.                        a.z * b[3][2],
  1133.                        a.x * b[1][3] +
  1134.                        a.y * b[2][3] +
  1135.                        a.z * b[3][3])
  1136.         end
  1137.     end
  1138.     error("cannot multiply vector", 2)
  1139. end
  1140.  
  1141. function div(a, b)
  1142.     if type(b) == "number" then
  1143.         return new(a.x / b, a.y / b, a.z / b)
  1144.     elseif type(a) == "table" and type(b) == "table" then
  1145.         if a.type == "vec3" and b.type == "mat3" then
  1146.             return a * b:inv()
  1147.         end
  1148.     end
  1149.     error("cannot divide vector", 2)
  1150. end
  1151.  
  1152. function mag(a)
  1153.     return math.sqrt(magSquared(a))
  1154. end
  1155.  
  1156. function magSquared(a)
  1157.     return a.x * a.x + a.y * a.y + a.z * a.z
  1158. end
  1159.  
  1160. function reverse(a)
  1161.     return a * -1
  1162. end
  1163.  
  1164. function normalize(a)
  1165.     local m = mag(a)
  1166.     if m == 0 then
  1167.         error("cannot normalize a vector with length 0", 2)
  1168.     end
  1169.     return new(a.x / m, a.y / m, a.z / m)
  1170. end
  1171.  
  1172. function cross(a, b)
  1173.     return new(
  1174.         a.y * b.z - a.z * b.y,
  1175.         a.z * b.x - a.x * b.z,
  1176.         a.x * b.y - a.y - b.x
  1177.     )
  1178. end
  1179.  
  1180. --[[
  1181. To be implemented
  1182. function Vec4(v, a)
  1183.     return Vec4.new(v,x, v.y, v.z, a)
  1184. end]]
  1185.  
  1186.  
  1187. local Prototype = {["type"] = "vec3"}
  1188.  
  1189. local mt = {}
  1190.  
  1191. mt.__index = Prototype
  1192.  
  1193. function mt.__tostring(a)
  1194.     return a.x .. ", " .. a.y .. ", " .. a.z
  1195. end
  1196.  
  1197.  
  1198. function new(x, y, z)
  1199.     t = {["x"] = x, ["y"] = y, ["z"] = z}
  1200.     setmetatable(t, mt)
  1201.     return t
  1202. end
  1203.  
  1204.  
  1205. Prototype.add = add
  1206. Prototype.div = div
  1207. Prototype.mult = mult
  1208. Prototype.sub = sub
  1209. Prototype.mag = mag
  1210. Prototype.magSquared = magSquared
  1211. Prototype.reverse = reverse
  1212. Prototype.normalize = normalize
  1213. Prototype.cross = cross
  1214. --Prototype.Vec4 = Vec4
  1215.  
  1216. mt.__add = add
  1217. mt.__mul = mult
  1218. mt.__div = div
  1219. mt.__sub = sub
  1220. mt.__unm = reverse
  1221. mt.__pow = cross]=])
  1222. f.close()
Advertisement
Add Comment
Please, Sign In to add comment