/* A class for drawing a Mandelbrot Fractal. This class uses the base method in Fractal, and adds a custom Render method to draw the fractal. */ public class Mandelbrot extends Fractal { public Mandelbrot() { super(); } public void render() { // get our pixels[] array loadPixels(); // iterations we done so far for the pixel int n = 0; for (int y = 0; y < height; y++) { // figure out the Imaginary Number component double c_IM = max_IM - y * step_IM; for (int x = 0; x < width; x++) { // figure out the Real Number component double c_RE = min_RE + x * step_RE; // We'll use z_RE and z_IM to iterate through the Tower Function double z_RE = c_RE; double z_IM = c_IM; // Is this pixel in the Fractal boolean isInside = true; for (n = 0; n < maxIterations; n++) { // Get the sqaure of our complex number double z_RE2 = z_RE * z_RE; double z_IM2 = z_IM * z_IM; // check if we are out of the bounds of the fractal if (z_RE2 + z_IM2 > 4) { isInside = false; break; } // store the iteration and continue z_IM = 2 * z_RE * z_IM + c_IM; z_RE = z_RE2 - z_IM2 + c_RE; } if (isInside) { // The pixel was inside the fractal, so color it appropriately pixels[x + y * width] = inColor.getColor(); } else { /* We do some quick math here to figure out how far outside of the fractal our point is. We'll do this by figuring out how many iterations it took to exclude this pixel from the set, and colorize based upon that. */ int mi = maxIterations / 2; ColorRGB c; if (n < mi) { // Pixel is far outside of the series, use a blended color // between the outside color and the midpoint color c = outColor.blendColor(outColor, midColor, 0, mi - 1, n); } else { // Pixel was moderately outside teh fractal, use a blended color // between the mid point and the edge of the Fractal c = midColor.blendColor(midColor, edgeColor, mi, maxIterations, n); } // set the color pixels[x + y * width] = c.getColor(); } } } updatePixels(); } }