Shop OBEX P1 Docs P2 Docs Learn Events
Mandelbrot fractal with the Prop — Parallax Forums

Mandelbrot fractal with the Prop

RaymanRayman Posts: 14,826
edited 2009-04-21 00:21 in Propeller 1
It's actually much simpler than I would have thought...

Here I generated the Mandelbrot fractal using SPIN.· It took ~3 hours, but this is because I did it all in SPIN (no assembly) and choose a rather high iteration limit of 255.· The resolution is 320x240.

It would be interesting and easy to zoom into any part of the fractal, but I haven't done that.

This could easily be ported to TV (using graphics.spin) with 4 color output or to VGA.· I think 4 colors is actually enough to get the gist of it...

Anyway, here's a photo of the result:

The real trick is generating colors from the result...· The result is the number of iterations until some·threshold is reached.· If the·threshold is reached fast, I've made it red, if the·threshold is never reached (because of the·255 cycle limit I've imposed), the·result is black.· Determining colors for in between iteration values is where it becomes more art than science...

Post Edited (Rayman) : 4/20/2009 12:17:34 AM GMT
836 x 624 - 96K

Comments

  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 00:10
    Wow. How did you achieve this design? what is your schematic and program?
    P.S. What is a fractal?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • RaymanRayman Posts: 14,826
    edited 2009-04-20 00:10
    Here's the routine that generated the fractal:
    Copyright 2009 Raymond Allen
     
    OBJ
      flt: "FloatMath" 
    con
      points=12
      mscale=1.8
      itest=4.0
      mfac=2.0
      fzero=0.0
     
     
    pub MandelBrot2|i,j,x0,y0,x,y,xtemp,maxiter,iter,c
    
     
     maxiter:=255
    
     repeat i from 0 to 319 step 1 
      repeat j from 0 to 239 step 1
        x0:=flt.fdiv(flt.fmul(flt.ffloat(i-200),mscale),flt.ffloat(160))
        x:=fzero
        y0:=flt.fdiv(flt.fmul(flt.ffloat(j-120),mscale),flt.ffloat(120))
        y:=fzero
        iter:=0
        repeat while flt.fadd(flt.fmul(x,x),flt.fmul(y,y))=<itest and (iter<maxiter)
          xtemp:=flt.fadd(flt.fsub(flt.fmul(x,x),flt.fmul(y,y)),x0)
          y:=flt.fadd(flt.fmul(mfac,flt.fmul(x,y)),y0)
          x:=xtemp
          iter++
        if iter==maxiter
          lcd.setpixel(i,j,0)
        else
          c:=lcd.rgb((150+iter*3)<#255,(255-(125-iter)*3)#>0,(255-(170-iter)*2)#>0 )
          lcd.setpixel(i,j,c)
    
  • RaymanRayman Posts: 14,826
    edited 2009-04-20 00:11
    Wikipedia has a good article about fractals and the Mandebrot fractal, and psuedocode that I used...
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 00:12
    I know what a fractal is now, I saw it on another post.
    This is a very clever way of doing fractal generation.
    What is the screen you have it connected to?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • RaymanRayman Posts: 14,826
    edited 2009-04-20 00:16
    The screen is part of the soon to be released "Propeller System Module" that includes USB, LCD, 3-axis accelerometer, DAC, ADC, and a few other things...
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 00:23
    Wow, cool!! It being released by you, I imagine. Where will you release it at? Do you have a website or is Parallax releasing it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • RaymanRayman Posts: 14,826
    edited 2009-04-20 00:25
    I don't have well laid out plan... But, I have enough parts to make 25 of them, as soon as the circuit board is finalized. I may just sell them on Ebay or my own web site (rayslogic.com) using PayPal tools...
  • jazzedjazzed Posts: 11,803
    edited 2009-04-20 00:28
    Cool I was wondering when someone would do this. Can you zoom in?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • tonyp12tonyp12 Posts: 1,951
    edited 2009-04-20 00:35
    The question is,
    can you do fractal in real-time at around 15frames a seconds.

    Something like Linus demo.

    This way you can zoom in fluently



    ·
  • RaymanRayman Posts: 14,826
    edited 2009-04-20 00:39
    It's easy to zoom into any part by modifying the x0,y0 equations...

    Going from 1 frame in 3 hours to 15 fps would be a challenge!· But, I've done no optimizations at all...
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 01:03
    The modules I think would be a big hit but I think that you should do them on your website instead of E-bay. This way everyone would know how to order them. You should also edit your account in the control panel and type in your personal homepage so people know where to find it. Ho much do you think each mod would cost?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • sylvie369sylvie369 Posts: 1,622
    edited 2009-04-20 02:09
    Rayman said...
    It's easy to zoom into any part by modifying the x0,y0 equations...

    Going from 1 frame in 3 hours to 15 fps would be a challenge!· But, I've done no optimizations at all...
    When I did this years ago, I read that one of the standard shortcuts was to compute points around an imaginary boundary, and if they were all the same, simply fill in the inner part with the same color, rather than compute each point separately. I never did quite understand how that would be done, given that the point of the Mandelbrot is the infinitely complex boundaries, so I never spent any time trying to figure it out, but that's a 20+ year old optimization idea that should help considerably, and oughta be pretty well documented by now, I'd imagine.

    Nice project. It's beautiful.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-20 06:27
    1st optimisation:
    Let 8 COGs calculate in parallel. That's the easies you can do. Simply divide the picture in 8 pieces and let each COG calculate one part. Without other optimisations it would be done in ~20 minutes then.
    2nd optimisation:
    I think there is an algorithm which uses integer instead of float.

    I'm not sure if the prop can do zooming in realtime, but it should be possible doin it in seconds instead of hours. And we currently don't know if the demo is really calculating the fractal in realtime because demos use tricks. My guess is that the image is calculated in a higher resolution during runtime of another part of the demo. Then it's zooming into the higher resoltion, but static picture.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-04-20 20:14
    Sylvie:

    The optimization you're speaking of is actually quite easy. You start by subdividing the screen into 4 quadrants, and compute the color of every pixel of the outline of each quadrant (just a rectangle). If any of the rectangles contain a value that is different than the others, you subdivide that rectangle and repeat the process. If all the pixels on the boundary of the rectangle are the same, you just fill with the solid color and move on. Below the first few levels of subdivision, you hit a lot of rectangles that are solid-filled, so it can be quite a large speedup.

    I wrote a real-time Julia set once using a few different optimization tricks, this being one of them. You moved the mouse to change the parameters of the set, so it was pretty obviously realtime. [noparse]:)[/noparse]

    Jason
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 21:54
    It shouldn't be too stressful for the propeller to zoom in real time without it taking too long. The program dosn't look like it takes up too much space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • rokickirokicki Posts: 1,000
    edited 2009-04-20 22:55
    Doing this computation with ints is not terribly difficult, because the loop exit condition
    is when the values get too large --> when ints threaten to overflow.

    Someone should do this; 8-way (initially, perhaps, each cog gets a scanline) and
    a quick int recasting and just see how it goes.

    I too coded up a Mandelbrot program many years ago on the Amiga.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-04-20 23:07
    You should design this to interface with a keyboard and as you press the up and down arrows it zooms in and out. You could make the x and y variables that are added by 5 every time an up key is pressed and a minus 5 every time the down key is pressed. This would adjust them to make them bigger; appearing as a zoomed in image. This works the same for zooming out it makes the picture smaller; appearing as a zoomed out picture. idea.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Toys are microcontroled.
    Robots are microcontroled.
    I am microcontrolled.
  • RaymanRayman Posts: 14,826
    edited 2009-04-21 00:21
    I tried doing fixed point, but must have been doing something wrong because it didn't work...
    Here's what I had, before I gave up and switched to floating point:
    con
      points=12
     
    pub MandelBrot|i,j,x0,y0,x,y,xtemp,maxiter,iter,itest,c
    maxiter:=500
    itest:=4<<(2*points)
    
    repeat i from 0 to 319 step 4 
      repeat j from 0 to 239 step 4
        x0:=(((i-160)<<points)/1600)*35
        x:=0
        y0:=(((j-120)<<points)/1200)*35
        y:=0
        iter:=0
        repeat while (((x*x)+(y*y))=<itest) and (iter<maxiter)
          xtemp:=(((x*x)-(y*y))~>(2*points))+x0
          y:=2*((x*y)~>(2*points))+y0
          x:=xtemp
          iter++
        if iter==maxiter
          lcd.setpixel(i,j,0)
        else
          case iter
            0..10:
              c:=lcd.rgb(255,0,0)
            11..100:
              c:=lcd.rgb(255,255,0)
            101..500:
              c:=lcd.rgb(0,255,255)
            other:
              c:=lcd.rgb(0,0,255)
          lcd.setpixel(i,j,c)
    
Sign In or Register to comment.