Advertisement
billysback

Art

Nov 9th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.17 KB | None | 0 0
  1. local pixels = {}
  2.  
  3. function getPixelIndex(pixel)
  4.     local index = -1
  5.     for i=1,#pixels do
  6.         if pixels[i] == pixel then index = i end
  7.     end
  8.     return index
  9. end
  10.  
  11. function clear()
  12.     local oldx, oldy = term.getCursorPos()
  13.     local width, height = term.getSize()
  14.     term.setBackgroundColor(colors.black)
  15.     for x=1,width do
  16.         for y=1,height do
  17.             term.setCursorPos(x,y)
  18.             term.write(" ")
  19.             term.setTextColor(colors.white)
  20.         end
  21.     end
  22.     term.setCursorPos(oldx, oldy)
  23.     pixels = {}
  24. end
  25.  
  26. function getPixelByLoc(x, y)
  27.     local index = -1
  28.     for i=1,#pixels do
  29.         local pixel = pixels[i]
  30.         if pixel[1] == x and pixel[2] == y then
  31.             index = i
  32.         end
  33.     end
  34.     return index
  35. end
  36.  
  37. function setPixel(x, y, bcolor, tcolor, ch, id, index)
  38.     local pixel ={}
  39.     if id ~= nil and id > 0 then
  40.         pixel ={id, x, y, bcolor, tcolor, ch}
  41.     else
  42.         pixel ={#pixels + 1, x, y, bcolor, tcolor, ch}
  43.     end
  44.     if index == nil then
  45.         index = getPixelIndex(pixel)
  46.         if index == -1 then
  47.             pixels[#pixels + 1] = pixel
  48.         else
  49.             pixels[index] = pixel
  50.         end
  51.     else
  52.         pixels[index] = pixel
  53.     end
  54.     return pixel
  55. end
  56.  
  57. function setPixelByData(x, y, data)
  58.     setPixel(x, y, data[1], data[2], data[3], data[4])
  59. end
  60.  
  61. function getPixel(pixel)
  62.     local id = pixel.getID()
  63.     local result = nil
  64.     for i=1,#pixels do
  65.         local pid = pixels[i][1]
  66.         if pid > 0 and pid == id then
  67.             result = pixels[i]
  68.         end
  69.     end
  70.     return result
  71. end
  72.  
  73. function getPixelIndex(pixel)
  74.     local index = -1
  75.     for i=1,#pixels do
  76.         if pixels[i] == pixel then index = i end
  77.     end
  78.     return index
  79. end
  80.  
  81. function createPixelByLoc(px, py)
  82.     local pixel = getPixelByLox(px, py)
  83.     local pix = createPixel(px, py, pixel[4], pixel[5], pixel[6])
  84.     return pix
  85. end
  86.  
  87.  
  88. function drawLine(x1, y1, x2, y2, data)
  89.         local pixs = {}
  90.         local xdif = x1-x2
  91.         local ydif = y1-y2
  92.         local grad = ydif/xdif
  93.         local curpos = 0
  94.         for i=1,ydif,1 do
  95.                 if grad <= 1 then
  96.                         pixs[#pixs + 1] = setPixelByData(x1+curpos, i, data)
  97.                 else
  98.                         for j=1,tonumber(grad),1 do
  99.                                 pixs[#pixs + 1] = setPixelByData(x1+curpos+j, i, data)
  100.                         end
  101.                 end
  102.                 curpos = curpos + grad
  103.         end
  104.         return pixs
  105. end
  106.  
  107. function drawLineP(pos1, pos2, data)
  108.     local pixs = {}
  109.     local pix1 = drawLine(pos1[0], pos1[1], pos2[0], pos2[1], data)
  110.     pixs = pix1
  111.     return pixs
  112. end
  113.  
  114. function drawSquare(corner1, corner2, corner3, corner4, data)
  115.         local pixs = {}
  116.         local pix1 = drawLine(corner1, corner2, data)
  117.         local pix2 = drawLine(corner2, corner3, data)
  118.         local pix3 = drawLine(corner3, corner4, data)
  119.         local pix4 = drawline(corner4, corner1, data)
  120.         for i=1,#pix1 do pixs[#pixs + 1] = pix1[i] end
  121.         for i=1,#pix2 do pixs[#pixs + 1] = pix2[i] end
  122.         for i=1,#pix3 do pixs[#pixs + 1] = pix3[i] end
  123.         for i=1,#pix4 do pixs[#pixs + 1] = pix4[i] end
  124.         return pixs
  125. end
  126.  
  127. function drawCircle(center, radius, data, fill)
  128.         -- (x-a)^2 + (y-B)^2 = r^2 (formula, where r is radius, x and y are coordinates and a and b is center)
  129.         local pixs = {}
  130.         local rads = radius^2
  131.         local topright = {center[0]-radius, center[1]+radius}
  132.         local bottomleft = {center[0]+radius, center[1]-radius}
  133.         for cx=topright[0],bottomleft[0],1 do
  134.                 for cy=topright[1],bottomleft[1],1 do
  135.                         local xb = (cx-center[0])^2
  136.                         local yb = (cy-center[1])^2
  137.                         if (xb+yb) == rads then
  138.                                 pixs[#pixs + 1] = setPixelByData(cx, cy, data)
  139.                         elseif fill and (xb+yb) <= rads then
  140.                                 pixs[#pixs + 1] = setPixelByData(cx,  cy, data)
  141.                         end
  142.                 end
  143.         end
  144.         return pixs
  145. end
  146.  
  147. function drawPolygon(points, tag)
  148.     local pixs = {}
  149.         for i,v in ipairs(points) do
  150.                 local precorner = nil
  151.                 if i == 1 then
  152.                         precorner = points[#points]
  153.                 else
  154.                         precorner = points[i-1]
  155.                 end
  156.                 local pix = drawLine(v, precorner, data)
  157.                 for i=1,#pix do pixs[#pixs + 1] = pix[i] end
  158.         end
  159.     return pixs
  160. end
  161.  
  162.  
  163.  
  164. function createPixelArray(pixs)
  165.     local cpixs = {}
  166.     for i=1,#pixs do
  167.         local pixel = pixs[i]
  168.         cpixs[#cpixs + 1] = createPixel(pixel[2], pixel[3], pixel[4], pixel[5], pixel[6])
  169.     end
  170.     local parray = {
  171.         pixs = cpixs,
  172.         getPixels = function(self)
  173.             return self.pixs
  174.         end,
  175.         getPixel = function(self, id)
  176.             for i=1,#self.pixs do
  177.                 local pixel = self.pixs[i]
  178.                 if pixel.id == id then
  179.                     return pixel
  180.                 end
  181.             end
  182.             return nil
  183.         end,
  184.         getPixelByLoc = function(self, x, y)
  185.             for i=1,#self.pixs do
  186.                 local pixel = self.pixs[i]
  187.                 if pixel.x == x and pixel.y == y then
  188.                     return pixel
  189.                 end
  190.             end
  191.             return nil
  192.         end,
  193.         draw = function(self)
  194.             for i=1,#self.pixs do
  195.                 local pixel = self.pixs[i]
  196.                 pixel:draw()
  197.             end
  198.         end,
  199.         updatePixels = function(self)
  200.             for i=1,#self.pixs do
  201.                 local pixel = self.pixs[i]
  202.                 pixel:updatePixels()
  203.             end
  204.         end,
  205.         merge = function(self, pixelarray)
  206.             for i=1,#pixelarray.pixs do
  207.                 self.pixs[#self.pixs + 1] = pixelarray.pixs[i]
  208.             end
  209.         end,
  210.         addPixels = function(self, apixs)
  211.             for i=1,#apixs do
  212.                 self.pixs[#self.pixs + 1] = apixs[i]
  213.             end
  214.         end,
  215.     }
  216.     return parray
  217. end
  218.  
  219. function createPixel(px, py, pbcolor, ptcolor, pcharacter)
  220.     local pixel = {
  221.         id = #pixels + 1,
  222.         x = px,
  223.         y = py,
  224.         background_color = pbcolor,
  225.         text_color = ptcolor,
  226.         character = pcharacter,
  227.         index,
  228.        
  229.         getID = function(self)
  230.             return self.id
  231.         end,
  232.         getX = function(self)
  233.             return self.x
  234.         end,
  235.         getY = function(self)
  236.             return self.y
  237.         end,
  238.         getBackgroundColor = function(self)
  239.             return self.background_color
  240.         end,
  241.         getTextColor = function(self)
  242.             return self.text_color
  243.         end,
  244.         getChar = function(self)
  245.             return self.charachter
  246.         end,
  247.         setX = function(self, nx)
  248.             self.x = nx
  249.         end,
  250.         setY = function(self, ny)
  251.             self.y = ny
  252.         end,
  253.         setBackgroundColor = function(self, ncolor)
  254.             self.background_color = ncolor
  255.         end,
  256.         setTextColor = function(self, ncolor)
  257.             self.text_color = ncolor
  258.         end,
  259.         setChar = function(self, ncharacter)
  260.             self.character = ncharacter
  261.         end,
  262.         updatePixel = function(self)
  263.             if self.index == nil then
  264.                 self.index = getPixelIndex(setPixel(self.x, self.y, self.background_color, self.text_color, self.character, self.id, nil))
  265.             else
  266.                 setPixel(self.x, self.y, self.background_color, self.text_color, self.character, self.id, self.index)
  267.             end
  268.         end,
  269.         draw = function(self)
  270.            
  271.             --oldx, oldy = term.getCursorPos()
  272.             --term.setCursorPos(oldx, oldy)
  273.             term.setCursorPos(self.x, self.y)
  274.             if self.text_color ~= nil then
  275.                 term.setTextColor(self.text_color)
  276.             end
  277.             term.write(self.character)
  278.             if self.background_color ~= nil then
  279.                 term.setBackgroundColor(self.background_color)
  280.             end
  281.             if self.text_color ~= nil then
  282.                 term.setTextColor(self.text_color)
  283.             end
  284.             --term.setCursorPos(oldx, oldy)
  285.             self:updatePixel()
  286.         end,
  287.     }
  288.     return pixel
  289. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement