Ex12_3



/*-------------------------------
  Ex12_3.java
  色々描く:マンデルブロー集合
 --------------------------------*/
package jp_bunkyo;

import java.applet.*;
import java.awt.*;

public class Ex12_3 extends Applet {
	static final int WIDTH = 600, HEIGHT = 480;
//	private double xMin = -0.9, xMax = 2.1, yMin = -1.2, yMax = 1.2;	// 全体図
//	private int iterMax = 20;											// 全体図
	private double xMin = 0.6, xMax = 1.2, yMin = 0, yMax = 0.4;		// 拡大図
	private int iterMax = 60;											// 拡大図
	private Color[] colors;

	public void paint(Graphics g) {
		int i, j;
		double x, y, h;

		colors = new Color[iterMax+1];
		colors[0] = Color.black;

		for (i = 1; i < colors.length; i++) {
			h = (double)i / colors.length;
			colors[i] = Color.getHSBColor(1f/16*(i%16), 0.5f, 1f-(float)Math.pow(h,12));
		}

		for (i = 0; i <= WIDTH; i++) {
			x = xMin + (xMax - xMin) * i / WIDTH;
			for (j = 0; j <= HEIGHT; j++) {
				y = yMax + (yMin - yMax) * j / HEIGHT;
				g.setColor(colors[getCount(x, y) % colors.length]);
				g.fillRect(i, j, 1, 1);
			}
		}
	}

	private int getCount(double x, double y) {
		double a = x, b = y;
		for (int i = iterMax; i > 0; i--) {
			double a2 = a*a, b2 = b*b;
			if (a2+b2 > 4) return i;
			b = 2*a*b - y;
			a = a2 - b2 - x;
		}
		return 0;
	}
}