Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Mandelbrot set: Iterate through the following function
- fc(z) z^2 + c
- Case1, blows up:
- c = 1
- f1(0) 0^2 + 1 --> 1
- f1(1) 1^2 + 1 --> 2
- f1(2) 2^2 + 1 --> 5
- f1(5) 5^2 + 1 --> 26
- Case2, is always <= 2:
- c = -1
- f-1(0) 0^2 - 1 --> -1
- f-1(-1) -1^2 - 1 --> 0
- f-1(0) 0^2 - 1 --> -1
- ]]
- function love.load()
- maxIter = 1000
- gridSize = 50
- default = 10
- default2 = 5
- box = 1
- colorScale = 13
- scale = 1
- transX = 0
- transY = 0
- power = 2
- iterationCount = 0
- pixelCount = 0
- love.graphics.setBackgroundColor(255, 255, 255)
- screenshot = nil
- points = {}
- loops = 0
- width, height = love.graphics.getWidth(), love.graphics.getHeight()
- processing = true
- zoomCol = width / 2
- zoomRow = height / 2
- nowH, nowW = 10, 10
- function love.keypressed(key)
- if key == "escape" then
- love.event.quit()
- end
- end
- function love.mousepressed(x, y, button)
- if button == "wu" and not processing then
- scale = scale + (scale / 4)
- elseif button == "wd" and not processing then
- scale = scale - (scale / 4)
- end
- end
- function toRealNumber(imaginary)
- return imaginary ^ 2
- end
- function f(z, c)
- return z ^ power + c
- end
- function comma(number)
- repeat number, count = tostring(number):gsub("^(%d+)(%d%d%d)", "%1,%2") until count == 0
- return number
- end
- function redraw(zoom)
- end
- end
- function love.draw()
- love.graphics.scale(scale, scale)
- love.graphics.translate(transX, transY)
- if loops == 0 then
- for _, point in ipairs(points) do
- local green = point[4] * colorScale
- local red = 0
- if green > 255 then
- red = green - 255
- green = 255
- end
- love.graphics.setColor(point[3] == "white" and {red, green, 0} or {0, 0, 0})
- love.graphics.rectangle("fill", point[1], point[2], default, default)
- end
- elseif loops == 1 or loops == 2 then
- love.graphics.draw(screenshot, 0, 0)
- for _, point in ipairs(points) do
- local green = point[4] * colorScale
- local red = 0
- if green > 255 then
- red = green - 255
- green = 255
- end
- love.graphics.setColor(point[3] == "white" and {red, green, 0} or {0, 0, 0})
- love.graphics.rectangle("fill", point[1], point[2], default, default)
- end
- else
- love.graphics.draw(screenshot, 0, 0)
- end
- love.graphics.setColor(255, 255, 255)
- love.graphics.rectangle("line", nowW - gridSize, nowH - gridSize, gridSize, gridSize)
- if loops < 3 then
- love.graphics.printf("Iterations: " .. comma(iterationCount), 25, 25, 500, "left")
- love.graphics.printf("Pixels: " .. comma(pixelCount), 25, 50, 500, "left")
- end
- end
- function love.update()
- if processing then
- for row = nowH - gridSize, nowH, default do
- for col = nowW - gridSize, nowW, default do
- pixelCount = pixelCount + 1
- local real = (col - zoomCol) * 4 / width
- local imaginary = (row - zoomRow) * 4 / width
- local z, c, iter = 0, 0, 0
- while (f(z, toRealNumber(c)) <= 4 and iter < maxIter) do
- iterationCount = iterationCount + 1
- local zNew = z^2 - toRealNumber(c) + real
- c = 2*z*c + imaginary
- z = zNew
- iter = iter + 1
- end
- if iter < maxIter then
- table.insert(points, {col, row, "white", iter})
- else
- table.insert(points, {col, row, "black", iter})
- end
- end
- end
- nowW = nowW + gridSize
- if nowW >= width + (gridSize * 3) then
- nowW = 1
- nowH = nowH + gridSize
- end
- if nowH - gridSize >= height then
- nowW = 1
- nowH = 1
- screenshot = love.graphics.newImage(love.graphics.newScreenshot(true))
- points = {}
- loops = loops + 1
- if loops == 1 then
- pixelCount = 0
- iterationCount = 0
- processing = true
- default = default2
- elseif loops == 2 then
- pixelCount = 0
- iterationCount = 0
- default = box
- else
- processing = false
- end
- end
- else
- if love.keyboard.isDown("w") then
- transY = transY + (5*(1/scale))
- end
- if love.keyboard.isDown("s") then
- transY = transY - (5*(1/scale))
- end
- if love.keyboard.isDown("d") then
- transX = transX - (5*(1/scale))
- end
- if love.keyboard.isDown("a") then
- transX = transX + (5*(1/scale))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment