The equation used for the Mandelbrot is:

z

Set z

To make the Burning Ship fractal, I make sure that the squaring part is positive, explained below the source code.

The equation is:

z

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)

{

float GraphTop=1.5F,GraphBottom=-1.5F,GraphLeft=-2F,GraphRight=1.5F; //Range 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

float DecrementY=((GraphTop-GraphBottom)/(r.height-1));

float Zx,Zy,CoordReal,CoordImaginary=GraphTop,SquaredX,SquaredY;

int palette[] = new int[256];

for(int n=0;n<256;n++)

{

palette[n]=(int)(n+512-512*Math.exp(-n/50.0)/3.0);

palette[n]=palette[n]<<16 | palette[n]<<8 | palette[n];

/*

* The maximum value should be 255.??? so the (int) conversion

* should give 255 as the maximum value

*/

}

palette[255]=0;

//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

*/

for(y=0;y<r.height;y++)

{

CoordReal=GraphLeft;//reset the variable when you start a new line

for(x=0;x<r.width;x++)

{

iterations=0;

Zx=CoordReal;Zy=CoordImaginary;//prepare to calculate z^2 + c

SquaredX=Zx*Zx;SquaredY=Zy*Zy; //set the initial squares

do

{

//(Zx + Zyi)^2 = Zx*Zx - Zy * Zy + 2Zx * Zyi

Zy=Math.abs(Zx*Zy);

Zy=Zy+Zy-CoordImaginary;//adding Zy to itself removes a slower times two multiply operation

Zx=SquaredX-SquaredY+CoordReal;

SquaredX=Zx*Zx;SquaredY=Zy*Zy;

iterations++;

}while((iterations<MAX_ITERATIONS)&&((SquaredX+SquaredY)<4.0));//Squareroot(n) < 2 squaring both sides gives n < 4

iterations--;//iterations would've otherwise been from 1 to MAX_ITERATIONS

g.setColor(new Color(palette[iterations]));

g.drawLine(x,y,x,y);

CoordReal+=IncrementX;//Increment to the next place on the graph

}

CoordImaginary-=DecrementY;//Go down one line on the graph

}

}

{

public static final int MAX_ITERATIONS=256;//maximum 256 for a palette from 0 to 255

public void paintComponent(Graphics g)

{

float GraphTop=1.5F,GraphBottom=-1.5F,GraphLeft=-2F,GraphRight=1.5F; //Range 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

float DecrementY=((GraphTop-GraphBottom)/(r.height-1));

float Zx,Zy,CoordReal,CoordImaginary=GraphTop,SquaredX,SquaredY;

int palette[] = new int[256];

for(int n=0;n<256;n++)

{

palette[n]=(int)(n+512-512*Math.exp(-n/50.0)/3.0);

palette[n]=palette[n]<<16 | palette[n]<<8 | palette[n];

/*

* The maximum value should be 255.??? so the (int) conversion

* should give 255 as the maximum value

*/

}

palette[255]=0;

//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

*/

for(y=0;y<r.height;y++)

{

CoordReal=GraphLeft;//reset the variable when you start a new line

for(x=0;x<r.width;x++)

{

iterations=0;

Zx=CoordReal;Zy=CoordImaginary;//prepare to calculate z^2 + c

SquaredX=Zx*Zx;SquaredY=Zy*Zy; //set the initial squares

do

{

//(Zx + Zyi)^2 = Zx*Zx - Zy * Zy + 2Zx * Zyi

Zy=Math.abs(Zx*Zy);

Zy=Zy+Zy-CoordImaginary;//adding Zy to itself removes a slower times two multiply operation

Zx=SquaredX-SquaredY+CoordReal;

SquaredX=Zx*Zx;SquaredY=Zy*Zy;

iterations++;

}while((iterations<MAX_ITERATIONS)&&((SquaredX+SquaredY)<4.0));//Squareroot(n) < 2 squaring both sides gives n < 4

iterations--;//iterations would've otherwise been from 1 to MAX_ITERATIONS

g.setColor(new Color(palette[iterations]));

g.drawLine(x,y,x,y);

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 faster.

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)

Zx × Zx + Zx × Zy +Zx × Zy - Zy×Zy =

Zx

The real part is Zx

The imaginary part is 2(Zx×Zy). It is quicker to set a variable 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:

Zy=Math.abs(Zx*Zy);

By 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. |