sapbot

Rotating Cube

Apr 4th, 2026 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. -- Rotating Cube Demo - Auto Monitor Detection + Safe Text Scale
  2. -- Uses Advanced Monitor on the right if available, otherwise falls back to primary terminal
  3.  
  4. local mon = peripheral.wrap("right")
  5. local isMonitor = false
  6.  
  7. if mon then
  8.     isMonitor = true
  9.     print("Using Advanced Monitor on the right")
  10. else
  11.     mon = term
  12.     print("No monitor on right - using primary terminal")
  13. end
  14.  
  15. -- Auto-set text scale ONLY if it's a real monitor
  16. local function autoSetScale(display)
  17.     if not isMonitor then
  18.         return  -- terminal does not support setTextScale
  19.     end
  20.  
  21.     local w, h = display.getSize()
  22.  
  23.     -- Choose a good text scale based on monitor size
  24.     if w >= 80 and h >= 40 then
  25.         display.setTextScale(3)
  26.     elseif w >= 51 and h >= 26 then
  27.         display.setTextScale(2)
  28.     elseif w >= 30 and h >= 16 then
  29.         display.setTextScale(1.5)
  30.     else
  31.         display.setTextScale(1)
  32.     end
  33. end
  34.  
  35. autoSetScale(mon)
  36.  
  37. local width, height = mon.getSize()
  38.  
  39. -- Cube vertices
  40. local vertices = {
  41.     {-1,-1,-1}, {1,-1,-1}, {1,1,-1}, {-1,1,-1},   -- back face
  42.     {-1,-1, 1}, {1,-1, 1}, {1,1, 1}, {-1,1, 1}    -- front face
  43. }
  44.  
  45. -- Cube edges
  46. local edges = {
  47.     {1,2},{2,3},{3,4},{4,1},   -- back
  48.     {5,6},{6,7},{7,8},{8,5},   -- front
  49.     {1,5},{2,6},{3,7},{4,8}    -- sides
  50. }
  51.  
  52. local function rotateY(p, angle)
  53.     local c, s = math.cos(angle), math.sin(angle)
  54.     return {p[1]*c - p[3]*s, p[2], p[1]*s + p[3]*c}
  55. end
  56.  
  57. local function rotateX(p, angle)
  58.     local c, s = math.cos(angle), math.sin(angle)
  59.     return {p[1], p[2]*c - p[3]*s, p[2]*s + p[3]*c}
  60. end
  61.  
  62. local function project(point)
  63.     local scale = math.min(width, height) / 4.8   -- tuned for good fit
  64.     local x = math.floor(width / 2 + point[1] * scale)
  65.     local y = math.floor(height / 2 - point[2] * scale)
  66.     return x, y
  67. end
  68.  
  69. -- Main loop
  70. local angle = 0
  71. local speed = 0.06
  72.  
  73. while true do
  74.     mon.clear()
  75.  
  76.     mon.setCursorPos(2, 1)
  77.     mon.setTextColor(colors.white)
  78.     mon.write("Rotating Cube")
  79.  
  80.     -- Rotate points
  81.     local rotated = {}
  82.     for i, v in ipairs(vertices) do
  83.         local p = rotateY(v, angle)
  84.         p = rotateX(p, angle * 0.75)
  85.         rotated[i] = p
  86.     end
  87.  
  88.     -- Draw wireframe
  89.     mon.setTextColor(colors.lime)
  90.     for _, edge in ipairs(edges) do
  91.         local p1 = rotated[edge[1]]
  92.         local p2 = rotated[edge[2]]
  93.  
  94.         local x1, y1 = project(p1)
  95.         local x2, y2 = project(p2)
  96.  
  97.         local dx = x2 - x1
  98.         local dy = y2 - y1
  99.         local steps = math.max(math.abs(dx), math.abs(dy)) + 1
  100.  
  101.         for i = 0, steps do
  102.             local x = math.floor(x1 + dx * i / steps)
  103.             local y = math.floor(y1 + dy * i / steps)
  104.             if x >= 1 and x <= width and y >= 1 and y <= height then
  105.                 mon.setCursorPos(x, y)
  106.                 mon.write("#")
  107.             end
  108.         end
  109.     end
  110.  
  111.     -- Center point
  112.     mon.setCursorPos(math.floor(width/2), math.floor(height/2))
  113.     mon.write("+")
  114.  
  115.     -- Footer
  116.     mon.setCursorPos(2, height)
  117.     mon.setTextColor(colors.lightGray)
  118.     mon.write("Ctrl + T to stop")
  119.  
  120.     angle = angle + speed
  121.     sleep(0.05)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment