java – Generating colorful Mandelbrot set wallpapers


The following is the program 3.2.6. from the book Computer Science An Interdisciplinary Approach by Sedgewick & Wayne:

// This data type is the basis for writing Java programs that manipulate complex numbers.    
public class Complex
{
    private final double re;
    private final double im;

    public Complex(double real, double imag)
    { re = real; im = imag; }
    public double re() 
    { return re; }
    public double im()
    { return im; }
    public double abs()
    { return Math.sqrt(re*re + im*im); }
    public Complex plus(Complex b)
    {
        double real = re + b.re;
        double imag = im + b.im;
        return new Complex(real, imag);
    }
    public Complex times(Complex b)
    {
        double real = re*b.re - im*b.im;
        double imag = re*b.im + im*b.re;
        return new Complex(real, imag);
    }
    public String toString()
    {
        return re + " + " + im + "i";
    }
    public static void main(String() args)
    {
        Complex z0 = new Complex(1.0, 1.0);
        Complex z = z0;
        z = z.times(z).plus(z0);
        z = z.times(z).plus(z0);
        System.out.println(z);
    }
}

The next section in the book discusses the creation of Mandelbrot set in black-and-white. But before studying the program written by the authors of the book I tried to implement my own program to draw the Mandelbrot set in color.

Here is my program:

import java.awt.Color;

public class MandelbrotSet 
{
    public static int checkDegreeOfDivergence(Complex c, int degree)
    {
        Complex nextRecurrence = c;
        for (int i = 0; i < degree; i++)
        {
            if (nextRecurrence.abs() >= 2) return i;
            nextRecurrence = nextRecurrence.times(nextRecurrence).plus(c);
        }
        return degree;
    }
    public static double randomize(double left, double right)
    {
        return left + Math.random()*(right-left);
    } 
    public static Color() createRandomColors(int degree)
    {
        Color() colors = new Color(degree+1);
        colors(degree) = new Color(0,0,0);
        double r = Math.random();
        int red = 0, green = 0, blue = 0;
        if (r < 1.0/3.0) 
        {
            for (int i = 0; i < degree; i++)
            {
                red = 255;
                green = (int) randomize(0,255);
                blue = (int) randomize(0,255);
                colors(i) = new Color(red,green,blue);
            }
        }
        else if (r < 2.0/3.0) 
        {
            for (int i = 0; i < degree; i++)
            {
                red = (int) randomize(0,255);
                green = 255;
                blue = (int) randomize(0,255);
                colors(i) = new Color(red,green,blue);
            }
        }
        else if (r < 3.0/3.0) 
        {
            for (int i = 0; i < degree; i++)
            {
                red = (int) randomize(0,255);
                green = (int) randomize(0,255);
                blue = 255;
                colors(i) = new Color(red,green,blue);
            }
        }
        return colors;
    }
    public static void main(String() args)
    {
        int width = Integer.parseInt(args(0));
        int height = Integer.parseInt(args(1));
        int contrast = Integer.parseInt(args(2));
        double x = Double.parseDouble(args(3));
        double y = Double.parseDouble(args(4));
        double zoom = Double.parseDouble(args(5));
        Picture mandelbrotSet = new Picture(width,height);
        Color() colors = createRandomColors(contrast);
        for (int j = 0; j < width; j++)
        {
            for (int i = 0; i < height; i++)
            {
                double realPart = x + zoom*j/width;
                double imaginaryPart = y + zoom*i/height;
                Complex c = new Complex(realPart,imaginaryPart);
                int degreeOfDivergence = checkDegreeOfDivergence(c, contrast);
                Color color = colors(degreeOfDivergence);
                mandelbrotSet.set(j,i,color);       
            }
        }
        mandelbrotSet.show();
    }
}

Picture is a simple API written by the authors of the book. I checked my program and it works. Here are a few instances of it:

Input: 3840 2160 255 -0.1015 0.833 0.01

Output:

enter image description here

Input 3840 2160 255 -0.2404 0.8354 0.001

Output:

enter image description here

Input: 3840 2160 255 0.1015 -0.633 0.01

Output:

enter image description here

One thing to note: The above pictures are not in the original resolution. Due to size restriction of uploads I took screenshots of the original images. I did not decrease the resolution of the above pictures directly from the command-line because I wanted them to look prettier.

One other thing to note: Since blue is my favorite color, they are intentionally in the blue spectrum.

Is there any way that I can improve my program?

Thanks for your attention.