How to program a fractal?

后端 未结 14 962
花落未央
花落未央 2020-12-04 05:34

I do not have any experience with programming fractals. Of course I\'ve seen the famous Mandelbrot images and such.

Can you provide me with simple algorithms for fra

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 06:02

    The mandelbrot set is generated by repeatedly evaluating a function until it overflows (some defined limit), then checking how long it took you to overflow.

    Pseudocode:

    MAX_COUNT = 64 // if we haven't escaped to infinity after 64 iterations, 
                   // then we're inside the mandelbrot set!!!
    
    foreach (x-pixel)
      foreach (y-pixel)
        calculate x,y as mathematical coordinates from your pixel coordinates
        value = (x, y)
        count = 0
        while value.absolutevalue < 1 billion and count < MAX_COUNT
            value = value * value + (x, y)
            count = count + 1
    
        // the following should really be one statement, but I split it for clarity
        if count == MAX_COUNT 
            pixel_at (x-pixel, y-pixel) = BLACK
        else 
            pixel_at (x-pixel, y-pixel) = colors[count] // some color map. 
    

    Notes:

    value is a complex number. a complex number (a+bi) is squared to give (aa-b*b+2*abi). You'll have to use a complex type, or include that calculation in your loop.

提交回复
热议问题