nonogamer9

mandelbrot set for ComputerCraft

Sep 25th, 2024 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | Software | 0 0
  1. local Mandelbrot_Viewer_For_CraftOS = {}
  2.  
  3. local width, height = term.getSize()
  4. local maxIterations = 1000
  5. local xMin, xMax, yMin, yMax = -2, 1, -1, 1
  6. local zoomFactor, moveSpeed = 0.8, 0.05
  7. local centerX, centerY = 0, 0
  8. local escapeRadius = 4
  9. local colorMode = 1
  10. local autoZoom = false
  11.  
  12. local colorPalette = {
  13.     colors.white, colors.orange, colors.magenta, colors.lightBlue,
  14.     colors.yellow, colors.lime, colors.pink, colors.gray,
  15.     colors.lightGray, colors.cyan, colors.purple, colors.blue,
  16.     colors.brown, colors.green, colors.red
  17. }
  18.  
  19. local function map(x, y)
  20.     local real = xMin + (xMax - xMin) * x / width
  21.     local imag = yMin + (yMax - yMin) * y / height
  22.     return real, imag
  23. end
  24.  
  25. local function mandelbrot(cr, ci)
  26.     local zr, zi, zr2, zi2 = 0, 0, 0, 0
  27.     for i = 1, maxIterations do
  28.         if zr2 + zi2 > escapeRadius then
  29.             return i
  30.         end
  31.         zi = 2 * zr * zi + ci
  32.         zr = zr2 - zi2 + cr
  33.         zr2, zi2 = zr * zr, zi * zi
  34.     end
  35.     return maxIterations
  36. end
  37.  
  38. local function getColor(iterations)
  39.     if iterations == maxIterations then return colors.black end
  40.     return colorPalette[(iterations % #colorPalette) + 1]
  41. end
  42.  
  43. local function renderMandelbrot()
  44.     for y = 1, height do
  45.         for x = 1, width do
  46.             local cr, ci = map(x, y)
  47.             local iterations = mandelbrot(cr, ci)
  48.             local color = getColor(iterations)
  49.             term.setCursorPos(x, y)
  50.             term.setBackgroundColor(color)
  51.             term.write(" ")
  52.         end
  53.     end
  54. end
  55.  
  56. local function zoom(factor, x, y)
  57.     local cr, ci = map(x, y)
  58.     local width, height = xMax - xMin, yMax - yMin
  59.     xMin = cr - width * factor / 2
  60.     xMax = cr + width * factor / 2
  61.     yMin = ci - height * factor / 2
  62.     yMax = ci + height * factor / 2
  63.     centerX, centerY = cr, ci
  64. end
  65.  
  66. local function move(dx, dy)
  67.     local width, height = xMax - xMin, yMax - yMin
  68.     local mx, my = dx * width * moveSpeed, dy * height * moveSpeed
  69.     xMin, xMax = xMin + mx, xMax + mx
  70.     yMin, yMax = yMin + my, yMax + my
  71.     centerX, centerY = centerX + mx, centerY + my
  72. end
  73.  
  74. function Mandelbrot_Viewer_For_CraftOS.run()
  75.     local running = true
  76.     local timer = os.startTimer(0.1)
  77.  
  78.     while running do
  79.         renderMandelbrot()
  80.         term.setCursorPos(1, 1)
  81.         term.setBackgroundColor(colors.black)
  82.         term.setTextColor(colors.white)
  83.         term.write(string.format("Zoom: %.2e", 2 / (xMax - xMin)))
  84.        
  85.         local event, param1 = os.pullEvent()
  86.        
  87.         if event == "key" then
  88.             if param1 == keys.space then zoom(zoomFactor, width / 2, height / 2)
  89.             elseif param1 == keys.leftShift then zoom(1 / zoomFactor, width / 2, height / 2)
  90.             elseif param1 == keys.left then move(-1, 0)
  91.             elseif param1 == keys.right then move(1, 0)
  92.             elseif param1 == keys.up then move(0, -1)
  93.             elseif param1 == keys.down then move(0, 1)
  94.             elseif param1 == keys.q then running = false
  95.             elseif param1 == keys.c then
  96.                 colorMode = colorMode == 1 and 2 or 1
  97.                 colorPalette = colorMode == 1 and colorPalette or {unpack(colorPalette, 1, 8)}
  98.             elseif param1 == keys.a then autoZoom = not autoZoom
  99.             elseif param1 == keys.equals then maxIterations = math.min(maxIterations * 2, 10000)
  100.             elseif param1 == keys.minus then maxIterations = math.max(100, maxIterations / 2)
  101.             end
  102.         elseif event == "timer" and param1 == timer then
  103.             timer = os.startTimer(0.1)
  104.             if autoZoom then zoom(0.99, width / 2, height / 2) end
  105.         end
  106.     end
  107. end
  108.  
  109. term.clear()
  110. print("Mandelbrot Viewer For CraftOS")
  111. print("Arrow keys: Move")
  112. print("Space/Shift: Zoom in/out")
  113. print("C: Toggle color mode")
  114. print("A: Toggle auto-zoom")
  115. print("+/-: Increase/decrease detail")
  116. print("Q: Quit")
  117. sleep(2)
  118. Mandelbrot_Viewer_For_CraftOS.run()
Add Comment
Please, Sign In to add comment