User:Simpsons contributor/Mandelbrot creator code

Click on 'edit this page' to copy the source code below

package mandelbrot;

import java.awt.image.BufferedImage;

public class MandelbrotCreator {   BufferedImage image; private int TYPE = BufferedImage.TYPE_INT_RGB; private double xLength, yLength; private double xDifference, yDifference; private double xLocation, yLocation; private int iteration; private double xInside, yInside, xTemp, z, colour; private int red, green, blue, rgb; public MandelbrotCreator(int width, int height, double xMin, double xMax,                             double yMin, double yMax, int maxIteration, int multiple) {       //Get dimensions and create new blank image and plane image = new BufferedImage(width, height, TYPE); //Set the length of the x and y axes visible on the screen 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 the x and y locations to their starting points (xMin and yMin) xLocation = xMin; yLocation = yMin; /*Move accross the image in an x, y scanning fashion and work out *if that point on the complex plane is in or out of the set * x and y represent the image's pixel, xLocation and yLocation * represent the x and y coordinates on the complex plane*/ for(int x=0; x<width; x++) {           for(int y=0; y<height; y++) {               /*System.out.print("X-coordinate " + xLocation); System.out.print(" Y-coordinate " + yLocation); System.out.println;*/ /*                * Here: x and y are pixels on image * xLocation and yLocation are coordinates in                * the complex plane x = x0 = x co-ordinate of pixel y = y0 = y co-ordinate of pixel while ( x*x + y*y <= (2*2) AND  iteration < max_iteration ) {                  xtemp = x*x - y*y + x0                   y = 2*x*y + y0                   x = xtemp iteration = iteration + 1 }*/                 xInside = xLocation; yInside = yLocation; iteration = 0; //Escape time algorithm while(xInside*xInside + yInside*yInside <= 4 &&                     iteration < maxIteration) {                   xTemp = xInside*xInside - yInside*yInside + xLocation; yInside = 2*xInside*yInside + yLocation; xInside = xTemp; iteration++; }               //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(iteration == 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(xInside*xInside + yInside*yInside); colour = iteration + 1 - (Math.log(Math.log(z)) / Math.log(2)); //Convert "iteration" to new "colour" value Number colourNumber = multiple * colour; iteration = colourNumber.intValue; if(iteration <= 255) {                       red = 255; blue = 0; green = iteration; }                   if(iteration > 255 && iteration <= 510) {                       green = 255; blue = 0; red = 510 - iteration; }                   if(iteration > 510 && iteration <= 765) {                       green = 255; red = 0; blue = iteration - 510; }                    if(iteration > 765 && iteration <= 1020) {                       blue = 255; red = 0; green = 1020 - iteration; }                   if(iteration > 1020 && iteration <= 1275) {                       blue = 255; green = 0; red = iteration - 1020; }                   if(iteration > 1275) {                       red = 255; blue = 255; green = 0; }               } //End of non-black colour determine //Set individual red, green and blue values to one rgb value rgb = ((1 & 0xFF) <<24) | ((red & 0xFF) <<16) | ((green & 0xFF) <<8) | ((blue & 0xFF) <<0); //Draw pixel based on derived rgb value image.setRGB(x, height-y-1, rgb); //Move xLocation along to the next position on the complex plane yLocation = yLocation + yDifference; }           //Move yLocation along to the next position on the complex plane yLocation = yMin; xLocation = xLocation + xDifference; } //End of nested for loops } }