milgra
about
articles
projects
blogroll
bit-101
coding horror
blameit
failblog
beszeljukmac
sghu
navigation
posts
docs
admin
|
Creating a Mandelbrot-set
|
Mandelbrot-Set
A Mandelbrot set marks the set of points in the complex plane where z(i) = z(i-1)*z(i-1) + c function does not tend to infinity. These points marked black, but the nearby points, which aren't in the set, is colored according to how fast they tend to infinity. So we can draw very spectacular nutmeats :) And in addition we can zoom in almost continously ( based on the floating-point handling capacity of our platform :).
Complex numbers have two parts: a real and an imaginery part: z = ( r , i )
Addition: z1 + z2 = ( r1 + r2 , i1 + i2 )
Multiplication: z1 * z2 = ( r1 * r2 - i1 * i2 , r1 * i2 + r2 * i1 ).
Based on these operations we can simplify the Mandelbrot function.
z(2) = z(1) * z(1) + c
( z(2)r , z(2)i ) = ( z(1)r , z(1)i )( z(1)r , z(1)i ) + ( cr , ci );
( z(2)r , z(2)i ) = ( z(1)r * z(1)r - z(1)i * z(1)i , z(1)r * z(1)i + z(1)r * z(1)i ) + ( cr , ci );
z(2)r = z(1)r * z(1)r - z(1)i * z(1)i + cr;
z(2)i = z(1)r * z(1)i + z(1)r * z(1)i + ci = 2 * z(1)r * z(1)i + ci;
| zi | > 2 -> we have to stay in the circle with the radius 2
| zi | = sqrt( zr * zr + zi * zi ) > 2;
| zi | exp( 2 ) = zr * zr + zi * zi > 4
Let's translate it to AS3.
Source Code
Showcase
Try it. Beautiful, eh? You can make it more detailed, if you set CYCLES higher, but it will also be slower. Check it under attachments.
And what about that bitwise-shift thing? Simple, we needed a 24 bit length colour code from a number under 128. So, i shifted 128 with 16 bits, to the "red" range ( 2(25) - 2(16) ), then i added 50, because i wanted green to be the dominant color, then shifted left with 8 bits to the green range ( 2(15) - 2(8) ), and i left it in the "blue" range, and made a logical "OR" between these values.
Roto-Zoom
Well, let's make our set more spectacular. Let's zoom coordinate interval continously, and also rotate system to make a roto-zoom effect.
For roto-zoom we need five continously changing variables : the interval borders and the angle. Create a new function called rotoZoom, which modifies these values periodically, based on Stage's enterframe event. Rotation happens in drawset, before requesting a new coordinate from getCycles. Let's see:
Source Code
It is quite good now. But we should try to zoom to a central point, and we should move all local variables to global scope, because local variables in a loop - hell like this can eat plenty of memory and processor time.
My final version is:
Source Code
Showcase
Article on Actionscript.org
MilGra
|
|