Advertisement
beezing

Fractal Drawing - Coders

Nov 3rd, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- Fractal Drawing
  2. -- by @beezing
  3.  
  4. imgHeight = 220
  5. imgWidth  = 300
  6.  
  7. inCol  = 0   -- mandelbrot set color
  8. colFac = 12  -- color gradient factor
  9. maxIt  = 48  -- number of iterations
  10.  
  11. function Fractal(x0,y0, minRe,maxRe,minIm, jRe,jIm, R,G,B)
  12.   maxIm = minIm+(maxRe-minRe)*(imgHeight/imgWidth)
  13.   reFac = (maxRe-minRe)/imgWidth
  14.   imFac = (maxIm-minIm)/imgHeight
  15.  
  16.   for y = 0, imgHeight-1 do
  17.     cIm = maxIm - y*imFac
  18.     for x = 0, imgWidth-1 do
  19.       cRe = minRe + x*reFac
  20.       zRe = cRe; zIm = cIm;
  21.  
  22.       n = 0; isIn = true; outCol = colFac;
  23.       repeat
  24.         n = n + 1
  25.         zRe2 = zRe*zRe; zIm2 = zIm*zIm;
  26.         zIm  = 2*zRe*zIm + (jIm or cIm)
  27.         zRe  = zRe2-zIm2 + (jRe or cRe)
  28.         isIn = not (zRe2+zIm2 > 4)
  29.  
  30.         if n < maxIt/2-1 then
  31.           outCol = outCol + colFac
  32.         else
  33.           outCol = outCol - colFac
  34.         end
  35.       until (not isIn) or (n > maxIt-1)
  36.  
  37.       if isIn then
  38.         setcolor(inCol,inCol,inCol)
  39.       else
  40.         if R then r = R-outCol else r = outCol end
  41.         if G then g = G-outCol else g = outCol end
  42.         if B then b = B-outCol else b = outCol end
  43.         setcolor(r,g,b)
  44.       end
  45.  
  46.       drawPoint(x+x0, y+y0)
  47.     end
  48.   end
  49. end
  50.  
  51. clear()
  52. --print('Calculating fractal image. Please wait...')
  53. --disableScreenRefresh()
  54. Fractal(10, 60,-1.1,-.6,-0.55,nil,nil,nil,nil,128)
  55. Fractal(10,290,-1.7,1.7,-1.25,-.8,.15,128,nil,nil)
  56. Fractal(10,520,-1.7,1.7,-1.25,-.7,.38,nil,128,nil)
  57. --enableScreenRefresh()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement