daily pastebin goal
15%
SHARE
TWEET

JuliaFractal.java | Class to create Julia Set fractals

abdulfatirs May 15th, 2014 316 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ComplexNumber.java is here: http://pastebin.com/FrG7gDDx
  2. import plane.complex.ComplexNumber;
  3. import java.io.IOException;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.InputStreamReader;
  7. import javax.imageio.ImageIO;
  8. import java.awt.Color;
  9. import java.awt.image.BufferedImage;
  10. import static java.lang.System.out;
  11.  
  12. /**
  13.  * <code>JuliaFractal</code> is an executable class which can create Julia set fractals for input constant ComplexNumber.
  14.  * @author      Abdul Fatir
  15.  * @version             1.0
  16.  *
  17.  */
  18.  
  19. public class JuliaFractal
  20. {
  21.         public static void main(String args[])throws IOException
  22.         {
  23.                 // Taking the Image WIDTH and HEIGHT variables. Increasing or decreasing the value will affect computation time.
  24.                 double WIDTH = 1600;
  25.                 double HEIGHT = 1200;
  26.                
  27.                 // Setting the Saturation of every pixel to maximum
  28.                 // This can be played with to get different results
  29.                 float Saturation = 1f;
  30.                
  31.                 // Creating a new blank RGB Image to draw the fractal on
  32.                 BufferedImage img = new BufferedImage((int)WIDTH, (int)HEIGHT,BufferedImage.TYPE_3BYTE_BGR);
  33.                 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  34.                
  35.                 // Getting the constant ComplexNumber as input from the user for use in the function f(z) = z + c
  36.                 out.print("Re(c): ");
  37.                 double cReal = Double.parseDouble(reader.readLine());
  38.                 out.print("Im(c): ");
  39.                 double cImag = Double.parseDouble(reader.readLine());
  40.                
  41.                 // Creating the constant complex number from input real and imaginary values
  42.                 ComplexNumber constant = new ComplexNumber(cReal,cImag);
  43.                
  44.                 // Setting the maximum iterations to 256. This can be increased if you suspect an escapee set may be found beyond this value.
  45.                 // Increasing or decreasing the value will affect computation time.
  46.                
  47.                 int max_iter = 256;
  48.                
  49.                 // Looping through every pixel of image
  50.                 for(int X=0; X<WIDTH; X++)
  51.                 {
  52.                         for(int Y=0; Y<HEIGHT; Y++)
  53.                         {
  54.                                 // Creating an empty complex number to hold the value of last z
  55.                                 ComplexNumber oldz = new ComplexNumber();
  56.                                
  57.                                 // Setting the value of z0 for every pixel
  58.                                 // z0 is a function of (x,y) i.e. the pixel co-ordinates.
  59.                                 // The function can be anything, but should depend on (x,y) in some way and should lie between [-1,1]
  60.                                 // I use the function,
  61.                                 // Re(z) = 2*(X-WIDTH/2)/(WIDTH/2)
  62.                                 // Im(z) = 1.33*(Y-HEIGHT/2)/(HEIGHT/2)
  63.                                 // This gives a good centered fractal.You can play around with the function to get better results.
  64.                                
  65.                                 ComplexNumber newz = new ComplexNumber(2.0*(X-WIDTH/2)/(WIDTH/2), 1.33*(Y-HEIGHT/2)/(HEIGHT/2) );
  66.                                
  67.                                 // Iterating till the orbit of z0 escapes the radius 2 or till maximum iterations are completed
  68.                                 int i;
  69.                                 for(i=0;i<max_iter; i++)
  70.                                 {
  71.                                         // Saving the current z in oldz
  72.                                         oldz = newz;
  73.                                        
  74.                                         // Applying the function newz = newz^2 + c, where c is the constant ComplexNumber user input
  75.                                         newz = newz.square();
  76.                                         newz.add(constant);
  77.                                        
  78.                                         // Checking if the modulus/magnitude of complex number has exceeded the radius of 2
  79.                                         // If yes, the pixel is an escapee and we break the loop
  80.                                         if(newz.mod() > 2)
  81.                                                 break;
  82.                                 }
  83.                                
  84.                                 // Checking if the pixel is an escapee
  85.                                 // If yes, setting the brightness to the maximum
  86.                                 // If no, setting the brightness to zero since the pixel is a prisoner
  87.                                 float Brightness = i < max_iter ? 1f : 0;
  88.                                
  89.                                 // Setting Hue to a function of number of iterations (i) taken to escape the radius 2
  90.                                 // Hue = (i%256)/255.0f;
  91.                                 // i%256 to bring i in range [0,255]
  92.                                 // Then dividing by 255.0f to bring it in range [0,1] so that we can pass it to Color.getHSBColor(H,S,B) function
  93.                                 float Hue = (i%256)/255.0f;
  94.                                
  95.                                 // Creating the color from HSB values and setting the pixel to the computed color
  96.                                 Color color = Color.getHSBColor(Hue, Saturation, Brightness);
  97.                                 img.setRGB(X,Y,color.getRGB());
  98.                         }
  99.                 }
  100.                 // Saving the image
  101.                 ImageIO.write(img,"PNG", new File("julia.png"));
  102.         }
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top