columna1

Computercraft mandelbrot viewer

Jun 19th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. function load()
  2.  
  3.     cou = 0
  4.     highest = 0
  5.    
  6.     pixels = {}
  7.     w,h = term.getSize()
  8.    
  9.     for x = 1,w do
  10.         pixels[x] = {}
  11.         for y = 1,h do
  12.             pixels[x][y] = 0
  13.         end
  14.     end
  15.     zoom = 1 -- 419622325484.5764--  1779.803945297549--1--10.87870050486385
  16.     moveX =  -0.5 -- -1.574958474577329--  -0.7431533999637661--  -0.5 -- -0.756171356183087
  17.     moveY =  0 -- -0.005723458362839707--  -0.1394057861346605--0 -- -0.09928312602189358
  18.     maxIterations = 255
  19.  
  20.     for x = 1,w do
  21.     for y = 1,h do
  22.     pr = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX
  23.         pi = (y - h / 2) / (0.5 * zoom * h) + moveY
  24.         newRe = 0
  25.         newIm = 0
  26.         oldRe = 0
  27.         oldIm = 0
  28.         for i = 1,maxIterations do
  29.             oldRe = newRe
  30.             oldIm = newIm
  31.             newRe = oldRe * oldRe - oldIm * oldIm + pr
  32.             newIm = 2 * oldRe * oldIm + pi
  33.             cc = i
  34.             cd = i
  35.             if newRe * newRe + newIm * newIm > 4 then break end
  36.         end
  37.         temp = math.floor(cc / 16)
  38.         if temp > 0 then
  39.         col = temp - 1
  40.         else
  41.         col = temp
  42.         end
  43.         if cc == 255 then col = 15 end
  44.         pixels[x][y] = col
  45.     end
  46.     end
  47. end
  48.  
  49. function draw()
  50.     term.clear()
  51.     for x = 1,w do
  52.         for y = 1,h do
  53.             ting = pixels[x][y]
  54.             term.setBackgroundColor(2^pixels[x][y])
  55.             term.setCursorPos(x,y)
  56.             term.write(" ")
  57.         end
  58.     end
  59. end
  60.  
  61. load()
  62. draw()
  63. sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment