Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- drawSurfaces = {
- [1] = {},
- [2] = {},
- }
- function packXY(x,y)
- return {x,y}
- end
- function drawPoint(x,y,color)
- if type(color) == "number" then
- drawSurfaces[1][x][y] = color
- elseif type(color) == "table" then
- drawSurfaces[2][x][y] = color
- else
- error("invalid color parameter")
- end
- end
- function resetScreen()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- function resetSurface()
- local maxX, maxY = term.getSize()
- for i=1, maxX do
- drawSurfaces[1][i] = {}
- drawSurfaces[2][i] = {}
- end
- end
- function update()
- resetScreen()
- local maxX, maxY = term.getSize()
- for x=1, maxX do
- for y=1, maxY do
- term.setCursorPos(x,y)
- if drawSurfaces[1][x][y] then
- term.setBackgroundColor(drawSurfaces[1][x][y])
- else
- term.setBackgroundColor(colors.black)
- end
- if drawSurfaces[2][x][y] then
- term.setTextColor(drawSurfaces[2][x][y][1])
- term.write(drawSurfaces[2][x][y][2])
- else
- term.setTextColor(colors.white)
- term.write(" ")
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function findAngle(startCoords, endCoords)
- local dX = endCoords[1] - startCoords[1]
- local dY = endCoords[2] - startCoords[2]
- return math.atan2(dY, dX) * 180 / math.pi
- end
- --[[
- function drawLine(startCoords, endCoords, color)
- local dX = endCoords[1] - startCoords[1]
- local dY = endCoords[2] - startCoords[2]
- if dX ~= 0 then -- not vertical
- local dErr = math.abs(dY / dX)
- local err = 0
- local slope = dY/dX
- local y = startCoords[1]
- --local angle = math.atan2(dY, dX) * 180 / math.pi
- --local y_int = (slope * startCoords[1]) + startCoords[2]
- for x=startCoords[1], endCoords[1] do
- y = slope*(x-startCoords[1]) + startCoords[2]
- if y - math.floor(y) >= 0.5 then
- y = math.ceil(y)
- else
- y = math.floor(y)
- end
- drawPoint(x,y,color)
- err = err + dErr
- if err >= 0.5 then
- y = y+1
- err = err - 1
- end
- end
- else
- local x = startCoords[1]
- for y=startCoords[2], endCoords[2] do
- drawPoint(x,y,color)
- end
- end
- end
- ]]
- function drawLine(startCoords, endCoords, color)
- local dX = math.abs(endCoords[1] - startCoords[1])
- local dY = math.abs(endCoords[2] - startCoords[2])*-1
- local sX = 0
- local sY = 0
- local x = startCoords[1]
- local y = startCoords[2]
- local err = dX + dY
- local e2 = 0
- if startCoords[1] < endCoords[1] then
- sX = 1
- else
- sX = -1
- end
- if startCoords[2] < endCoords[2] then
- sY = 1
- else
- sY = -1
- end
- while true do
- drawPoint(x,y,color)
- if x == endCoords[1] and y == endCoords[2] then
- break
- end
- if (2*err) >= dY then
- err = err+dY
- x = x+ sX
- end
- if (2*err) <= dX then
- err = err+dX
- y = y + sY
- end
- end
- end
- function drawRect(startCoords, endCoords, color, fill)
- if fill then
- local oldX, oldY = term.getCursorPos()
- local XLen = 0
- local YLen = 0
- if startCoords[1] > endCoords[1] then
- XLen = startCoords[1] - endCoords[1]
- else
- XLen = endCoords[1] - startCoords[1]
- end
- if startCoords[2] > endCoords[2] then
- YLen = startCoords[2] - endCoords[2]
- else
- YLen = endCoords[2] - startCoords[2]
- end
- if startCoords[2] > endCoords[2] then
- for y=startCoords[2], endCoords[2], -1 do
- if startCoords[1] > endCoords[1] then
- for x=startCoords[1], endCoords[1], -1 do
- drawPoint(x,y,color)
- end
- else
- for x=startCoords[1], endCoords[1] do
- drawPoint(x,y,color)
- end
- end
- end
- else
- for y=startCoords[2], endCoords[2]do
- if startCoords[1] > endCoords[1] then
- for x=startCoords[1], endCoords[1], -1 do
- drawPoint(x,y,color)
- end
- else
- for x=startCoords[1], endCoords[1] do
- drawPoint(x,y,color)
- end
- end
- end
- end
- else
- local dX = endCoords[1] - startCoords[1]
- local dY = endCoords[2] - startCoords[2]
- local c1 = {endCoords[1], startCoords[2]}
- local c2 = {startCoords[1], endCoords[2]}
- drawLine(startCoords, c1, color)
- drawLine(startCoords, c2, color)
- drawLine(endCoords, c1, color)
- drawLine(endCoords, c2, color)
- end
- end
- --[[function drawCircle(centerCoords, radius, color)
- local r2 = radius^2
- local f = 1-radius
- local ddF_x = 1
- local ddF_y = -2*radius
- local x = 0
- local y = radius
- drawPoint(centerCoords[1],centerCoords[2]+radius],color)
- drawPoint(centerCoords[1],centerCoords[2]-radius],color)
- drawPoint(centerCoords[1]+radius,centerCoords[2],color)
- drawPoint(centerCoords[1]-radius,centerCoords[2],color)
- while x < y do
- if f >= 0 then
- y = y - 1
- ddF_y = ddF_y+2
- f = f + ddF_y
- end
- x = x+1
- ddF_x = ddF_x+2
- f = f + ddF_x
- drawPoint(centerCoords[1]+x,centerCoords[2]+y,color)
- drawPoint(centerCoords[1]-x,centerCoords[2]+y,color)
- drawPoint(centerCoords[1]+x,centerCoords[2]-y,color)
- drawPoint(centerCoords[1]-x,centerCoords[2]-y,color)
- drawPoint(centerCoords[1]+x,centerCoords[2]+y,color)
- drawPoint(centerCoords[1]-x,centerCoords[2]+y,color)
- drawPoint(centerCoords[1]+x,centerCoords[2]-y,color)
- drawPoint(centerCoords[1]-x,centerCoords[2]-y,color)
- end
- -- Thanks, Wikipedia! (algorithm adapted from C code found in Wikipedia)
- end]]
- function drawCircle(centerCoords, radius, color)
- local x = radius*-1
- local y=0
- local err = 2-2*radius
- drawPoint(centerCoords[1], centerCoords[2]+radius, color)
- drawPoint(centerCoords[1], centerCoords[2]-radius, color)
- while x<0 do
- drawPoint(centerCoords[1]-x, centerCoords[2]+y, color)
- drawPoint(centerCoords[1]-x, centerCoords[2]-y, color)
- drawPoint(centerCoords[1]+x, centerCoords[2]-y, color)
- drawPoint(centerCoords[1]+x, centerCoords[2]+y, color)
- radius = err
- if radius > x then
- x = x+1
- err = err + (x*2)+1
- end
- if radius <= y then
- y = y+1
- err = err+y*2+1
- end
- end
- -- Thanks, Unknown Person! (algorithm adapted from C code found from the WayBack Machine)
- end
- function drawPolygon(pointList, color)
- for i=1, #pointList-1 do
- drawLine(pointList[i], pointList[i+1], color)
- end
- drawLine(pointList[1], pointList[#pointList], color)
- end
- function writeText(text, startCoords, color)
- local i = 0
- for letter in string.gmatch(text, "(.)") do
- drawPoint(i+startCoords[1], startCoords[2], {color, letter})
- i = i+1
- end
- end
- local function floodFillRecursive_BG(location, original_color, replacement_color)
- if drawSurfaces[1][location[1]][location[2]] ~= original_color then
- return
- else
- drawSurfaces[1][location[1]][location[2]] = replacement_color
- end
- floodFillRecursive_BG({location[1]+1, location[2]}, original_color, replacement_color)
- floodFillRecursive_BG({location[1], location[2]+1}, original_color, replacement_color)
- floodFillRecursive_BG({location[1]-1, location[2]}, original_color, replacement_color)
- floodFillRecursive_BG({location[1], location[2]-1}, original_color, replacement_color)
- end
- local function floodFillRecursive_FG(location, original_color, replacement_color)
- if drawSurfaces[2][location[1]][location[2]] ~= original_color then
- return
- else
- drawSurfaces[2][location[1]][location[2]] = replacement_color
- end
- floodFillRecursive_FG({location[1]+1, location[2]}, original_color, replacement_color)
- floodFillRecursive_FG({location[1], location[2]+1}, original_color, replacement_color)
- floodFillRecursive_FG({location[1]-1, location[2]}, original_color, replacement_color)
- floodFillRecursive_FG({location[1], location[2]-1}, original_color, replacement_color)
- end
- function floodFill(startCoords, color)
- local colorFG = drawSurfaces[2][startCoords[1]][startCoords[2]]
- local colorBG = drawSurfaces[1][startCoords[1]][startCoords[2]]
- if type(color) == "table" then
- floodFillRecursive_FG(startCoords, colorFG, color)
- elseif type(color) == "number" then
- floodFillRecursive_BG(startCoords, colorBG, color)
- end
- end
- resetSurface()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement