How to make the Julia 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
+ c
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 Julia fractal, I make the variable c a constant.
and z0 to be a point on the graph.
Source code
Here is a code fragment written in Java (easily converted to C++)
class JuliaPanel extends JPanel
{
public void paintComponent(Graphics g)
{
double
GraphTop=1.5,GraphBottom=-1.5,GraphLeft=-1.5,GraphRight=1.5;
int x,y,iterations;
Rectangle r = getBounds();
double IncrementX=((GraphRight-GraphLeft)/(r.width-1));
double IncrementY=((GraphTop-GraphBottom)/(r.height-1));
double Zx,Zy,ConstantX,ConstantY=GraphTop,SquaredX,SquaredY;
double ExamineReal=0.32,ExamineImaginary=0.043;//constants to make the
fractal
for(y=0;y<r.height;y++)
{
ConstantX=GraphLeft;
for(x=0;x<r.width;x++)
{
iterations=0;
Zx=ConstantX;Zy=ConstantY;
SquaredX=Zx*Zx;SquaredY=Zy*Zy;
do
{
Zy=Zx*Zy;
Zy=Zy+Zy+ExamineImaginary;
Zx=SquaredX-SquaredY+ExamineReal;
SquaredX=Zx*Zx;SquaredY=Zy*Zy;
iterations++;
}while((iterations<256)&&((SquaredX+SquaredY)<4.0));
iterations--;
g.setColor(new Color(0,iterations,0));
g.drawLine(x,y,x,y);
ConstantX+=IncrementX;
}
ConstantY-=IncrementY;
}
}
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 =
Zx2-Zy2 +
2(Zx×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.
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 that I am
adding constants called ExamineReal and ExamineImaginary. In the above,
I just use 0.32 + 0.043i but you can choose others. There are numerous
numbers which you can find to make all sorts of interesting shapes.
Here is some code for the Casio calculator. Note that it generates this
fractal very slowly. It looks almost identical to the Mandelbrot set souce code.
ViewWindow -2,1.5,.5,-1.5,1.5,.5
-.32-.043i→D
For Ymin→T To Ymax Step (Ymax-Ymin)÷62
For Xmin→S To Xmax Step (Xmax-Xmin)÷125
S+Ti→C
1→I
Do
C2+D→C
1+I→I
LpWhile Abs C≤2 And I<20
Abs C≤2⇒Plot S,T
Next
Next
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.
|