Advertisement
Tatantyler

Graphics API

Nov 21st, 2012
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.84 KB | None | 0 0
  1. drawSurfaces = {
  2.     [1] = {},
  3.     [2] = {},
  4. }
  5.  
  6. function packXY(x,y)
  7.     return {x,y}
  8. end
  9.  
  10. function drawPoint(x,y,color)
  11.     if type(color) == "number" then
  12.         drawSurfaces[1][x][y] = color
  13.     elseif type(color) == "table" then
  14.         drawSurfaces[2][x][y] = color
  15.     else
  16.         error("invalid color parameter")
  17.     end
  18. end
  19.  
  20. function resetScreen()
  21.     term.setTextColor(colors.white)
  22.     term.setBackgroundColor(colors.black)
  23.     term.clear()
  24.     term.setCursorPos(1,1)
  25. end
  26.  
  27. function resetSurface()
  28.     local maxX, maxY = term.getSize()
  29.     for i=1, maxX do
  30.         drawSurfaces[1][i] = {}
  31.         drawSurfaces[2][i] = {}
  32.     end
  33. end
  34.  
  35. function update()
  36.     resetScreen()
  37.     local maxX, maxY = term.getSize()
  38.     for x=1, maxX do
  39.         for y=1, maxY do
  40.             term.setCursorPos(x,y)
  41.             if drawSurfaces[1][x][y] then
  42.                 term.setBackgroundColor(drawSurfaces[1][x][y])
  43.             else
  44.                 term.setBackgroundColor(colors.black)
  45.             end
  46.             if drawSurfaces[2][x][y] then
  47.                 term.setTextColor(drawSurfaces[2][x][y][1])
  48.                 term.write(drawSurfaces[2][x][y][2])
  49.             else
  50.                 term.setTextColor(colors.white)
  51.                 term.write(" ")
  52.             end
  53.         end
  54.     end
  55.     term.setBackgroundColor(colors.black)
  56.     term.setTextColor(colors.white)
  57. end
  58.  
  59. function findAngle(startCoords, endCoords)
  60.     local dX = endCoords[1] - startCoords[1]
  61.     local dY = endCoords[2] - startCoords[2]
  62.     return math.atan2(dY, dX) * 180 / math.pi
  63. end
  64.  
  65. --[[
  66. function drawLine(startCoords, endCoords, color)
  67.     local dX = endCoords[1] - startCoords[1]
  68.     local dY = endCoords[2] - startCoords[2]
  69.     if dX ~= 0 then -- not vertical
  70.         local dErr = math.abs(dY / dX)
  71.         local err = 0
  72.         local slope = dY/dX
  73.         local y = startCoords[1]
  74.         --local angle = math.atan2(dY, dX) * 180 / math.pi
  75.         --local y_int = (slope * startCoords[1]) + startCoords[2]
  76.         for x=startCoords[1], endCoords[1] do
  77.             y = slope*(x-startCoords[1]) + startCoords[2]
  78.             if y - math.floor(y) >= 0.5 then
  79.                 y = math.ceil(y)
  80.             else
  81.                 y = math.floor(y)
  82.             end
  83.             drawPoint(x,y,color)
  84.             err = err + dErr
  85.             if err >= 0.5 then
  86.                 y = y+1
  87.                 err = err - 1
  88.             end
  89.         end
  90.     else
  91.         local x = startCoords[1]
  92.         for y=startCoords[2], endCoords[2] do
  93.             drawPoint(x,y,color)
  94.         end
  95.     end
  96. end
  97. ]]
  98.  
  99. function drawLine(startCoords, endCoords, color)
  100.     local dX = math.abs(endCoords[1] - startCoords[1])
  101.     local dY = math.abs(endCoords[2] - startCoords[2])*-1
  102.     local sX = 0
  103.     local sY = 0
  104.     local x = startCoords[1]
  105.     local y = startCoords[2]
  106.     local err = dX + dY
  107.     local e2 = 0
  108.     if startCoords[1] < endCoords[1] then
  109.         sX = 1
  110.     else
  111.         sX = -1
  112.     end
  113.     if startCoords[2] < endCoords[2] then
  114.         sY = 1
  115.     else
  116.         sY = -1
  117.     end
  118.     while true do
  119.         drawPoint(x,y,color)
  120.         if x == endCoords[1] and y == endCoords[2] then
  121.             break
  122.         end
  123.         if (2*err) >= dY then
  124.             err = err+dY
  125.             x = x+ sX
  126.         end
  127.         if (2*err) <= dX then
  128.             err = err+dX
  129.             y = y + sY
  130.         end
  131.     end
  132. end
  133.  
  134. function drawRect(startCoords, endCoords, color, fill)
  135.     if fill then
  136.         local oldX, oldY = term.getCursorPos()
  137.         local XLen = 0
  138.         local YLen = 0
  139.         if startCoords[1] > endCoords[1] then
  140.             XLen = startCoords[1] - endCoords[1]
  141.         else
  142.             XLen = endCoords[1] - startCoords[1]
  143.         end
  144.         if startCoords[2] > endCoords[2] then
  145.             YLen = startCoords[2] - endCoords[2]
  146.         else
  147.             YLen = endCoords[2] - startCoords[2]
  148.         end
  149.         if startCoords[2] > endCoords[2] then
  150.             for y=startCoords[2], endCoords[2], -1 do
  151.                 if startCoords[1] > endCoords[1] then
  152.                     for x=startCoords[1], endCoords[1], -1 do
  153.                         drawPoint(x,y,color)
  154.                     end
  155.                 else
  156.                     for x=startCoords[1], endCoords[1] do
  157.                         drawPoint(x,y,color)
  158.                     end
  159.                 end
  160.             end
  161.         else
  162.             for y=startCoords[2], endCoords[2]do
  163.                 if startCoords[1] > endCoords[1] then
  164.                     for x=startCoords[1], endCoords[1], -1 do
  165.                         drawPoint(x,y,color)
  166.                     end
  167.                 else
  168.                     for x=startCoords[1], endCoords[1] do
  169.                         drawPoint(x,y,color)
  170.                     end
  171.                 end
  172.             end
  173.         end
  174.     else
  175.         local dX = endCoords[1] - startCoords[1]
  176.         local dY = endCoords[2] - startCoords[2]
  177.         local c1 = {endCoords[1], startCoords[2]}
  178.         local c2 = {startCoords[1], endCoords[2]}
  179.         drawLine(startCoords, c1, color)
  180.         drawLine(startCoords, c2, color)
  181.         drawLine(endCoords, c1, color)
  182.         drawLine(endCoords, c2, color)
  183.     end
  184. end
  185.  
  186. --[[function drawCircle(centerCoords, radius, color)
  187.     local r2 = radius^2
  188.     local f = 1-radius
  189.     local ddF_x = 1
  190.     local ddF_y = -2*radius
  191.     local x = 0
  192.     local y = radius
  193.     drawPoint(centerCoords[1],centerCoords[2]+radius],color)
  194.     drawPoint(centerCoords[1],centerCoords[2]-radius],color)
  195.     drawPoint(centerCoords[1]+radius,centerCoords[2],color)
  196.     drawPoint(centerCoords[1]-radius,centerCoords[2],color)
  197.    
  198.     while x < y do
  199.         if f >= 0 then
  200.             y = y - 1
  201.             ddF_y = ddF_y+2
  202.             f = f + ddF_y
  203.         end
  204.         x = x+1
  205.         ddF_x = ddF_x+2
  206.         f = f + ddF_x
  207.         drawPoint(centerCoords[1]+x,centerCoords[2]+y,color)
  208.         drawPoint(centerCoords[1]-x,centerCoords[2]+y,color)
  209.         drawPoint(centerCoords[1]+x,centerCoords[2]-y,color)
  210.         drawPoint(centerCoords[1]-x,centerCoords[2]-y,color)
  211.         drawPoint(centerCoords[1]+x,centerCoords[2]+y,color)
  212.         drawPoint(centerCoords[1]-x,centerCoords[2]+y,color)
  213.         drawPoint(centerCoords[1]+x,centerCoords[2]-y,color)
  214.         drawPoint(centerCoords[1]-x,centerCoords[2]-y,color)
  215.     end
  216.     -- Thanks, Wikipedia! (algorithm adapted from C code found in Wikipedia)
  217. end]]
  218.  
  219. function drawCircle(centerCoords, radius, color)
  220.     local x = radius*-1
  221.     local y=0
  222.     local err = 2-2*radius
  223.     drawPoint(centerCoords[1], centerCoords[2]+radius, color)
  224.     drawPoint(centerCoords[1], centerCoords[2]-radius, color)
  225.     while x<0 do
  226.         drawPoint(centerCoords[1]-x, centerCoords[2]+y, color)
  227.         drawPoint(centerCoords[1]-x, centerCoords[2]-y, color)
  228.         drawPoint(centerCoords[1]+x, centerCoords[2]-y, color)
  229.         drawPoint(centerCoords[1]+x, centerCoords[2]+y, color)
  230.         radius = err
  231.         if radius > x then
  232.             x = x+1
  233.             err = err + (x*2)+1
  234.         end
  235.         if radius <= y then
  236.             y = y+1
  237.             err = err+y*2+1
  238.         end
  239.     end
  240.     -- Thanks, Unknown Person! (algorithm adapted from C code found from the WayBack Machine)
  241. end
  242.  
  243.  
  244. function drawPolygon(pointList, color)
  245.     for i=1, #pointList-1 do
  246.         drawLine(pointList[i], pointList[i+1], color)
  247.     end
  248.     drawLine(pointList[1], pointList[#pointList], color)
  249. end
  250.  
  251. function writeText(text, startCoords, color)
  252.     local i = 0
  253.     for letter in string.gmatch(text, "(.)") do
  254.         drawPoint(i+startCoords[1], startCoords[2], {color, letter})
  255.         i = i+1
  256.     end
  257. end
  258.  
  259. local function floodFillRecursive_BG(location, original_color, replacement_color)
  260.     if drawSurfaces[1][location[1]][location[2]] ~= original_color then
  261.         return
  262.     else
  263.         drawSurfaces[1][location[1]][location[2]] = replacement_color
  264.     end
  265.     floodFillRecursive_BG({location[1]+1, location[2]}, original_color, replacement_color)
  266.     floodFillRecursive_BG({location[1], location[2]+1}, original_color, replacement_color)
  267.     floodFillRecursive_BG({location[1]-1, location[2]}, original_color, replacement_color)
  268.     floodFillRecursive_BG({location[1], location[2]-1}, original_color, replacement_color)
  269. end
  270.  
  271. local function floodFillRecursive_FG(location, original_color, replacement_color)
  272.     if drawSurfaces[2][location[1]][location[2]] ~= original_color then
  273.         return
  274.     else
  275.         drawSurfaces[2][location[1]][location[2]] = replacement_color
  276.     end
  277.     floodFillRecursive_FG({location[1]+1, location[2]}, original_color, replacement_color)
  278.     floodFillRecursive_FG({location[1], location[2]+1}, original_color, replacement_color)
  279.     floodFillRecursive_FG({location[1]-1, location[2]}, original_color, replacement_color)
  280.     floodFillRecursive_FG({location[1], location[2]-1}, original_color, replacement_color)
  281. end
  282.  
  283. function floodFill(startCoords, color)
  284.     local colorFG = drawSurfaces[2][startCoords[1]][startCoords[2]]
  285.     local colorBG = drawSurfaces[1][startCoords[1]][startCoords[2]]
  286.     if type(color) == "table" then
  287.         floodFillRecursive_FG(startCoords, colorFG, color)
  288.     elseif type(color) == "number" then
  289.         floodFillRecursive_BG(startCoords, colorBG, color)
  290.     end
  291. end
  292.  
  293. resetSurface()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement