分形解释
一段时间以来,我对分形、它们背后的数学以及它们可以产生的视觉效果很感兴趣。
我只是无法真正弄清楚如何将数学公式映射到绘制图片的一段代码。
给定曼德尔布罗集合的公式:Pc(z) = z * z + c
与以下代码相比如何:
$outer_adder = ($MaxIm - $MinIm) / $Lines;
$inner_adder = ($MaxRe - $MinRe) / $Cols;
for($Im = $MinIm; $Im <= $MaxIm; $Im += $outer_adder)
{
$x=0;
for($Re = $MinRe; $Re <= $MaxRe; $Re += $inner_adder)
{
$zr = $Re;
$zi = $Im;
for($n = 0; $n < $MaxIter; ++$n)
{
$a = $zr * $zr;
$b = $zi * $zi;
if($a + $b > 2) break;
$zi = 2 * $zr * $zi + $Im;
$zr = $a - $b + $Re;
}
$n = ($n >= $MaxIter ? $MaxIter - 1 : $n);
ImageFilledRectangle($img, $x, $y, $x, $y, $c[$n]);
++$x;
}
++$y;
}
代码不完整,为了简洁起见,仅显示了主要迭代部分。
所以问题是:有人可以向我解释一下数学与代码的比较吗?
编辑:需要明确的是,我找到了数十种解释数学的资源,以及数十种显示代码的资源,但我找不到将两者结合在一起的良好解释。
For a while now I've been interested in fractals, the math behind them and the visuals they can produce.
I just can't really figure out how to map the mathematical formula to a piece of code that draws the picture.
Given this formula for the mandelbrot set: Pc(z) = z * z + c
How does that compare to the following code:
$outer_adder = ($MaxIm - $MinIm) / $Lines;
$inner_adder = ($MaxRe - $MinRe) / $Cols;
for($Im = $MinIm; $Im <= $MaxIm; $Im += $outer_adder)
{
$x=0;
for($Re = $MinRe; $Re <= $MaxRe; $Re += $inner_adder)
{
$zr = $Re;
$zi = $Im;
for($n = 0; $n < $MaxIter; ++$n)
{
$a = $zr * $zr;
$b = $zi * $zi;
if($a + $b > 2) break;
$zi = 2 * $zr * $zi + $Im;
$zr = $a - $b + $Re;
}
$n = ($n >= $MaxIter ? $MaxIter - 1 : $n);
ImageFilledRectangle($img, $x, $y, $x, $y, $c[$n]);
++$x;
}
++$y;
}
Code is not complete, just showing the main iteration part for brevity.
So the question is: could someone explain to me how the math compares to the code?
Edit: To be clear, I've found dozens of resources explaining the math, and dozens of resources showing code, but nowhere can I find a good explanation of the two combined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明。我以前对分形一无所知,但一直想知道,所以我阅读了 wikipedia 文章< /a> 并决定写下我在这里找到的内容。
正如他们所说,如果你想理解某件事,请尝试向别人解释它。 ;)
好的,我们要对复数进行运算。复数实际上是一对(实数)数字,因此,对于我们 PHP 程序员来说,让它成为一个双元素数组。
现在我们需要告诉 php 如何对我们的复数进行算术运算。我们需要加法、乘法和 mod(“范数”)运算符。 (请参阅 http://mathworld.wolfram.com/topics/ComplexNumbers.html 了解更多信息细节)。
现在我们编写一个函数,如果给定(复数)点 $c 属于 mandelbrot 集合,则返回 true
如果所有点
z = z^2 + c<,则点
c
属于该集合/code> 位于半径为 2 的圆内。z的模
> 2 - 也就是说,我们超出了圆圈 - 该点不在集合中,为了防止无限循环,请限制最大迭代次数。
其余的与数学无关,相当明显
结果
当然,这段代码效率低到了极点。它的唯一目的是理解数学概念。
Disclaimer. I didn't know anything about fractals before, but always wanted to know, so I've read the wikipedia article and decided to write what I've found here.
As they say, if you want to understand something, try explaining it to someone else. ;)
Okay, we're going to operate on complex numbers. A complex number is actually a pair of (real) numbers, so, for us php programmers, let it be a two-elements array.
Now we need to tell php how to do arithmetics on our complex numbers. We'll need addition, multiplication and the mod ("norm") operator. (see http://mathworld.wolfram.com/topics/ComplexNumbers.html for more details).
Now we write a function that returns true if the given (complex) point $c belongs to the mandelbrot set
A point
c
belongs to the set if all pointsz = z^2 + c
lie inside the circle with the radius 2.modulus of z
> 2 - that is, we're out of the circle - the point is NOT in setTo prevent that from looping endlessly, limit the max number of iterations.
The rest has nothing to do with math and is quite obvious
Result
Of course, this code is ineffective to the extreme. Its only purpose is to understand the math concept.