我对分形有很大的兴趣,但直到最近才有机会实现它们。我第一次实现了一个黑色和白色的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