Mit Hilfe des folgenden Java-Schnipsels kann die Mandelbrotmenge dargestellt werden. Für eine erfolgreiche Kompilierung wird allerdings das Java Utility Package von Aegidius Plüss benötigt.
Dazugehörender Artikel: Darstellung einer Julia- und Mandelbrotmenge mittels Java
import ch.aplu.util.*;
import java.awt.Color;
/**
* Diese Klasse zeichnet eine Mandelbrot-Menge.
*
* Benötigt: Java Utility Package
* http://www.aplu.ch/home/apluhomex.jsp?site=65
*
* Autor: Pascal Hollenstein <webmaster@zockerade.com>
* Version: 1.0
*/
class Mandelbrot {
/**
* Grenze, ab welcher der Punkt als divergiert angesehen werden soll.
*/
int grenze = 100;
/**
* Anzahl der Iterationen.
*/
int anzahl_iterationen;
/**
* Ergebnis, bestehend aus Real- und Imaginaerteil.
*/
double[] w = new double[2];
/**
* Laenge des Koordinatensystem.
*/
double laenge = 2.8;
/**
* Real- und Imaginaerteil der Konstante c.
*/
double[] c = {-2.05, 1.4};
/**
* Schritt in x-Richtung des Koordinatensystems.
*/
double schritt_x = (-c[0] + c[0] + laenge) * 0.002;
/**
* Schritt in y-Richtung des Koordinatensystems.
*/
double schritt_y = (c[1] - c[1] + laenge) * 0.002;
/**
* Instanziert ein neues GPanel von 500px Breite und 500px Hoehe.
*/
GPanel p = new GPanel("Mandelbrotmenge", c[0], c[0] + laenge, c[1] - laenge, c[1]);
/**
* Konstruktor der Mandelbrot-Klasse.
*
* @see #iterationen
* @see #koordinatenkreuz_anzeigen
*/
Mandelbrot() {
for (double i = c[0]; i <= c[0] + laenge; i = i + schritt_x) {
for (double j = c[1] - laenge; j <= c[1]; j = j + schritt_y) {
iterationen(i, j);
if (anzahl_iterationen > 0)
p.color(Color.white);
if (anzahl_iterationen > 5)
p.color(Color.pink);
if (anzahl_iterationen > 10)
p.color(Color.yellow);
if (anzahl_iterationen > 15)
p.color(Color.orange);
if (anzahl_iterationen > 20)
p.color(Color.red);
if (anzahl_iterationen > 25)
p.color(Color.green);
if (anzahl_iterationen > 30)
p.color(Color.blue);
if (anzahl_iterationen > 35)
p.color(Color.cyan);
if (anzahl_iterationen >= 40)
p.color(Color.black);
p.point(i, j);
}
}
koordinatenkreuz_anzeigen();
}
/**
* Diese Prozedur iteriert die Rechenvorschrift.
*
* @param c_1 Realteil eines Punktes im Koordinatensystem.
* @param c_2 Imaginaerteil eines Punktes im Koordinatensystem.
*
* @see #laenge
*/
void iterationen(double c_1, double c_2) {
anzahl_iterationen = 0;
w[0] = c_1;
w[1] = c_2;
while (laenge(c_1, c_2) < grenze && anzahl_iterationen < 40) {
double[] w_temp = {w[0], w[1]};
w[0] = (Math.pow(w_temp[0], 2) - Math.pow(w_temp[1], 2)) + c_1;
w[1] = (2 * w_temp[0] * w_temp[1]) + c_2;
anzahl_iterationen++;
}
}
/**
* Berechnet die Strecke zwischen einem Punkt und dessen Abbild.
*
* @param x Ein Punk P.
* @param y Ein Punkt P'.
*
* @return Strecke zwischen P und P'.
*/
double laenge(double x, double y) {
return Math.sqrt(Math.pow(w[0] - x, 2) + Math.pow(w[1] - y, 2));
}
/**
* Zeichnet ueber die Mandelbrotmenge ein Koordinatenkreuz.
*/
void koordinatenkreuz_anzeigen() {
p.color(Color.gray);
double[] n = new double[2];
n[0] = (c[0] + c[0] + laenge) * 0.5;
n[1] = (c[1] + c[1] - laenge) * 0.5;
p.line(n[0], (c[1] - laenge), n[0], c[1]);
p.line(c[0], n[1], (c[0] + laenge), n[1]);
double dy = (c[1] - c[1] + laenge) * 0.25;
double dx = (c[0] + laenge - c[0]) * 0.25;
for (int i = 1; i < 4; i++) {
p.line(n[0] - 5 * schritt_x, c[1] - laenge + i * dy, n[0] + 5 * schritt_x, (c[1] - laenge) + i * dy);
p.line(c[0] + i * dx, n[1] + 5 * schritt_y, c[0] + i * dx, n[1] - 5 * schritt_y);
if (i == 3 || i == 1) {
double zahl_y = Math.floor((c[1] - laenge + i * dy) * 10000) / 10000;
double zahl_x = Math.floor((c[0] + i * dx) * 10000) / 10000;
p.text(n[0] + 10 * schritt_x, c[1] - laenge + i * dy - 5 * schritt_y, "" + zahl_y);
p.text(c[0] + i * dx - 20 * schritt_x, n[1] - 20 * schritt_y, "" + zahl_x);
}
}
}
/**
* Das eigentliche Hauptprogramm.
*
* @param args Kommandozeilenparameter
*/
public static void main(String[] args) {
new Mandelbrot();
}
}