ColorJulia.java


This is the syntax highlighted version of ColorJulia.java from 3.2 Creating Data Types of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.

/*************************************************************************
 *  Compilation:  javac ColorJulia.java
 *  Execution:    java ColorJulia a b
 *  Dependencies: Picture.java
 *  
 *  Plots the Julia for the complex point c = a + ib.
 *
 *  The set of points in the Julia set is connected if and only if
 *  c is in the Mandelbrot set.
 *
 *  % java ColorJulia -0.75 0.1 
 *
 *  % java ColorJulia -1.25 0
 *
 *  % java ColorJulia 0.1 0.7
 *
 *************************************************************************/

import java.awt.Color;

public class ColorJulia {


    // return number of iterations to check z is in the Julia set of c
    static int julia(Complex c, Complex z, int ITERS) {
        for (int t = 0; t < ITERS; t++) {
            if (z.abs() >= 2.0) return t;
            z = z.times(z).plus(c);
        }
        return ITERS - 1;
    }


    public static void main(String args[]) {
        double real = Double.parseDouble(args[0]);      // a
        double imag = Double.parseDouble(args[1]);      // b
        Complex c = new Complex(real, imag);            // c = a + ib
        double xmin   = -2.0;
        double ymin   = -2.0;
        double width  =  4.0;
        double height =  4.0;

        int WIDTH  = 250;
        int HEIGHT = 250;
        int ITERS  = 256;

        // read in color map
        Color[] colors = new Color[ITERS];
        for (int t = 0; t < ITERS; t++) {
            int r = StdIn.readInt();
            int g = StdIn.readInt();
            int b = StdIn.readInt();
            colors[t] = new Color(r, g, b);
        }
        Picture pic = new Picture(WIDTH, HEIGHT);

        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                double x = xmin + i * width / WIDTH;
                double y = ymin + j * height / HEIGHT;
                Complex z = new Complex(x, y);
                int t = julia(c, z, ITERS);
                pic.setColor(i, j, colors[t]);
            }
        }
        pic.show();
        pic.save("julia.png");
    }

}


Last updated: Sat Aug 7 07:50:01 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.