Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function drawLine(x1, y1, x2, y2)
- local pixs = {}
- if nColour then
- term.setBackgroundColor( nColour )
- end
- x1 = math.floor(x1)
- y1 = math.floor(y1)
- x2 = math.floor(x2)
- y2 = math.floor(y2)
- if x1 == x2 and y1 == y2 then
- if pixs[x1] == nil then pixs[x1] = {} end
- pixs[x1][y1] = true
- return pixs
- end
- local minX = math.min( x1, x2 )
- if minX == x1 then
- minY = y1
- maxX = x2
- maxY = y2
- else
- minY = y2
- maxX = x1
- maxY = y1
- end
- local xDiff = maxX - minX
- local yDiff = maxY - minY
- if xDiff > math.abs(yDiff) then
- local y = minY
- local dy = yDiff / xDiff
- for x=minX,maxX do
- if pixs[x] == nil then pixs[x] = {} end
- pixs[x][math.floor(y + 0.5)] = true
- y = y + dy
- end
- else
- local x = minX
- local dx = xDiff / yDiff
- if maxY >= minY then
- for y=minY,maxY do
- if pixs[math.floor(x+0.5)] == nil then pixs[math.floor(x+0.5)] = {} end
- pixs[math.floor(x+0.5)][y] = true
- x = x + dx
- end
- else
- for y=minY,maxY,-1 do
- if pixs[math.floor(x+0.5)] == nil then pixs[math.floor(x+0.5)] = {} end
- pixs[math.floor(x+0.5)][y] = true
- x = x - dx
- end
- end
- end
- return pixs
- end
- function getTriangle(p1, p2, p3)
- local tab1 = drawLine(p1[1], p1[2], p2[1], p2[2])
- local tab2 = drawLine(p1[1], p1[2], p3[1], p3[2])
- local tab3 = drawLine(p2[1], p2[2], p3[1], p3[2])
- local tabs = {tab1, tab2, tab3}
- local triangle = {}
- for i=1,#tabs do
- local tab = tabs[i]
- for x,v in pairs(tab) do
- if v ~= nil then
- if triangle[x] == nil then triangle[x] = {} end
- for y,r in pairs(v) do
- triangle[x][y] = r
- end
- end
- end
- end
- return triangle
- end
- local function getSize(tab)
- local size = 0
- for i,v in pairs(tab) do size = size + 1 end
- return size
- end
- local function getLastIndex(tab)
- local index = 1
- for i,v in pairs(tab) do
- if type(i) == "number" then
- if tonumber(i) > index then index = tonumber(i) end
- end
- end
- return index
- end
- local w,h = term.getSize()
- function fillTriangle(tri)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local minX, maxX = nil
- local minys = {}
- local ntri = {}
- for x,v in pairs(tri) do
- if minX == nil then
- minX = x
- elseif x < minX then
- minX = x
- end
- if maxX == nil then
- maxX = x
- elseif x > maxX then
- maxX = x
- end
- if v ~= nil then
- if ntri[x] == nil then ntri[x] = {} end
- local minY = nil
- for y,r in pairs(v) do
- ntri[x][y] = r
- if minY == nil then
- minY = y
- elseif y < minY then
- minY = y
- end
- end
- if minY ~= nil then
- minys[x] = minY
- end
- end
- end
- if minX ~= nil and maxX ~= nil then
- for x=minX, maxX do
- if minys[x] ~= nil then
- local y = minys[x]
- if tri[x] ~= nil then
- if ntri[x] == nil then ntri[x] = {} end
- if getSize(tri[x]) > 1 then
- local lasti = getLastIndex(tri[x])
- for iy=y,lasti do
- ntri[x][iy] = true
- end
- end
- end
- end
- end
- end
- return ntri
- end
- function fillSquare(p1, p2, p3, p4)
- local tri1 = fillTriangle(getTriangle(p1, p2, p3))
- local tri2 = fillTriangle(getTriangle(p1, p4, p3))
- local tris = {tri1, tri2}
- local square = {}
- for i=1,#tris do
- local tri = tris[i]
- for x,v in pairs(tri) do
- if v ~= nil then
- if square[x] == nil then square[x] = {} end
- for y,r in pairs(v) do
- square[x][y] = true
- end
- end
- end
- end
- return square
- end
- function drawShape(shape, color, textcolor, text)
- for xt,v in pairs(shape) do
- if v ~= nil and type(v) == "table" then
- for yt,r in pairs(v) do
- if type(r) == "boolean" and type(yt) == "number" then
- term.setCursorPos(xt, yt)
- term.setBackgroundColor(color)
- term.setTextColor(textcolor)
- term.write(text)
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement