// Mandelbrot.java (Ver.1.00) // Mandelbrot Fractal Drawing class // Copyright (C) 1997 Y.Nagatani // nagatani@eken.phys.nagoya-u.ac.jp import java.awt.*; public class Mandelbrot { public double realStart = -2.1, realWidth = 3.0, imagStart = -1.5, imagWidth = 3.0; public int xMax = 200, yMax = 200; public boolean bAbsolute = true; public int nNestLevel = 64; public boolean bLowResolution = true;; Mandelbrot (int xMax, int yMax) { this.xMax = xMax; this.yMax = yMax; } public Mandelbrot setRegion (double realStart, double imagStart, double realWidth, double imagWidth) { this.realStart = realStart; this.imagStart = imagStart; this.realWidth = realWidth; this.imagWidth = imagWidth; return this; } public Mandelbrot setNestLevel (int nNestLevel) { this.nNestLevel = nNestLevel; return this; } public Mandelbrot setAbsoluteMode () { this.bAbsolute = true; return this; } public Mandelbrot setPhaseMode () { this.bAbsolute = false; return this; } public Mandelbrot setLowResolution () { this.bLowResolution = true; return this; } public Mandelbrot setHighResolution () { this.bLowResolution = false; return this; } private double GetFracAbsolute (double r0, double i0) { int n; double r, i, r1; r = r0; /* Z(0) = c */ i = i0; for (n = nNestLevel ; n > 0 ; n--) { r1 = r0 + r*r - i*i; /* Z(n+1) = c + z(n)^2 */ i = i0 + 2.0*r*i; r = r1; if (r*r + i*i > 4.0) /* continue if |Z| < 4 */ break; } return 1.0 * n / nNestLevel; } private double GetFracPhase (double r0, double i0) { double r, i, r1; r = 0; i = 0; for (int n = 0 ; n < nNestLevel ; n++) { r1 = r0 + r*r - i*i; i = i0 + 2.0*r*i; r = r1; } double arg; if (Math.abs(i) < 0.0000000000001) arg = (r > 0 ? 0 : Math.PI); else arg = Math.atan2 (i, r); return arg / 2.0 / Math.PI; } public void Put1Line (int y, Graphics g) { double i = imagStart + imagWidth * (yMax - y) / yMax; for (int x = 0 ; x < xMax ; x++) { double r = realStart + realWidth * x / xMax; double f; if (bAbsolute) f = GetFracAbsolute (r, i) * 0.8; else f = GetFracPhase (r, i); g.setColor (new Color (Color.HSBtoRGB ( (float)f, (float)1.0, (float)1.0))); g.drawLine (x, y, x+1, y); if (bLowResolution) { g.drawLine (x, y+1, x+1, y+1); x++; } } } int y = 0; public void SubPaintInit () { y = 0; } public boolean SubPaint (Graphics g) { for (int n = 0 ; n < 5 ; n++) { Put1Line (y++, g); if (y >= yMax) { y = 0; return false; // completed } if (bLowResolution) y++; } return true; // uncompleted } public void paint (Graphics g) { this.SubPaintInit (); while (SubPaint (g)); } }