在 Mathematica 中迭代生成谢尔宾斯基三角形?
我编写了绘制谢尔宾斯基分形的代码。由于它使用递归,所以速度非常慢。你们中有人知道我如何在不使用递归的情况下编写相同的代码以使其更快吗?这是我的代码:
midpoint[p1_, p2_] := Mean[{p1, p2}]
trianglesurface[A_, B_, C_] := Graphics[Polygon[{A, B, C}]]
sierpinski[A_, B_, C_, 0] := trianglesurface[A, B, C]
sierpinski[A_, B_, C_, n_Integer] :=
Show[
sierpinski[A, midpoint[A, B], midpoint[C, A], n - 1],
sierpinski[B, midpoint[A, B], midpoint[B, C], n - 1],
sierpinski[C, midpoint[C, A], midpoint[C, B], n - 1]
]
编辑:
我用混沌游戏方法编写了它,以防有人感兴趣。感谢您的精彩回答! 这是代码:
random[A_, B_, C_] := Module[{a, result},
a = RandomInteger[2];
Which[a == 0, result = A,
a == 1, result = B,
a == 2, result = C]]
Chaos[A_List, B_List, C_List, S_List, n_Integer] :=
Module[{list},
list = NestList[Mean[{random[A, B, C], #}] &,
Mean[{random[A, B, C], S}], n];
ListPlot[list, Axes -> False, PlotStyle -> PointSize[0.001]]]
I have written code which draws the Sierpinski fractal. It is really slow since it uses recursion. Do any of you know how I could write the same code without recursion in order for it to be quicker? Here is my code:
midpoint[p1_, p2_] := Mean[{p1, p2}]
trianglesurface[A_, B_, C_] := Graphics[Polygon[{A, B, C}]]
sierpinski[A_, B_, C_, 0] := trianglesurface[A, B, C]
sierpinski[A_, B_, C_, n_Integer] :=
Show[
sierpinski[A, midpoint[A, B], midpoint[C, A], n - 1],
sierpinski[B, midpoint[A, B], midpoint[B, C], n - 1],
sierpinski[C, midpoint[C, A], midpoint[C, B], n - 1]
]
edit:
I have written it with the Chaos Game approach in case someone is interested. Thank you for your great answers!
Here is the code:
random[A_, B_, C_] := Module[{a, result},
a = RandomInteger[2];
Which[a == 0, result = A,
a == 1, result = B,
a == 2, result = C]]
Chaos[A_List, B_List, C_List, S_List, n_Integer] :=
Module[{list},
list = NestList[Mean[{random[A, B, C], #}] &,
Mean[{random[A, B, C], S}], n];
ListPlot[list, Axes -> False, PlotStyle -> PointSize[0.001]]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这使用
Scale
和Translate
结合Nest
来创建三角形列表。This uses
Scale
andTranslate
in combination withNest
to create the list of triangles.如果您想要谢尔宾斯基三角形的高质量近似,您可以使用一种称为混沌游戏。想法如下 - 选择您想要定义为谢尔宾斯基三角形顶点的三个点,并随机选择其中一个点。然后,根据需要重复以下过程:
正如您在此动画中所看到的,此过程最终将追踪出一个高-三角形的分辨率版本。如果您愿意,您可以对其进行多线程处理,以便让多个进程同时绘制像素,这最终会更快地绘制三角形。
或者,如果您只想将递归代码转换为迭代代码,一种选择是使用工作列表方法。维护一个包含记录集合的堆栈(或队列),每个记录都保存三角形的顶点和数字 n。最初将主三角形的顶点和分形深度放入该工作列表中。然后:
这本质上是迭代地模拟递归。
希望这有帮助!
If you would like a high-quality approximation of the Sierpinski triangle, you can use an approach called the chaos game. The idea is as follows - pick three points that you wish to define as the vertices of the Sierpinski triangle and choose one of those points randomly. Then, repeat the following procedure as long as you'd like:
As you can see at this animation, this procedure will eventually trace out a high-resolution version of the triangle. If you'd like, you can multithread it to have multiple processes plotting pixels at once, which will end up drawing the triangle more quickly.
Alternatively, if you just want to translate your recursive code into iterative code, one option would be to use a worklist approach. Maintain a stack (or queue) that contains a collection of records, each of which holds the vertices of the triangle and the number n. Initially put into this worklist the vertices of the main triangle and the fractal depth. Then:
This essentially simulates the recursion iteratively.
Hope this helps!
您可以尝试
然后用更有效的东西替换AppendTo。请参阅示例 https://mathematica.stackexchange.com/questions/845/internalbag-inside-compile
编辑
更快:
You may try
And then replace the AppendTo by something more efficient. See for example https://mathematica.stackexchange.com/questions/845/internalbag-inside-compile
Edit
Faster:
由于基于三角形的函数已经被很好地介绍了,这里是基于栅格的方法。
这会迭代地构造帕斯卡三角形,然后取模 2 并绘制结果。
Since the triangle-based functions have already been well covered, here is a raster based approach.
This iteratively constructs pascal's triangle, then takes modulo 2 and plots the result.
这是一个 3D 版本,https://mathematica.stackexchange.com/questions/ 22256/how-can-i-compile-this-function
Here is a 3D version,https://mathematica.stackexchange.com/questions/22256/how-can-i-compile-this-function