User:Simpsons contributor/Mandelbrot set creator

package mandelbrotzoom;

import java.awt.image.BufferedImage;

public class RainbowMandelbrot {   private BufferedImage image; private int TYPE = BufferedImage.TYPE_INT_RGB; private double xLength, yLength; private double xDifference, yDifference; private double xLocation, yLocation; private int counter, rgb, red, green, blue; private Complex valueN = new Complex; private Complex valueNPlus1 = new Complex; private ComplexOperations operations = new ComplexOperations; private double real, imaginary, z, colour; private int width, height, maxIteration, mean; private double multiple; private double xMin, xMax, yMin, yMax; private double planeWidth, planeHeight; /*FileWriter dataFile; PrintWriter dataWriter; private long oldTime, newTime, difference;*/ public BufferedImage runWithMinMax(double widthIn, double heightIn, double xMinIn, double xMaxIn,                      double yMinIn, double yMaxIn, int maxIterationIn, double multipleIn) {       width = convert(widthIn); height = convert(heightIn); xMin = xMinIn; xMax = xMaxIn; yMin = yMinIn; yMax = yMaxIn; maxIteration = maxIterationIn; multiple = multipleIn; run; return image; }   public BufferedImage runWithCentreOnlyWidth(double widthIn, double heightIn, double planeWidthIn,                               double centreXIn, double centreYIn,                               int maxIterationIn, double multipleIn) {       width = convert(widthIn); height = convert(heightIn); planeHeight = (heightIn / widthIn) * planeWidthIn; planeWidth = planeWidthIn; xMin = centreXIn - (planeWidth / 2); xMax = centreXIn + (planeWidth / 2); yMin = centreYIn - (planeHeight / 2); yMax = centreYIn + (planeHeight / 2); /*System.out.println("Width " + planeWidth); System.out.println("Height " + planeHeight); System.out.println("X-min " + xMin); System.out.println("X-max " + xMax); System.out.println("Y-min " + yMin); System.out.println("Y-max " + yMax);*/ multiple = multipleIn; maxIteration = maxIterationIn; run; return image; }   public BufferedImage runWithCentreOnlyHeight(int widthIn, int heightIn, double planeHeightIn,                              double centreXIn, double centreYIn,                              int maxIterationIn, double multipleIn) {       width = convert(widthIn); height = convert(heightIn); planeWidth = (widthIn / heightIn) * planeHeightIn; planeHeight = planeHeightIn; xMin = centreXIn - (planeWidth / 2); xMax = centreXIn + (planeWidth / 2); yMin = centreYIn - (planeHeight / 2); yMax = centreYIn + (planeHeight / 2); /*System.out.println("Width " + planeWidth); System.out.println("Height " + planeHeight); System.out.println("X-min " + xMin); System.out.println("X-max " + xMax); System.out.println("Y-min " + yMin); System.out.println("Y-max " + yMax);*/ multiple = multipleIn; maxIteration = maxIterationIn; run; return image; }   public BufferedImage runWithCentre(int widthIn, int heightIn, double planeHeightIn,                                double planeWidthIn, double centreXIn, double centreYIn,                               int maxIterationIn, double multipleIn) {       width = convert(widthIn); height = convert(heightIn); planeWidth = planeWidthIn; planeHeight = planeHeightIn; xMin = centreXIn - (planeWidth / 2); xMax = centreXIn + (planeWidth / 2); yMin = centreYIn - (planeHeight / 2); yMax = centreYIn + (planeHeight / 2); /*System.out.println("Width " + planeWidth); System.out.println("Height " + planeHeight); System.out.println("X-min " + xMin); System.out.println("X-max " + xMax); System.out.println("Y-min " + yMin); System.out.println("Y-max " + yMax);*/ multiple = multipleIn; maxIteration = maxIterationIn; run; return image; }   private void run {          /*Create a new complex plane and BufferedImage * based upon the width and height dimensions*/ image = new BufferedImage(width, height, TYPE); /*The complex number plane has the dimensions of the image * [width] [height]. The values of the complex numbers in        * the complex number plane is different. Use the values of        * xMin, xMax, yMin and yMax to fill the plane with the * actual values of the plane. */       //Work out the length width of the complex plane xLength = xMax - xMin; yLength = yMax - yMin; /*Work out the difference on the x and y axes that results from * the move from one pixel to the next */ xDifference = xLength / width; yDifference = yLength / height; /*Set xLocation and Ylocation to xMin and yMin and then sweep * through the complex plane in an x-y fashion setting each real * and imaginary value on the way */ xLocation = xMin; yLocation = yMin; for(int x=0; x<width; x++) {           //oldTime = System.currentTimeMillis; for(int y=0; y<height; y++) {                                         counter = 0; //Create new complex and set it to coordinates valueN.setReal(xLocation); valueN.setImaginary(yLocation); valueNPlus1.setReal(xLocation); valueNPlus1.setImaginary(yLocation); //Get values real = valueN.realDouble; imaginary = valueN.imaginaryDouble; while(real*real + imaginary*imaginary <= 4 &&                      counter < maxIteration) {                    //Create (n+1) complex: x(n+1) = x(n)^2 + c                     valueNPlus1 = operations.multiply(valueNPlus1, valueNPlus1); valueNPlus1 = operations.add(valueNPlus1, valueN); //Get values real = valueNPlus1.realDouble; imaginary = valueNPlus1.imaginaryDouble; //Advance counter by one counter++; }                                 mean += counter; //Select a colour based on the iteration number and the hue line /*more than	less than or equal to	red	green	blue 0	       255	                255	0-255	0                255	        510	                255-0	255	0                510	        765	                0	255	0-255                765	        1020	                0	255-0	255                1020	        1275	                0-255	0	255                1275	        1530	                255	0	255-0*/                //Increase = iteration - lower bound //Decrease = upper bound - iteration if(counter == maxIteration) {                   red = 0; green = 0; blue = 0; //Black }               else //Non-black {                   /*   Normalized iteration count algorithm: _________                   |z| = \/x^2 + y^2

ln(ln(|z|)) n + 1 - --- ln 2        */ z = Math.sqrt(real*real + imaginary*imaginary); colour = counter + 1 - (Math.log(Math.log(z)) / Math.log(2)); //Convert "iteration" to new "colour" value Number colourNumber = multiple * colour; counter = colourNumber.intValue; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter > 1275) counter = counter / 2; if(counter <= 255) {                       red = 255; blue = 0; green = counter; }                   if(counter > 255 && counter <= 510) {                       green = 255; blue = 0; red = 510 - counter; }                   if(counter > 510 && counter <= 765) {                       green = 255; red = 0; blue = counter - 510; }                    if(counter > 765 && counter <= 1020) {                       blue = 255; red = 0; green = 1020 - counter; }                   if(counter > 1020 && counter <= 1275) {                       blue = 255; green = 0; red = counter - 1020; }                   if(counter > 1275) {                       red = 255; blue = 255; green = 0; }                } //End of non-black colour determine rgb = ((1 & 0xFF) <<24) | ((red & 0xFF) <<16) | ((green & 0xFF) <<8) | ((blue & 0xFF) <<0); image.setRGB(x, height-y-1, rgb); yLocation += yDifference; }           xLocation += xDifference; yLocation = yMin; } //End of nested for loops mean = mean / (width * height); }   public int getMean {       return mean; }   private int convert(double input) {       Number number = input; return number.intValue; } }