Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local term = require("term")
- local gpu = component.gpu
- local draw = {}
- draw.gpu = gpu
- local function sign(x)
- return (x < 0 and -1) or (x == 0 and 0) or (x > 0 and 1)
- end
- function draw.text(x, y, text, bgcolor, txcolor)
- local defaultBg, defaultTx = gpu.getBackground(), gpu.getForeground()
- gpu.setBackground(bgcolor)
- gpu.setForeground(txcolor)
- term.setCursor(x, y)
- term.write(tostring(text))
- gpu.setBackground(defaultBg)
- gpu.setForeground(defaultTx)
- end
- function draw.pixel(x, y, color)
- gpu.setBackground(color)
- gpu.set(x, y, " ")
- gpu.setBackground(0)
- end
- function draw.spixel(x, y, color)
- draw.rect(x, y, 2, 1, color)
- end
- function draw.rect(x, y, l, w, color)
- gpu.setBackground(color)
- gpu.fill(x, y, l, w, " ")
- gpu.setBackground(0)
- end
- function draw.line(p1, p2, color)
- local x0, x1, y0, y1 = p1.x, p2.x, p1.y, p2.y
- local xStart, xEnd, yStart, yEnd, isReversed, dx, dy = x0, x1, y0, y1, false, math.abs(x1 - x0), math.abs(y1 - y0)
- if dx < dy then
- xStart, xEnd, yStart, yEnd, isReversed, dx, dy = y0, y1, x0, x1, true, dy, dx
- end
- if yStart > yEnd then
- yStart, yEnd = yEnd, yStart
- xStart, xEnd = xEnd, xStart
- end
- local y, yc, ytI = yStart, 1, dx / dy
- local yt = ytI
- for x = xStart, xEnd, sign(xEnd - xStart) do
- if isReversed then
- draw.pixel(y, x, color)
- else
- draw.pixel(x, y, color)
- end
- yc = yc + 1
- if yc > yt then
- y, yt = y + 1, yt + ytI
- end
- end
- end
- function draw.poly(points, color)
- for i, point in ipairs(points) do
- if i < #points then
- draw.line(point, points[i + 1], color)
- else
- draw.line(point, points[1], color)
- end
- end
- end
- return draw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement