Here is a mandelbrot set generator I ported from some code I found on pastebin by an anonymous coder...


Code:

zmg.clear()

iter=100
esclim=2.0
ulx=-2.0
uly=1.0
lrx=1.0
lry=-1.0
SCREEN_WIDTH = 384
SCREEN_HEIGHT = 216
width=50
height=100
tilesizey=SCREEN_HEIGHT/height
tilesizex=SCREEN_WIDTH/width

function drawFracLine( line, y )
    for i=1,#line,1 do
        if string.sub(line,i,i)=="#" then
            zmg.drawRectFill((i*tilesizex)-tilesizex,(y*tilesizey)-tilesizey,tilesizex,tilesizey,zmg.makeColor("black"))
        end
    end
end

function abs(x,y)
    return math.sqrt(x*x+y*y)
end

function escapeq(cx,cy)
    local zx=0.0
    local zy=0.0
    local i=0

    while i<iter and abs(zx,zy)<esclim do
        tmp=zx*zx-zy*zy
        zy=2.0*zx*zy
        zx=tmp

        zx=zx+cx
        zy=zy+cy
        i=i+1
    end

    return i<iter
end

for y=1,height do
    line=''
    for x=1,width do
        zx=ulx+(lrx-ulx)/width*x;
        zy=uly+(lry-uly)/height*y;
        if escapeq(zx,zy) then
            line=line..'.'
        else
            line=line..'#'
        end
    end
    drawFracLine(line, y)
    --print(line)
end
zmg.fastCopy()
zmg.keyMenu()


It works well, but takes forever to generate. Is there a way to optimize this more?

Its a cool little script to play around with nonetheless. Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement