--[[ MADE BY BCHEESE, 2021. Control: Arrow Keys - move Key Z - zoom in Key X - zoom out --]] local Window = require("window") local win local w, h local scale local pos local mimg local update local gamepad function sqr(v) return v * v end function vec(_x, _y) local v = { x = _x, y = _y } return v end function len(_vec) return math.sqrt(sqr(_vec.x) + sqr(_vec.y)) end function mandelbrot(w, h, iter, offset, scale) update = false for y = 0, h - 1 do for x = 0, w - 1 do p = vec(x - offset.x, y - offset.y) p.x = p.x * scale p.y = p.y * scale z = vec(0, 0) i = 0 while (i < iter and (sqr(z.x) + sqr(z.y)) < 4) do n = vec() n.x = sqr(z.x) - sqr(z.y) + p.x n.y = 2 * z.x * z.y + p.y z.x = n.x z.y = n.y i = i + 1 end if (i == iter) then image.pixel(mimg, x * 2, y, 1) image.pixel(mimg, x * 2 + 1, y, 1) else image.pixel(mimg, x * 2, y, 1 + (i / iter) * 7) image.pixel(mimg, x * 2 + 1, y, 1 + (i / iter) * 7) end end end end function _init() w = 280 h = 120 win = Window:new("Mandelbrot", 144, 24, w, h) win.onclose = function() sys.exit() end mimg = image.new(w, h, 8) scale = 30 pos = vec(w / 3.5, h / 2.25) mandelbrot(w, h, 16, pos, 1 / scale) end function _step(t) win:step(t) gamepad = input.gamepad(0) if gamepad & 1 > 0 then pos.x = pos.x - 5; update = true end if gamepad & 2 > 0 then pos.x = pos.x + 5; update = true end if gamepad & 4 > 0 then pos.y = pos.y + 5; update = true end if gamepad & 8 > 0 then pos.y = pos.y - 5; update = true end if gamepad & 64 > 0 then scale = scale - 5; update = true end if gamepad & 128 > 0 then scale = scale + 5; update = true end if update == true then mandelbrot(w, h, 16, pos, 1 / scale) end image.draw(mimg, 0, 0, 0, 0, w, h) end