Advertisement
billysback

triapi

Jun 27th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. function drawLine(x1, y1, x2, y2)
  2.         local pixs = {}
  3.         if nColour then
  4.                         term.setBackgroundColor( nColour )
  5.                 end
  6.                
  7.                 x1 = math.floor(x1)
  8.                 y1 = math.floor(y1)
  9.                 x2 = math.floor(x2)
  10.                 y2 = math.floor(y2)
  11.                
  12.                 if x1 == x2 and y1 == y2 then
  13.                         if pixs[x1] == nil then pixs[x1] = {} end
  14.                         pixs[x1][y1] = true
  15.                         return pixs
  16.                 end
  17.                
  18.                 local minX = math.min( x1, x2 )
  19.                 if minX == x1 then
  20.                         minY = y1
  21.                         maxX = x2
  22.                         maxY = y2
  23.                 else
  24.                         minY = y2
  25.                         maxX = x1
  26.                         maxY = y1
  27.                 end
  28.                        
  29.                 local xDiff = maxX - minX
  30.                 local yDiff = maxY - minY
  31.                                
  32.                 if xDiff > math.abs(yDiff) then
  33.                         local y = minY
  34.                         local dy = yDiff / xDiff
  35.                         for x=minX,maxX do
  36.                                 if pixs[x] == nil then pixs[x] = {} end
  37.                                 pixs[x][math.floor(y + 0.5)] = true
  38.                                 y = y + dy
  39.                         end
  40.                 else
  41.                         local x = minX
  42.                         local dx = xDiff / yDiff
  43.                         if maxY >= minY then
  44.                                 for y=minY,maxY do
  45.                                         if pixs[math.floor(x+0.5)] == nil then pixs[math.floor(x+0.5)] = {} end
  46.                                         pixs[math.floor(x+0.5)][y] = true
  47.                                         x = x + dx
  48.                                 end
  49.                         else
  50.                                 for y=minY,maxY,-1 do
  51.                                         if pixs[math.floor(x+0.5)] == nil then pixs[math.floor(x+0.5)] = {} end
  52.                                         pixs[math.floor(x+0.5)][y] = true
  53.                                         x = x - dx
  54.                                 end
  55.                         end
  56.                 end
  57.                 return pixs
  58. end
  59.  
  60. function getTriangle(p1, p2, p3)
  61.     local tab1 = drawLine(p1[1], p1[2], p2[1], p2[2])
  62.     local tab2 = drawLine(p1[1], p1[2], p3[1], p3[2])
  63.     local tab3 = drawLine(p2[1], p2[2], p3[1], p3[2])
  64.     local tabs = {tab1, tab2, tab3}
  65.     local triangle = {}
  66.     for i=1,#tabs do
  67.         local tab = tabs[i]
  68.         for x,v in pairs(tab) do
  69.             if v ~= nil then
  70.                 if triangle[x] == nil then triangle[x] = {} end
  71.                 for y,r in pairs(v) do
  72.                     triangle[x][y] = r
  73.                 end
  74.             end
  75.         end
  76.     end
  77.     return triangle
  78. end
  79.  
  80. local function getSize(tab)
  81.     local size = 0
  82.     for i,v in pairs(tab) do size = size + 1 end
  83.     return size
  84. end
  85.  
  86. local function getLastIndex(tab)
  87.     local index = 1
  88.     for i,v in pairs(tab) do
  89.         if type(i) == "number" then
  90.             if tonumber(i) > index then index = tonumber(i) end
  91.         end
  92.     end
  93.    
  94.     return index
  95. end
  96.  
  97. local w,h = term.getSize()
  98.  
  99. function fillTriangle(tri)
  100.     term.setBackgroundColor(colors.black)
  101.     term.setTextColor(colors.white)
  102.     local minX, maxX = nil
  103.     local minys = {}
  104.     local ntri = {}
  105.     for x,v in pairs(tri) do
  106.         if minX == nil then
  107.             minX = x
  108.         elseif x < minX then
  109.             minX = x
  110.         end
  111.        
  112.         if maxX == nil then
  113.             maxX = x
  114.         elseif x > maxX then
  115.             maxX = x
  116.         end
  117.        
  118.         if v ~= nil then
  119.             if ntri[x] == nil then ntri[x] = {} end
  120.             local minY = nil
  121.             for y,r in pairs(v) do
  122.                 ntri[x][y] = r
  123.                 if minY == nil then
  124.                     minY = y
  125.                 elseif y < minY then
  126.                     minY = y
  127.                 end
  128.             end
  129.             if minY ~= nil then
  130.                 minys[x] = minY
  131.             end
  132.         end
  133.     end
  134.    
  135.     if minX ~= nil and maxX ~= nil then
  136.         for x=minX, maxX do
  137.             if minys[x] ~= nil then
  138.                 local y = minys[x]
  139.                 if tri[x] ~= nil then
  140.                     if ntri[x] == nil then ntri[x] = {} end
  141.                     if getSize(tri[x]) > 1 then
  142.                         local lasti = getLastIndex(tri[x])
  143.                         for iy=y,lasti do
  144.                             ntri[x][iy] = true
  145.                         end
  146.                     end
  147.                 end
  148.             end
  149.         end
  150.     end
  151.     return ntri
  152. end
  153.  
  154. function fillSquare(p1, p2, p3, p4)
  155.     local tri1 = fillTriangle(getTriangle(p1, p2, p3))
  156.     local tri2 = fillTriangle(getTriangle(p1, p4, p3))
  157.     local tris = {tri1, tri2}
  158.     local square = {}
  159.     for i=1,#tris do
  160.         local tri = tris[i]
  161.         for x,v in pairs(tri) do
  162.             if v ~= nil then
  163.                 if square[x] == nil then square[x] = {} end
  164.                 for y,r in pairs(v) do
  165.                     square[x][y] = true
  166.                 end
  167.             end
  168.         end
  169.     end
  170.     return square
  171. end
  172.  
  173. function drawShape(shape, color, textcolor, text)
  174.     for xt,v in pairs(shape) do
  175.         if v ~= nil and type(v) == "table" then
  176.             for yt,r in pairs(v) do
  177.                 if type(r) == "boolean" and type(yt) == "number" then
  178.                     term.setCursorPos(xt, yt)
  179.                     term.setBackgroundColor(color)
  180.                     term.setTextColor(textcolor)
  181.                     term.write(text)
  182.                 end
  183.             end
  184.         end
  185.     end
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement