# Java Program to draw Mandelbrot Set And Julia

###
__Problem Statement:-__

A Mandelbrot Set is a set of complex number z that does not diverge under thetransformation

with

.Where, both x and z represent the complex

numbers. Write C++/Java program to

a). Plot the Mandelbrot set for the threshold |x|= 2.

b) Plot Julia set choosing z ≠ 0. Use 254 colors for plotting in both cases.

###
__Code:-__

import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import complex.*; public class MandelbrotSet { public static void main(String[] args) throws Exception { Julia(); jul(); mandelbrot(); } public static void mandelbrot() throws IOException { int width=1920,height=1080,MAX=100; BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); int black=0x000000; int white=0xFFFFFF; float sat=1f; for(int row=0;row<height;row++) { for(int col=0;col<width;col++) { double c_real=2.0*(col-(width/2))/(width/2);//(col-width/2)*4.0/width; double c_img=1.33*(row-(height/2))/(height/2);//(row-height/2)*4.0/width; int i=0; double x=0,y=0; while(x*x+y*y<4 && i<MAX) //Repeat while orbit exceeds rad=2 or max iterations { double x_new=x*x-y*y+c_real; y=2*x*y+c_img; x=x_new; i++; } float brightness=i<MAX?1f:0; float hue=(i)/255.0F; image.setRGB(col, row, Color.getHSBColor(hue, sat, brightness).getRGB()); } } ImageIO.write(image, "png", new File("vnk.png")); } public static void jul() throws Exception { int width=1920,height=1080,MAX=75; BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); int black=0x000000; int white=0xFFFFFF; float sat=1f; //==== Change these values to get different julia double c_real=-0.76; double c_img=0.11; for(int row=0;row<height;row++) { for(int col=0;col<width;col++) { //You can use commented equations if required//both are same double x=2.0*(col-(width/2))/(width/2); //(col-width/2)*4.0/width; double y=1.33*(row-(height/2))/(height/2); //(row-height/2)*4.0/width; int i=0; for(i=0;i<MAX;i++) { //int iterations=0; //double x=0,y=0; //while(x*x+y*y<4 && iterations<MAX) //Repeat while orbit exceeds rad=2 or max iterations //{ double x_new=x*x-y*y+c_real; y=2*x*y+c_img; x=x_new; //iterations++; if(x*x+y*y>4) break; } float brightness=i<MAX?1f:0; float hue=(i%256)/255.0F; image.setRGB(col, row, Color.getHSBColor(hue, sat, brightness).getRGB()); /* * * * if(i<MAX) //pixel is escapee image.setRGB(col, row, white); else //pixel is prisoner image.setRGB(col, row, (black)); * */ } } ImageIO.write(image, "png", new File("jull.png")); } }

###
__Output:-__

MandelBrot Output |

Julia Output |

###
__References:-__

__Theory:-__```
The difference between the Mandelbrot set (M-set) and Julia set
(J-set) is not in the formula, but in the type of iteration involved.
The complex number c is called a parameter. The M-set is plotted in
the *parameter space* of c, whereas the J-set is not. It is instead
plotted in the orbit space z. That is, for the M-set, you calculate:
0, c, c^2 + c, (c^2 + c)^2 + c, ...
for a given complex number c, and see if the sequence converges.
If it does, that point c in the complex plane is in the M-set. So what
you're plotting is a picture of all complex c such that the mapping
z |-> z^2 + c is bounded, where the initial condition is z = 0.
However, the J-set is plotted for a *single* value of c, but you are
now changing the initial condition:
z, z^2 + c, (z^2 + c)^2 + c, ....
The value of c is fixed for all points z. So in a J-set you're plotting
in the space of starting values of z.
In short, you plot the M-set by iterating over many different values
of c, whereas in the J-set you fix c and iterate over many different
values of z. In both cases, the formula for iteration is the same;
only two things change:
M-set: starting condition is z = 0
pick a point in the plane, c
iterate and see if bounded; if so then c is in the M-set
J-set: fix a number c -- this never changes
pick a point in the plane, z
iterate and see if bounded, if so then z is in the J-set.
So there are many J-sets, one for each c, but only one M-set. If you
think about it, the M-set is a "guide" to all the J-sets. What I mean
is that if a point c is in the M-set, then the J-set with starting
value c is connected. This is because there is a theorem which says
the point z = 0 is in a J-set if and only if that J-set is connected.
```

## No comments: