package cse1030; // NOTE: There is some use of magic numbers that I didn't have time to fix... import princeton.introcs.Picture; import java.awt.Color; import java.util.List; import java.util.ArrayList; public class Mandelbrot { public static List makeColorMap() { final int NCOLORS = 256; List cmap = new ArrayList(NCOLORS); cmap.add(new Color(0, 0, 0)); for (int i = 1; i < NCOLORS; i++) { final int R = 255; final int G = (7 * i) % NCOLORS; final int B = (5 * i) % NCOLORS; cmap.add(new Color(R, G, B)); } return cmap; } // return number of iterations to check if c = a + ib is in Mandelbrot set public static int mand(Complex z0, int max) { Complex z = z0; for (int t = 0; t < max; t++) { if (z.mag() > 2.0) return t; z = z.multiply(z).add(z0); } return max; } public static void main(String[] args) { double xc = -0.5; double yc = 0; double size = 2; if (args.length == 3) { xc = Double.parseDouble(args[0]); yc = Double.parseDouble(args[1]); size = Double.parseDouble(args[2]); } final int N = 512; // create N-by-N image final int MAX = 255; // maximum number of iterations List colorMap = makeColorMap(); System.out.println("starting"); Picture pic = new Picture(N, N); for (int i = 0; i < N; i++) { if (i % 8 == 0) { System.out.print("."); } for (int j = 0; j < N; j++) { double x0 = xc - size / 2 + size * i / N; double y0 = yc - size / 2 + size * j / N; Complex z0 = new Complex(x0, y0); int gray = MAX - mand(z0, MAX); Color color = colorMap.get(gray); pic.set(i, N - 1 - j, color); } } System.out.println("done"); pic.show(); } }