ForeverDev

Untitled

Oct 7th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. --[[
  2.  
  3.     Mandelbrot set:  Iterate through the following function
  4.  
  5.     fc(z) z^2 + c
  6.  
  7.  
  8.  
  9.     Case1, blows up:
  10.  
  11.     c = 1
  12.     f1(0) 0^2 + 1 --> 1
  13.     f1(1) 1^2 + 1 --> 2
  14.     f1(2) 2^2 + 1 --> 5
  15.     f1(5) 5^2 + 1 --> 26
  16.  
  17.  
  18.     Case2, is always <= 2:
  19.  
  20.     c = -1
  21.     f-1(0) 0^2 - 1 --> -1
  22.     f-1(-1) -1^2 - 1 --> 0
  23.     f-1(0) 0^2 - 1 --> -1
  24.  
  25. ]]
  26.  
  27. function love.load()
  28.  
  29.     maxIter = 1000
  30.     gridSize = 50
  31.  
  32.     default = 10
  33.     default2 = 5
  34.     box = 1
  35.  
  36.     colorScale = 13
  37.  
  38.     scale = 1
  39.     transX = 0
  40.     transY = 0
  41.  
  42.     power = 2
  43.  
  44.     iterationCount = 0
  45.     pixelCount = 0
  46.  
  47.  
  48.     love.graphics.setBackgroundColor(255, 255, 255)
  49.     screenshot = nil
  50.     points = {}
  51.     loops = 0
  52.     width, height = love.graphics.getWidth(), love.graphics.getHeight()
  53.     processing = true
  54.     zoomCol = width / 2
  55.     zoomRow = height / 2
  56.     nowH, nowW = 10, 10
  57.  
  58.     function love.keypressed(key)
  59.         if key == "escape" then
  60.             love.event.quit()
  61.         end
  62.     end
  63.  
  64.     function love.mousepressed(x, y, button)
  65.         if button == "wu" and not processing then
  66.             scale = scale + (scale / 4)
  67.         elseif button == "wd" and not processing then
  68.             scale = scale - (scale / 4)
  69.         end
  70.     end
  71.  
  72.     function toRealNumber(imaginary)
  73.         return imaginary ^ 2
  74.     end
  75.  
  76.     function f(z, c)
  77.         return z ^ power + c
  78.     end
  79.  
  80.     function comma(number)
  81.         repeat number, count = tostring(number):gsub("^(%d+)(%d%d%d)", "%1,%2") until count == 0
  82.         return number
  83.     end
  84.  
  85.     function redraw(zoom)
  86.  
  87.     end
  88. end
  89.  
  90. function love.draw()
  91.     love.graphics.scale(scale, scale)
  92.     love.graphics.translate(transX, transY)
  93.     if loops == 0 then
  94.         for _, point in ipairs(points) do
  95.             local green = point[4] * colorScale
  96.             local red = 0
  97.             if green > 255 then
  98.                 red = green - 255
  99.                 green = 255
  100.             end
  101.             love.graphics.setColor(point[3] == "white" and {red, green, 0} or {0, 0, 0})
  102.             love.graphics.rectangle("fill", point[1], point[2], default, default)
  103.         end
  104.     elseif loops == 1 or loops == 2 then
  105.         love.graphics.draw(screenshot, 0, 0)
  106.         for _, point in ipairs(points) do
  107.             local green = point[4] * colorScale
  108.             local red = 0
  109.             if green > 255 then
  110.                 red = green - 255
  111.                 green = 255
  112.             end
  113.             love.graphics.setColor(point[3] == "white" and {red, green, 0} or {0, 0, 0})
  114.             love.graphics.rectangle("fill", point[1], point[2], default, default)
  115.         end
  116.     else
  117.         love.graphics.draw(screenshot, 0, 0)
  118.     end
  119.     love.graphics.setColor(255, 255, 255)
  120.     love.graphics.rectangle("line", nowW - gridSize, nowH - gridSize, gridSize, gridSize)
  121.     if loops < 3 then
  122.         love.graphics.printf("Iterations: " .. comma(iterationCount), 25, 25, 500, "left")
  123.         love.graphics.printf("Pixels: " .. comma(pixelCount), 25, 50, 500, "left")
  124.     end
  125. end
  126.  
  127. function love.update()
  128.     if processing then
  129.         for row = nowH - gridSize, nowH, default do
  130.             for col = nowW - gridSize, nowW, default do
  131.                 pixelCount = pixelCount + 1
  132.                 local real = (col - zoomCol) * 4 / width
  133.                 local imaginary = (row - zoomRow) * 4 / width
  134.                 local z, c, iter = 0, 0, 0
  135.                 while (f(z, toRealNumber(c)) <= 4 and iter < maxIter) do
  136.                     iterationCount = iterationCount + 1
  137.                     local zNew = z^2 - toRealNumber(c) + real
  138.                     c = 2*z*c + imaginary
  139.                     z = zNew
  140.                     iter = iter + 1
  141.                 end
  142.                 if iter < maxIter then
  143.                     table.insert(points, {col, row, "white", iter})
  144.                 else
  145.                     table.insert(points, {col, row, "black", iter})
  146.                 end
  147.             end
  148.         end
  149.         nowW = nowW + gridSize
  150.         if nowW >= width + (gridSize * 3) then
  151.             nowW = 1
  152.             nowH = nowH + gridSize
  153.         end
  154.         if nowH - gridSize >= height then
  155.             nowW = 1
  156.             nowH = 1
  157.             screenshot = love.graphics.newImage(love.graphics.newScreenshot(true))
  158.             points = {}
  159.             loops = loops + 1
  160.             if loops == 1 then
  161.                 pixelCount = 0
  162.                 iterationCount = 0
  163.                 processing = true
  164.                 default = default2
  165.             elseif loops == 2 then
  166.                 pixelCount = 0
  167.                 iterationCount = 0
  168.                 default = box
  169.             else
  170.                 processing = false
  171.             end
  172.         end
  173.     else
  174.         if love.keyboard.isDown("w") then
  175.             transY = transY + (5*(1/scale))
  176.         end
  177.         if love.keyboard.isDown("s") then
  178.             transY = transY - (5*(1/scale))
  179.         end
  180.         if love.keyboard.isDown("d") then
  181.             transX = transX - (5*(1/scale))
  182.         end
  183.         if love.keyboard.isDown("a") then
  184.             transX = transX + (5*(1/scale))
  185.         end
  186.     end
  187. end
Advertisement
Add Comment
Please, Sign In to add comment