Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Mandelbrot_Viewer_For_CraftOS = {}
- local width, height = term.getSize()
- local maxIterations = 1000
- local xMin, xMax, yMin, yMax = -2, 1, -1, 1
- local zoomFactor, moveSpeed = 0.8, 0.05
- local centerX, centerY = 0, 0
- local escapeRadius = 4
- local colorMode = 1
- local autoZoom = false
- local colorPalette = {
- colors.white, colors.orange, colors.magenta, colors.lightBlue,
- colors.yellow, colors.lime, colors.pink, colors.gray,
- colors.lightGray, colors.cyan, colors.purple, colors.blue,
- colors.brown, colors.green, colors.red
- }
- local function map(x, y)
- local real = xMin + (xMax - xMin) * x / width
- local imag = yMin + (yMax - yMin) * y / height
- return real, imag
- end
- local function mandelbrot(cr, ci)
- local zr, zi, zr2, zi2 = 0, 0, 0, 0
- for i = 1, maxIterations do
- if zr2 + zi2 > escapeRadius then
- return i
- end
- zi = 2 * zr * zi + ci
- zr = zr2 - zi2 + cr
- zr2, zi2 = zr * zr, zi * zi
- end
- return maxIterations
- end
- local function getColor(iterations)
- if iterations == maxIterations then return colors.black end
- return colorPalette[(iterations % #colorPalette) + 1]
- end
- local function renderMandelbrot()
- for y = 1, height do
- for x = 1, width do
- local cr, ci = map(x, y)
- local iterations = mandelbrot(cr, ci)
- local color = getColor(iterations)
- term.setCursorPos(x, y)
- term.setBackgroundColor(color)
- term.write(" ")
- end
- end
- end
- local function zoom(factor, x, y)
- local cr, ci = map(x, y)
- local width, height = xMax - xMin, yMax - yMin
- xMin = cr - width * factor / 2
- xMax = cr + width * factor / 2
- yMin = ci - height * factor / 2
- yMax = ci + height * factor / 2
- centerX, centerY = cr, ci
- end
- local function move(dx, dy)
- local width, height = xMax - xMin, yMax - yMin
- local mx, my = dx * width * moveSpeed, dy * height * moveSpeed
- xMin, xMax = xMin + mx, xMax + mx
- yMin, yMax = yMin + my, yMax + my
- centerX, centerY = centerX + mx, centerY + my
- end
- function Mandelbrot_Viewer_For_CraftOS.run()
- local running = true
- local timer = os.startTimer(0.1)
- while running do
- renderMandelbrot()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(string.format("Zoom: %.2e", 2 / (xMax - xMin)))
- local event, param1 = os.pullEvent()
- if event == "key" then
- if param1 == keys.space then zoom(zoomFactor, width / 2, height / 2)
- elseif param1 == keys.leftShift then zoom(1 / zoomFactor, width / 2, height / 2)
- elseif param1 == keys.left then move(-1, 0)
- elseif param1 == keys.right then move(1, 0)
- elseif param1 == keys.up then move(0, -1)
- elseif param1 == keys.down then move(0, 1)
- elseif param1 == keys.q then running = false
- elseif param1 == keys.c then
- colorMode = colorMode == 1 and 2 or 1
- colorPalette = colorMode == 1 and colorPalette or {unpack(colorPalette, 1, 8)}
- elseif param1 == keys.a then autoZoom = not autoZoom
- elseif param1 == keys.equals then maxIterations = math.min(maxIterations * 2, 10000)
- elseif param1 == keys.minus then maxIterations = math.max(100, maxIterations / 2)
- end
- elseif event == "timer" and param1 == timer then
- timer = os.startTimer(0.1)
- if autoZoom then zoom(0.99, width / 2, height / 2) end
- end
- end
- end
- term.clear()
- print("Mandelbrot Viewer For CraftOS")
- print("Arrow keys: Move")
- print("Space/Shift: Zoom in/out")
- print("C: Toggle color mode")
- print("A: Toggle auto-zoom")
- print("+/-: Increase/decrease detail")
- print("Q: Quit")
- sleep(2)
- Mandelbrot_Viewer_For_CraftOS.run()
Add Comment
Please, Sign In to add comment