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 fractals.
Programming language doesn't matter really, but I'm most familiar with actionscript, C#, Java.
I know that if I google fractals, I get a lot of (complicated) information but I would like to start with a simple algorithm and play with it.
Suggestions to improve on the basic algorithm are also welcome, like how to make them in those lovely colors and such.
The method is best suited for textures and natural images, relying on the fact that parts of an image often resemble other parts of the same image. Fractal algorithms convert these parts into mathematical data called "fractal codes" which are used to recreate the encoded image.
Programming the Mandelbrot is easy.
My quick-n-dirty code is below (not guaranteed to be bug-free, but a good outline).
Here's the outline: The Mandelbrot-set lies in the Complex-grid completely within a circle with radius 2.
So, start by scanning every point in that rectangular area. Each point represents a Complex number (x + yi). Iterate that complex number:
[new value] = [old-value]^2 + [original-value]
while keeping track of two things:
1.) the number of iterations
2.) the distance of [new-value] from the origin.
If you reach the Maximum number of iterations, you're done. If the distance from the origin is greater than 2, you're done.
When done, color the original pixel depending on the number of iterations you've done. Then move on to the next pixel.
public void MBrot() { float epsilon = 0.0001; // The step size across the X and Y axis float x; float y; int maxIterations = 10; // increasing this will give you a more detailed fractal int maxColors = 256; // Change as appropriate for your display. Complex Z; Complex C; int iterations; for(x=-2; x<=2; x+= epsilon) { for(y=-2; y<=2; y+= epsilon) { iterations = 0; C = new Complex(x, y); Z = new Complex(0,0); while(Complex.Abs(Z) < 2 && iterations < maxIterations) { Z = Z*Z + C; iterations++; } Screen.Plot(x,y, iterations % maxColors); //depending on the number of iterations, color a pixel. } } }
Some details left out are:
1.) Learn exactly what the Square of a Complex number is and how to calculate it.
2.) Figure out how to translate the (-2,2) rectangular region to screen coordinates.
You should indeed start with the Mandelbrot set, and understand what it really is.
The idea behind it is relatively simple. You start with a function of complex variable
f(z) = z2 + C
where z is a complex variable and C is a complex constant. Now you iterate it starting from z = 0, i.e. you compute z1 = f(0), z2 = f(z1), z3 = f(z2) and so on. The set of those constants C for which the sequence z1, z2, z3, ... is bounded, i.e. it does not go to infinity, is the Mandelbrot set (the black set in the figure on the Wikipedia page).
In practice, to draw the Mandelbrot set you should:
The astounding fact about fractals is how we can obtain a tremendously complex set (in particular, the frontier of the Mandelbrot set) from easy and apparently innocuous requirements.
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With