hi - I made a short program to plot the Mandelbrot set. My algorithm takes several seconds to draw the set. It iterates over every ‘pixel’ in my 320 x 480 screen and calculates what shade of grey it should be, then creates a 1 x 1 rectangle with that shade of grey. display.newRect() is called around 154,000 times. I’m wondering if there’s a faster way to draw the set. Thanks.
Here’s the code.
local MAX\_ITER = 80 local re\_start= -2 local re\_end = 1 local im\_start = -1 local im\_end = 1 local WIDTH = display.contentWidth local HEIGHT = display.contentHeight --count and return number of iterations of f(z) = z^2 + c, where c is a complex number (cr + ci), --until either MAX\_ITER is reached or z exceeds 2. local function mandelbrot(cr,ci) local z = 0 local zr = 0 local zi = 0 local zrp = 0 local zip = 0 local n = 0 while math.abs(z) \<= 2 and n \< MAX\_ITER do zr = (zrp \* zrp) + cr - (zip \* zip) zi = (2 \* (zrp \* zip)) + ci z = math.sqrt((zr \* zr) + (zi \* zi)) zrp = zr zip = zi n = n + 1 end return n end draw = function() local t = 0 local real = 0 local im = 0 local mag = 0 local col = 0 for x = 0, WIDTH do for y = 0, HEIGHT do real = re\_start+ ((x/WIDTH) \* (re\_end - RE\_START)) im = IM\_START + ((y/HEIGHT) \* (IM\_END - IM\_START) ) t = mandelbrot(real,im) col = 255 - (t \* 255 / MAX\_ITER) local dot = display.newRect(x,y,1,1) dot:setFillColor(col/255,col/255,col/255) end end end draw()