Advertisement
HappySunChild

DrawLibrary-OpenComputers

Aug 7th, 2022 (edited)
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local gpu = component.gpu
  4.  
  5. local draw = {}
  6. draw.gpu = gpu
  7.  
  8. local function sign(x)
  9.     return (x < 0 and -1) or (x == 0 and 0) or (x > 0 and 1)
  10. end
  11.  
  12. function draw.text(x, y, text, bgcolor, txcolor)
  13.     local defaultBg, defaultTx = gpu.getBackground(), gpu.getForeground()
  14.  
  15.     gpu.setBackground(bgcolor)
  16.     gpu.setForeground(txcolor)
  17.  
  18.     term.setCursor(x, y)
  19.     term.write(tostring(text))
  20.  
  21.     gpu.setBackground(defaultBg)
  22.     gpu.setForeground(defaultTx)
  23. end
  24.  
  25. function draw.pixel(x, y, color)
  26.     gpu.setBackground(color)
  27.     gpu.set(x, y, " ")
  28.     gpu.setBackground(0)
  29. end
  30.  
  31. function draw.spixel(x, y, color)
  32.     draw.rect(x, y, 2, 1, color)
  33. end
  34.  
  35. function draw.rect(x, y, l, w, color)
  36.     gpu.setBackground(color)
  37.     gpu.fill(x, y, l, w, " ")
  38.     gpu.setBackground(0)
  39. end
  40.  
  41. function draw.line(p1, p2, color)
  42.     local x0, x1, y0, y1 = p1.x, p2.x, p1.y, p2.y
  43.     local xStart, xEnd, yStart, yEnd, isReversed, dx, dy = x0, x1, y0, y1, false, math.abs(x1 - x0), math.abs(y1 - y0)
  44.  
  45.     if dx < dy then
  46.         xStart, xEnd, yStart, yEnd, isReversed, dx, dy = y0, y1, x0, x1, true, dy, dx
  47.     end
  48.  
  49.     if yStart > yEnd then
  50.         yStart, yEnd = yEnd, yStart
  51.         xStart, xEnd = xEnd, xStart
  52.     end
  53.  
  54.     local y, yc, ytI = yStart, 1, dx / dy
  55.     local yt = ytI
  56.  
  57.     for x = xStart, xEnd, sign(xEnd - xStart) do
  58.         if isReversed then
  59.             draw.pixel(y, x, color)
  60.         else
  61.             draw.pixel(x, y, color)
  62.         end
  63.  
  64.         yc = yc + 1
  65.  
  66.         if yc > yt then
  67.             y, yt = y + 1, yt + ytI
  68.         end
  69.     end
  70. end
  71.  
  72. function draw.poly(points, color)
  73.     for i, point in ipairs(points) do
  74.         if i < #points then
  75.             draw.line(point, points[i + 1], color)
  76.         else
  77.             draw.line(point, points[1], color)
  78.         end
  79.     end
  80. end
  81.  
  82. return draw
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement