所以我一直在尝试用java编写mandelbrot集,我知道代码不是很优化,但我刚刚开始这样做。
这个想法是,当我单击一个像素时,它将将该像素放在中心(代表某个复数)并计算它周围的集合。这在开始时有效,但如果我放大它会开始表现得很奇怪。我猜这可能是我的 midX、midY 或显示功能很奇怪,但我已经看了很长时间了,无法弄清楚,希望能得到一些帮助。
class set {
DotWindow w;
int[][] arrayColor;
int max = 100;
Grayscale gray;
double zoom = 1.1;
double midX = -0.5;
double midY = 0;
public static void main(String[] args) {
new set().run();
}
void run() {
setup();
runLoop();
}
void runLoop() {
int x;
int y;
while (true) {
GameEvent event = w.getNextEvent();
switch (event.getKind()) {
case GameEvent.KEY_PRESSED:
int key = event.getKey();
if (key == 43) {
zoom = zoom * 1.1;
} else if (key == 45) {
zoom = zoom / 1.1;
}
display();
break;
case GameEvent.MOUSE_CLICKED:
midX = midX - (1 - event.getX() / 250.0);
midY = midY - (1 - event.getY() / 250.0);
System.out.println(midX);
display();
break;
}
}
}
void setup() {
w = new DotWindow(500, 500, 1);
w.checkMouse(true, false, false, false, false);
w.checkKeys(true, false, false);
arrayColor = new int[500][500];
zoom = zoom / 1.1;
display();
}
int calculate(double re, double im) {
double Zre = 0;
double Zim = 0;
double Zim2 = 0;
double Zre2 = 0;
int iterations = 0;
for (int k = 0; k < max; k++) {
if (Zre2 + Zim2 > 4.0) {
return k;
}
Zim2 = Zim * Zim;
Zre2 = Zre * Zre;
Zim = 2.0 * Zre * Zim + im;
Zre = Zre2 - Zim2 + re;
iterations = k;
}
return iterations;
}
void display() {
for (double y = 0; y < 500; y++) {
for (double x = 0; x < 500; x++) {
double value = calculate((midX - (1 - x / 250) / zoom),
(midY - (1 - y / 250) / zoom));
if (value == 99) {
w.setDot((int) x, (int) y, Color.BLACK);
} else {
w.setDot((int) x, (int) y, gray = new Grayscale(
255 - (int) value * 2));
}
}
}
}
}