我對分形有很大的興趣,但直到最近纔有機會實現它們。我第一次實現了一個黑色和白色的mandelbrot,然後我試着給它添加顏色。將顏色層添加到mandelbrot集
這裏是我的天壤之別的實現(我用org.apache.commons.math3.complex.Complex用於複數)
public class MyMandelbrot {
public static int numberOfIterationsToCheck(Complex z0, int max) {
Complex z = z0;
for (int t = 0; t < max; t++) {
if (z.abs() > 2.0) return t;
z =z.multiply(z).add(z0);
}
return max;
}
public static void main(String[] args) {
double xc = Double.parseDouble(args[0]);
double yc = Double.parseDouble(args[1]);
double size = Double.parseDouble(args[2]);
int N = 512;
int max = 255;
Viewer viewer = new Viewer(N, N);
for (int i = 0; i < N; i++) {
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 - numberOfIterationsToCheck(z0, max);
Color color = new Color(gray, gray, gray);
if (z0.abs() > 2.0) {
color = new Color(gray, 128, gray);
} else if (z0.abs() > 2.0 && numberOfIterationsToCheck(z0, max) > max/2) {
color = new Color(255, gray, 255);
} else if (z0.abs() > 2.0 && numberOfIterationsToCheck(z0, max) < max/2) {
color = new Color(gray, 128,128);
}
else if (z0.abs() > 1.0 && numberOfIterationsToCheck(z0, max) < max/2) {
color = new Color(128, gray, 128);
} else if (z0.abs() > 1.0) {
color = new Color(128, gray, 128);
}
else if (z0.abs() <= 1.0) {
color = new Color(gray, gray, 128);
}
viewer.set(i, N-1-j, color);
}
}
viewer.show();
}
}
我使用一個自定義瀏覽器類,以查看集繪圖之後一個圖像對象。這裏是查看器的設置方法
public void set(int col, int row, Color color) {
if (col < 0 || col >= width()) throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
if (color == null) throw new NullPointerException("can't set Color to null");
if (isOriginUpperLeft) image.setRGB(col, row, color.getRGB());
else image.setRGB(col, height - row - 1, color.getRGB());
}
代碼正確渲染集,但我沒有獲得預期的結果。我要的是能夠產生有色一套類似於這些
或者這
但我不能得到較好的有色集莫過於此。
我看了一下它here和here一些理論框架的解釋,但我明明做錯事的做法。我的着色方法有什麼問題?我該如何解決它?謝謝
謝謝你的答案。我會盡快申請 – alainlompo
好吧,讓我在一分鐘內申請,我會回來 – alainlompo
謝謝,這很好! – alainlompo