Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Mandelbrot Fractal
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Geniis




PostPosted: Fri Mar 12, 2010 12:54 am   Post subject: Mandelbrot Fractal

Hey just made a quick little mandelbrot set generator, well 2 actually. Second ones a little faster but you can change the degree of the first one.

Heres the first one :
Turing:


for i : 0 .. 255
    RGB.SetColor (i, (i ** 2 / 255), (i ** 2) / 255, (255 - i) / 255)
end for
const Black := RGB.AddColor (0, 0, 0)

type CNum :
    record
        r, i : real
    end record

fcn CAdd (a, b : CNum) : CNum
    var c := a
    c.r += b.r
    c.i += b.i
    result c
end CAdd

fcn CSub (a, b : CNum) : CNum
    var c := a
    c.r -= b.r
    c.i -= b.i
    result c
end CSub

fcn CMult (a, b : CNum) : CNum
    var c : CNum
    c.r := a.r * b.r - a.i * b.i
    c.i := a.r * b.i + b.r * a.i
    result c
end CMult

proc GenerateMandelbrot (deg, magnitude, depth, xt, yt : int, zoom : real)
    %deg : the degree of the fractal (number of branches)
    %magnitude : the magnitude of the fractal (recommended left at 4)
    %depth : how deep the algorithm looks for an escape (max number of iterations, higher = longer to calculate)
    %xt and yt : the x and y translations
    %zoom : the amount the fractal is zoomed in
    var z, c, t : CNum
    var p : real
    var n : nat := 0
    drawfillbox (0, 0, maxx, maxy, Black)
    for x : 0 .. maxx
        for y : 0 .. maxy
            c.r := (x - xt) / zoom
            c.i := (y - yt) / zoom
            z := c
            n := 0
            if deg = 2 then
                if (c.r + 1) ** 2 + c.i ** 2 < 1 / 16 then
                    p := 0
                else
                    p := sqrt ((c.r - 0.25) ** 2 + c.i ** 2)
                end if
            else
                p := sqrt (maxint)
            end if
            if c.r > p - 2 * p ** 2 + 0.25 then
                loop
                    n += 1
                    exit when z.r ** 2 + z.i ** 2 > magnitude or n = depth
                    t := z
                    for i : 2 .. deg
                        t := CMult (t, z)
                    end for
                    z := CAdd (t, c)
                end loop
                if n not= depth then
                    drawdot (x, y, n)
                end if
            end if
        end for
    end for
end GenerateMandelbrot

GenerateMandelbrot (2, 4, 255, maxx div 2, maxy div 2, 100)



Play around with it, do what you want... the higher degrees look pretty interesting.

The second one:
Turing:


for i : 0 .. 255
    RGB.SetColor (i, (i ** 2 / 255), (i ** 2) / 255, (255 - i) / 255)
end for
const Black := RGB.AddColor (0, 0, 0)

proc GenMandlebrot (mag, dep, xt, yt : int, zoom : real)
    %mag : the magnitude of the fractal (recommended left at 4)
    %dep : how deep the algorithm looks for an escape (max number of iterations, higher = longer to calculate)
    %xt and yt : the x and y translations
    %zoom : the amount the fractal is zoomed in
    var zr, zi, zt, cr, ci, p : real := 0
    var n : nat := 0
    for x : 0 .. maxx
        for y : 0 .. maxy
            cr := (x - xt) / zoom
            ci := (y - yt) / zoom
            zr := cr
            zi := ci
            n := 0
            if (cr + 1) ** 2 + ci ** 2 < 1 / 16 then
                p := cr + 1
            else
                p := sqrt ((cr - 0.25) ** 2 + ci ** 2)
            end if
            if cr < p - 2 * p ** 2 + 0.25 then
                drawdot (x, y, Black)
            else
                loop
                    n += 1
                    exit when zr ** 2 + zi ** 2 > mag or n = dep
                    zt := zr ** 2 - zi ** 2 + cr
                    zi := 2 * zr * zi + ci
                    zr := zt
                end loop
                if n not= dep then
                    drawdot (x, y, n)
                else
                    drawdot (x, y, Black)
                end if
            end if
        end for
    end for
end GenMandlebrot

GenMandlebrot (10, 255, -maxx div 2, -maxy div 2, 100)



This one is a little faster than the other but it always draws a second degree mandelbrot set.

EDIT: Oh yeah, and they're a little slow... didnt do too much to optimize them... not sure i can
Sponsor
Sponsor
Sponsor
sponsor
Turing_Gamer




PostPosted: Fri Mar 12, 2010 8:33 am   Post subject: RE:Mandelbrot Fractal

Second one is just a blue screen... First one was nice though... Reminds me of Raycaster
USEC_OFFICER




PostPosted: Fri Mar 12, 2010 12:10 pm   Post subject: RE:Mandelbrot Fractal

Nice program. The Madelbrot Fractal always reminds me of some sort of bug. (Sorry, that was kinda inane.)
Geniis




PostPosted: Fri Mar 12, 2010 2:33 pm   Post subject: Re: Mandelbrot Fractal

Ok heres the second one fixed:
Turing:



for i : 0 .. 255
    RGB.SetColor (i, (i ** 2 / 255), (i ** 2) / 255, (255 - i) / 255)
end for
const Black := RGB.AddColor (0, 0, 0)

proc GenMandlebrot (mag, dep, xt, yt : int, zoom : real)
    %mag : the magnitude of the fractal (recommended left at 4)
    %dep : how deep the algorithm looks for an escape (max number of iterations, higher = longer to calculate)
    %xt and yt : the x and y translations
    %zoom : the amount the fractal is zoomed in
    var zr, zi, zt, cr, ci, p : real := 0
    var n : nat := 0
    for x : 0 .. maxx
        for y : 0 .. maxy
            cr := (x - xt) / zoom
            ci := (y - yt) / zoom
            zr := cr
            zi := ci
            n := 0
            if (cr + 1) ** 2 + ci ** 2 < 1 / 16 then
                p := cr + 1
            else
                p := sqrt ((cr - 0.25) ** 2 + ci ** 2)
            end if
            if cr < p - 2 * p ** 2 + 0.25 then
                drawdot (x, y, Black)
            else
                loop
                    n += 1
                    exit when zr ** 2 + zi ** 2 > mag or n = dep
                    zt := zr ** 2 - zi ** 2 + cr
                    zi := 2 * zr * zi + ci
                    zr := zt
                end loop
                if n not= dep then
                    drawdot (x, y, n)
                else
                    drawdot (x, y, Black)
                end if
            end if
        end for
    end for
end GenMandlebrot

GenMandlebrot (10, 255, maxx div 2, maxy div 2, 100)

I had set the translations to the wrong side it should work now.

I think im gonna do the julia fractal now... maybee...
Homer_simpson




PostPosted: Sat Mar 13, 2010 8:06 pm   Post subject: Re: Mandelbrot Fractal

really cool fun to play with Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: