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
ericfourfour




PostPosted: Sun Dec 03, 2006 12:39 am   Post subject: Mandelbrot Fractal

Here is a mandelbrot fractal. The algorithm is straight from the mandelbrot fractal page on wikipedia (look at my updateMandelbrot () procedure in the Mandelbrot class). You can set the magnification and the offsets (offsets have not been perfected but it gets the job done). I made it update one pixel at a time so you can add stuff in between (you could even have 2 mandelbrots going at once!). Since this is Turing it will go very slow when the magnification is high.

By the way, a magnification of 1 is very boring. Start at 10 and work your way up.

code:
class Mandelbrot
    export create, update

    var startX, startY : int
    var endX, endY : int
    var offsetX, offsetY : int
    var pixelX, pixelY : int
    var scale : real

    var clr : int

    proc create (x1, y1, x2, y2 : int, magnification : real)
        startX := x1
        startY := y1
        endX := x2
        endY := y2
        offsetX := -startX
        offsetY := -startY
        pixelX := startX
        pixelY := startY
        scale := 1 / magnification
        clr := black
    end create

    fcn updatePixels () : boolean
        if pixelX >= endX and pixelY >= endY then
            result false
        end if
        pixelX += 1
        if pixelX > endX then
            pixelX := startX
            pixelY += 1
        end if
        result true
    end updatePixels

    proc updateMandelbrot ()
        var x := pixelX * scale
        var y := pixelY * scale
        var x0 := x
        var y0 := y

        var x2 := x * x
        var y2 := y * y

        var iteration := 0
        var maxIteration := 1000

        loop
            exit when x2 + y2 > 4 or iteration >= maxIteration

            y := 2 * x * y + y0
            x := x2 - y2 + x0

            x2 := x * x
            y2 := y * y

            iteration += 1
        end loop

        if iteration = maxIteration then
            clr := black
        end if
        clr := (iteration + 1) rem 256
    end updateMandelbrot

    fcn think () : boolean
        if not updatePixels () then
            result false
        end if
        updateMandelbrot ()
        result true
    end think

    proc render ()
        Draw.Dot (pixelX + offsetX, pixelY + offsetY, clr)
    end render

    fcn update () : boolean
        if not think () then
            result false
        end if
        render ()
        result true
    end update
end Mandelbrot

View.Set ("graphics:max;max")

type Data :
    record
        mag : real
        offsetX, offsetY : int
    end record

fcn getData () : Data
    var newData : Data

    put "Magnification: " ..
    get newData.mag

    put "Offset X: " ..
    get newData.offsetX

    put "Offset Y: " ..
    get newData.offsetY

    result newData
end getData

proc mandelbrotLoop (m : ^Mandelbrot)
    loop
        exit when not m -> update ()
    end loop
end mandelbrotLoop

proc main ()
    var data : Data
    var highX : int
    var lowX : int
    var highY : int
    var lowY : int

    var m : ^Mandelbrot
    new Mandelbrot, m

    loop
        data := getData ()

        highX := round (data.mag * 2) + data.offsetX
        lowX := -highX
        highY := round (data.mag * 2) + data.offsetY
        lowY := -highY

        m -> create (lowX, lowY, highX, highY, data.mag)
        cls
        mandelbrotLoop (m)
    end loop
end main
main ()
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Sun Dec 03, 2006 10:10 pm   Post subject: (No subject)

I updated it and now you can use the mouse with it while it is running.

Controls:
Left Mouse - Centre fractal around mouse coordinates.
Middle Mouse - Clear the screen and refresh the fractal.
Right Mouse - Stop drawing the fractal and go back to the settings.

You can do all of the above commands while it is generating the fractal because I thought it would get boring if you have to wait until it is done before you can move it around to view other parts.

It also now shows the current line (on the y axis) that it is generating so you can have an idea of how long it is going to take.

If you are wondering a bit about the Mandelbrot class here is a few of its procedures it uses (new version):

code:
update ()
Updates the next pixel (calculates colour and draws it). Returns false if there wasn't any pixels left to generate.

code:
refresh ()
Redraws the current state of the fractal.

code:
reload (x, y : int)
Loads the current state of the fractal centred at a new position.

I made it an attachment because the code is around 200 lines now.



mandelbrot.t
 Description:
Here is the new Mandelbrot program.

Download
 Filename:  mandelbrot.t
 Filesize:  4.12 KB
 Downloaded:  139 Time(s)

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  [ 2 Posts ]
Jump to:   


Style:  
Search: