Advertisement
remie92

SketchLib

Jul 5th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. local lib={}
  2.  
  3. function lib.testPrint()
  4.     print("SketchLib testPrint function called")
  5. end
  6.  
  7.  
  8. lib.screen = nil
  9. lib.yRatio=8/11
  10.  
  11. function lib.setMonitor(monitor)
  12.  
  13.     if monitor == nil then
  14.         error("Monitor cannot be nil")
  15.     end
  16.     scale=0.5
  17.     lib.screen = monitor
  18.     lib.screen.setTextScale(math.max(math.min(scale,5),0.5))
  19.     -- Clear the monitor
  20.     lib.background(colors.gray)
  21.    
  22.     local width, height = lib.screen.getSize()
  23.     print("Monitor size: " .. width .. "x" .. height/lib.yRatio)
  24. end
  25.  
  26. function lib.testDraw()
  27.     if lib.screen == nil then
  28.         error("Monitor not set. Call setMonitor first.")
  29.     end
  30.  
  31.     lib.background(colors.gray)
  32.  
  33.     lib.rect(2,3,20,20,colors.red)
  34. end
  35.  
  36. function lib.background(col)
  37.     lib.screen.setBackgroundColor(col)
  38.     lib.screen.clear()
  39. end
  40.  
  41. lib.strokeWeight=1
  42. lib.strokeColor=colors.black
  43. lib.doStroke=true
  44. lib.doFill=true
  45. lib.fillColor=colors.white
  46.  
  47.  
  48. function lib.stroke(col)
  49.     lib.doStroke=true
  50.     lib.strokeColor = col
  51. end
  52.  
  53. function lib.noStroke()
  54.     lib.doStroke=false
  55. end
  56.  
  57. function lib.setStrokeWeight(weight)
  58.     lib.strokeWeight = weight
  59. end
  60.  
  61. function lib.noFill()
  62.     lib.doFill=false
  63. end
  64.  
  65. function lib.fill(col)
  66.     lib.doFill=true
  67.     lib.fillColor = col
  68. end
  69.  
  70.  
  71. function lib.rect(x, y, w, h)
  72.     if lib.doFill then
  73.         for i = x + 1, x + w - 1 do
  74.             for j = y + 1, y + h - 1 do
  75.                 lib.set(i, j, lib.fillColor)
  76.             end
  77.         end
  78.     end
  79.  
  80.     if lib.doStroke then
  81.         for weight = 1, lib.strokeWeight do
  82.             for i = x, x + w - 1 do
  83.                 lib.set(i, y+weight-1, lib.strokeColor)
  84.                 lib.set(i, y + h-weight+1, lib.strokeColor)
  85.             end
  86.             for j = y, y + h - 1 do
  87.                 lib.set(x+weight-1, j, lib.strokeColor)
  88.                 lib.set(x + w-weight+1, j, lib.strokeColor)
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. function lib.circle(x, y, extent)
  95.     if lib.doFill then
  96.        for i=x-extent,x+extent do
  97.             for j=y-extent,y+extent do
  98.                 if math.sqrt((i-x)^2+(j-y)^2) <= extent then
  99.                     lib.set(i,j,lib.fillColor)
  100.                 end
  101.             end
  102.         end
  103.     end
  104.  
  105.         if lib.doStroke then
  106.        for i=x-extent,x+extent do
  107.             for j=y-extent,y+extent do
  108.                 length=math.sqrt((i-x)^2+(j-y)^2)
  109.                 if length <= extent and length>extent-lib.strokeWeight then
  110.                     lib.set(i,j,lib.strokeColor)
  111.                 end
  112.             end
  113.         end
  114.     end
  115. end
  116.  
  117. function lib.set(x,y,textCol)
  118.     lib.screen.setCursorPos(x,math.floor(y*lib.yRatio))
  119.     lib.screen.setBackgroundColor(textCol)
  120.     lib.screen.write(" ")
  121.  
  122. end
  123.  
  124.  
  125.  
  126. return lib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement