billysback

Art

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