Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Rotating Cube Demo - Auto Monitor Detection + Safe Text Scale
- -- Uses Advanced Monitor on the right if available, otherwise falls back to primary terminal
- local mon = peripheral.wrap("right")
- local isMonitor = false
- if mon then
- isMonitor = true
- print("Using Advanced Monitor on the right")
- else
- mon = term
- print("No monitor on right - using primary terminal")
- end
- -- Auto-set text scale ONLY if it's a real monitor
- local function autoSetScale(display)
- if not isMonitor then
- return -- terminal does not support setTextScale
- end
- local w, h = display.getSize()
- -- Choose a good text scale based on monitor size
- if w >= 80 and h >= 40 then
- display.setTextScale(3)
- elseif w >= 51 and h >= 26 then
- display.setTextScale(2)
- elseif w >= 30 and h >= 16 then
- display.setTextScale(1.5)
- else
- display.setTextScale(1)
- end
- end
- autoSetScale(mon)
- local width, height = mon.getSize()
- -- Cube vertices
- local vertices = {
- {-1,-1,-1}, {1,-1,-1}, {1,1,-1}, {-1,1,-1}, -- back face
- {-1,-1, 1}, {1,-1, 1}, {1,1, 1}, {-1,1, 1} -- front face
- }
- -- Cube edges
- local edges = {
- {1,2},{2,3},{3,4},{4,1}, -- back
- {5,6},{6,7},{7,8},{8,5}, -- front
- {1,5},{2,6},{3,7},{4,8} -- sides
- }
- local function rotateY(p, angle)
- local c, s = math.cos(angle), math.sin(angle)
- return {p[1]*c - p[3]*s, p[2], p[1]*s + p[3]*c}
- end
- local function rotateX(p, angle)
- local c, s = math.cos(angle), math.sin(angle)
- return {p[1], p[2]*c - p[3]*s, p[2]*s + p[3]*c}
- end
- local function project(point)
- local scale = math.min(width, height) / 4.8 -- tuned for good fit
- local x = math.floor(width / 2 + point[1] * scale)
- local y = math.floor(height / 2 - point[2] * scale)
- return x, y
- end
- -- Main loop
- local angle = 0
- local speed = 0.06
- while true do
- mon.clear()
- mon.setCursorPos(2, 1)
- mon.setTextColor(colors.white)
- mon.write("Rotating Cube")
- -- Rotate points
- local rotated = {}
- for i, v in ipairs(vertices) do
- local p = rotateY(v, angle)
- p = rotateX(p, angle * 0.75)
- rotated[i] = p
- end
- -- Draw wireframe
- mon.setTextColor(colors.lime)
- for _, edge in ipairs(edges) do
- local p1 = rotated[edge[1]]
- local p2 = rotated[edge[2]]
- local x1, y1 = project(p1)
- local x2, y2 = project(p2)
- local dx = x2 - x1
- local dy = y2 - y1
- local steps = math.max(math.abs(dx), math.abs(dy)) + 1
- for i = 0, steps do
- local x = math.floor(x1 + dx * i / steps)
- local y = math.floor(y1 + dy * i / steps)
- if x >= 1 and x <= width and y >= 1 and y <= height then
- mon.setCursorPos(x, y)
- mon.write("#")
- end
- end
- end
- -- Center point
- mon.setCursorPos(math.floor(width/2), math.floor(height/2))
- mon.write("+")
- -- Footer
- mon.setCursorPos(2, height)
- mon.setTextColor(colors.lightGray)
- mon.write("Ctrl + T to stop")
- angle = angle + speed
- sleep(0.05)
- end
Advertisement
Add Comment
Please, Sign In to add comment