我没有任何分形编程经验。当然,我看过著名的曼德尔布罗图像等。
你能为我提供简单的分形算法吗?
编程语言其实并不重要,但我最熟悉的是actionscript、C#、Java。
我知道如果我用谷歌搜索分形,我会得到很多(复杂的)信息,但我想从一个简单的算法开始并使用它。
也欢迎提出改进基本算法的建议,例如如何使它们具有可爱的颜色等。
最佳答案
对 Mandelbrot 进行编程很容易。
我的快速脏代码如下(不保证没有错误,但一个很好的轮廓)。
大纲如下: Mandelbrot 集完全位于半径为 2 的圆内的复杂网格中。
因此,首先扫描该矩形区域中的每个点。 每个点代表一个复数 (x + yi)。 迭代该复数:
[新值] = [旧值]^2 + [原始值]
,同时跟踪两件事:
1.) 迭代次数
2.) [new-value] 距原点的距离。
如果达到最大迭代次数,则完成。 如果距原点的距离大于 2,则完成。
完成后,根据您完成的迭代次数为原始像素着色。 然后移动到下一个像素。
public void MBrot()
{
float epsilon = 0.0001; // The step size across the X and Y axis
float x;
float y;
int maxIterations = 10; // increasing this will give you a more detailed fractal
int maxColors = 256; // Change as appropriate for your display.
Complex Z;
Complex C;
int iterations;
for(x=-2; x<=2; x+= epsilon)
{
for(y=-2; y<=2; y+= epsilon)
{
iterations = 0;
C = new Complex(x, y);
Z = new Complex(0,0);
while(Complex.Abs(Z) < 2 && iterations < maxIterations)
{
Z = Z*Z + C;
iterations++;
}
Screen.Plot(x,y, iterations % maxColors); //depending on the number of iterations, color a pixel.
}
}
}
遗漏的一些细节是:
1.) 准确了解复数的平方是什么以及如何计算它。
2.) 弄清楚如何将 (-2,2) 矩形区域转换为屏幕坐标。
关于theory - 如何编写分形编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/425953/