How to make the Burning Ship fractal
I am going to jump straight in to this one. This fractal is just
a slightly modified equation of the Mandelbrot set. See my article on
the Mandelbrot set to understand more about this fractal.
The equation used for the Mandelbrot is:
zn+1 = zn2
Set z0 = 0 and c
to a point on the graph. Keep
applying the equation until |z|>2 or you have
iterated a certain number of times.
To make the Burning Ship fractal, I make sure that the squaring part is
positive, explained below the source code.
The equation is:
zn+1 = (|Re(zn) | + i|Im(zn)|)2 + c, z0 = 0
Here is a code fragment written in Java (easily converted to C++)
class BurningShipPanel extends JPanel
public static final int MAX_ITERATIONS=256;//maximum 256 for
a palette from 0 to 255
public void paintComponent(Graphics g)
of the graph
int x,y,iterations; //x and y are the current pixel to draw
Rectangle r = getBounds();//this is just to get the applet dimensions
float IncrementX=((GraphRight-GraphLeft)/(r.width-1));//this is the
increment on the graph corresponding to one pixel
int palette = new int;
palette[n]<<8 | palette[n];
* The maximum value should be
255.??? so the (int) conversion
* should give 255 as the
//The last colour index is for values clearly within the BurningShip
set, coloured black
CoordReal and CoordImaginary are the variables of the point on the
graph. Remember that (0,0) on the
screen correspond to GraphLeft,GraphTop
SquaredX and SquaredY are used just to speed up the program
CoordReal=GraphLeft;//reset the variable
when you start a new line
Zx=CoordReal;Zy=CoordImaginary;//prepare to calculate z^2 + c
SquaredX=Zx*Zx;SquaredY=Zy*Zy; //set the initial squares
//(Zx + Zyi)^2 = Zx*Zx - Zy * Zy + 2Zx *
Zy to itself removes a slower times two multiply operation
< 2 squaring both sides gives n < 4
iterations--;//iterations would've otherwise been from 1 to
CoordReal+=IncrementX;//Increment to the next place on the graph
CoordImaginary-=DecrementY;//Go down one
line on the graph
I have made a non-linear palette just to brighten it up. It is made
just for 256 iterations only so if you lessen the iterations, you
should change this.
I made this to iterate a maximum of 256 times but you can do one that
iterates 50 times with perfectly good results and of course it will be
I have set a variable to Zx*Zy just so I do not have to multiply these
again in the loop.
Now, to square a complex number, I expand this equation:
(Zx + Zyi)2 =
Zx × Zx + Zx × Zy
+Zx × Zy - Zy×Zy =
The real part is Zx2-Zy2.
It is quicker to multiply them together (the Zx*Zx part) than use a
function for raising a number to another.
imaginary part is 2(Zx×Zy). It is quicker to set a
n = Zx*Zy then set n = n + n to avoid multiplying by two (adding is
quicker than multiplying). Zy is a floating point number so I cannot do
a bit shift left to multiply by two.
Now the part that is different to the Mandelbrot set is this:
making this positive, the above fractal is generated. The squares Zx*Zx
and Zy*Zy do not need the Math.abs() function as squaring a negative
number automatically makes it positive.
| Have you found an error or do you want to add more
information to these pages?
You can contact me at the bottom of the home page.