<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=%E4%BD%BF%E7%94%A8python%E8%AF%AD%E8%A8%80%E8%A1%A8%E8%BE%BE%E5%88%86%E5%BD%A2%E4%B8%8E%E9%80%92%E5%BD%92</id>
	<title>使用python语言表达分形与递归 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=%E4%BD%BF%E7%94%A8python%E8%AF%AD%E8%A8%80%E8%A1%A8%E8%BE%BE%E5%88%86%E5%BD%A2%E4%B8%8E%E9%80%92%E5%BD%92"/>
	<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8python%E8%AF%AD%E8%A8%80%E8%A1%A8%E8%BE%BE%E5%88%86%E5%BD%A2%E4%B8%8E%E9%80%92%E5%BD%92&amp;action=history"/>
	<updated>2026-11-03T05:48:06Z</updated>
	<subtitle>本wiki的该页面的版本历史</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8python%E8%AF%AD%E8%A8%80%E8%A1%A8%E8%BE%BE%E5%88%86%E5%BD%A2%E4%B8%8E%E9%80%92%E5%BD%92&amp;diff=15431&amp;oldid=prev</id>
		<title>思无涯咿呀咿呀：创建页面，内容为“   ==Fibonacci数列==  250px 250px 250px  Fibonacci数列是…”</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8python%E8%AF%AD%E8%A8%80%E8%A1%A8%E8%BE%BE%E5%88%86%E5%BD%A2%E4%B8%8E%E9%80%92%E5%BD%92&amp;diff=15431&amp;oldid=prev"/>
		<updated>2020-10-16T01:47:25Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“   ==Fibonacci数列==  &lt;a href=&quot;/index.php/%E6%96%87%E4%BB%B6:Fractalandrecursive1.svg.png&quot; title=&quot;文件:Fractalandrecursive1.svg.png&quot;&gt;250px&lt;/a&gt; &lt;a href=&quot;/index.php/%E6%96%87%E4%BB%B6:Fractalandrecursive2.svg.png&quot; title=&quot;文件:Fractalandrecursive2.svg.png&quot;&gt;250px&lt;/a&gt; &lt;a href=&quot;/index.php/%E6%96%87%E4%BB%B6:Carrotspiral.jpg&quot; title=&quot;文件:Carrotspiral.jpg&quot;&gt;250px&lt;/a&gt;  Fibonacci数列是…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Fibonacci数列==&lt;br /&gt;
&lt;br /&gt;
[[File:fractalandrecursive1.svg.png|250px]]&lt;br /&gt;
[[File:fractalandrecursive2.svg.png|250px]]&lt;br /&gt;
[[File:carrotspiral.jpg|250px]]&lt;br /&gt;
&lt;br /&gt;
Fibonacci数列是一个很有趣的结构，每后一项都等于前两项之和。它的前几位如下：0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 134...我们可以表达该函数如下所示：&lt;br /&gt;
&lt;br /&gt;
fib(0) = 0&lt;br /&gt;
fib(1) = 1&lt;br /&gt;
fib(n) = fib(n-1) + fib(n-2)  for n &amp;gt;= 2&lt;br /&gt;
&lt;br /&gt;
在python里如何表达这样一个函数呢？我们可以用递归来表达。所谓递归，就是一个函数不断引用它自身，直到抵达一个可计算出确定值的初始态。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    def fib(n):&lt;br /&gt;
        if n &amp;lt;= 1:&lt;br /&gt;
            return n&lt;br /&gt;
        else:&lt;br /&gt;
            return fib(n-1) + fib(n-2)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==分形结构==&lt;br /&gt;
&lt;br /&gt;
分形概念由法国数学家曼德布罗在1975年提出，用于形容局部与整体相似的形状。如下所示图案叫科赫雪花（Koch fractal），是一个典型的分形。&lt;br /&gt;
&lt;br /&gt;
[[File:koch_0.png|250px]]&lt;br /&gt;
&lt;br /&gt;
[[File:koch_1.png|250px]]&lt;br /&gt;
&lt;br /&gt;
[[File:koch_2.png|250px]]&lt;br /&gt;
&lt;br /&gt;
[[File:koch_3.png|250px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
如何用计算机制造出这个图像呢？仔细观察并思考这个图像的绘制过程，我们会发现用递归的方式也能解决。我们称第一张图（只有一条直线）为0阶（order）雪花，第二张为1阶，以此类推。我们会发现，例如第二阶的图，可以通过引用三次第一阶的图来绘制。因此这个结构可以使用如下函数实现：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    def koch(t, order, size):&lt;br /&gt;
        if order == 0:&lt;br /&gt;
            t.forward(size)&lt;br /&gt;
        else:&lt;br /&gt;
            for angle in [60, -120, 60, 0]:&lt;br /&gt;
               koch(t, order-1, size/3)&lt;br /&gt;
               t.left(angle)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
其中t是一个移动的小海龟（类似netlogo里的turtle），order是层级，size是整个雪花的线性规模。如果我们不用递归，则有n阶就必须定义n个函数：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    def koch_0(t, size):&lt;br /&gt;
        t.forward(size)&lt;br /&gt;
    &lt;br /&gt;
    def koch_1(t, size):&lt;br /&gt;
        for angle in [60, -120, 60, 0]:&lt;br /&gt;
           koch_0(t, size/3)&lt;br /&gt;
           t.left(angle)&lt;br /&gt;
    &lt;br /&gt;
    def koch_2(t, size):&lt;br /&gt;
        for angle in [60, -120, 60, 0]:&lt;br /&gt;
           koch_1(t, size/3)&lt;br /&gt;
           t.left(angle)&lt;br /&gt;
    &lt;br /&gt;
    def koch_3(t, size):&lt;br /&gt;
        for angle in [60, -120, 60, 0]:&lt;br /&gt;
           koch_2(t, size/3)&lt;br /&gt;
           t.left(angle)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
可见递归确实是一种非常简洁的思想。&lt;br /&gt;
&lt;br /&gt;
==动态演化的分形结构==&lt;br /&gt;
&lt;br /&gt;
[[File:recursivetree1.png|250px]]    [[File:recursivetree2.png|250px]]&lt;br /&gt;
&lt;br /&gt;
我们可以使用递归函数制造出分形结构，并且不断修正其中的参数，来构造动态演化的结构.为了帮助进行动态演示，下面这段程序使用了pygame模块，安装地址见[http://www.pygame.org/download.shtml 这里]。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    import pygame, math&lt;br /&gt;
    pygame.init()           # prepare the pygame module for use&lt;br /&gt;
    # Create a new surface and window.&lt;br /&gt;
    surface_size = 1024&lt;br /&gt;
    main_surface = pygame.display.set_mode((surface_size,surface_size))&lt;br /&gt;
    my_clock = pygame.time.Clock()&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    def draw_tree(order, theta, sz, posn, heading, color=(0,0,0), depth=0):&lt;br /&gt;
       trunk_ratio = 0.29       # How big is the trunk relative to whole tree?&lt;br /&gt;
       trunk = sz * trunk_ratio # length of trunk&lt;br /&gt;
       delta_x = trunk * math.cos(heading)&lt;br /&gt;
       delta_y = trunk * math.sin(heading)&lt;br /&gt;
       (u, v) = posn&lt;br /&gt;
       newpos = (u + delta_x, v + delta_y)&lt;br /&gt;
       pygame.draw.line(main_surface, color, posn, newpos)&lt;br /&gt;
       if order &amp;gt; 0:   # Draw another layer of subtrees&lt;br /&gt;
          # These next six lines are a simple hack to make the two major halves&lt;br /&gt;
          # of the recursion different colors. Fiddle here to change colors&lt;br /&gt;
          # at other depths, or when depth is even, or odd, etc.&lt;br /&gt;
          if depth == 0:&lt;br /&gt;
              color1 = (255, 0, 0)&lt;br /&gt;
              color2 = (0, 0, 255)&lt;br /&gt;
          else:&lt;br /&gt;
              color1 = color&lt;br /&gt;
              color2 = color&lt;br /&gt;
          # make the recursive calls to draw the two subtrees&lt;br /&gt;
          newsz = sz*(1 - trunk_ratio)&lt;br /&gt;
          draw_tree(order-1, theta, newsz, newpos, heading-theta, color1, depth+1)&lt;br /&gt;
          draw_tree(order-1, theta, newsz, newpos, heading+theta, color2, depth+1)&lt;br /&gt;
    &lt;br /&gt;
    def gameloop():&lt;br /&gt;
        theta = 0&lt;br /&gt;
        while True:&lt;br /&gt;
            # Handle evente from keyboard, mouse, etc.&lt;br /&gt;
            ev = pygame.event.poll()&lt;br /&gt;
            if ev.type == pygame.QUIT:&lt;br /&gt;
                break;&lt;br /&gt;
            # Updates - change the angle&lt;br /&gt;
            theta += 0.01&lt;br /&gt;
            # Draw everything&lt;br /&gt;
            main_surface.fill((255, 255, 0))&lt;br /&gt;
            draw_tree(9, theta, surface_size*0.9, (surface_size//2, surface_size-50), -math.pi/2)&lt;br /&gt;
            pygame.display.flip()&lt;br /&gt;
            my_clock.tick(120)&lt;br /&gt;
 &lt;br /&gt;
    gameloop()&lt;br /&gt;
    pygame.quit()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[category:python]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[category:旧词条迁移]]&lt;/div&gt;</summary>
		<author><name>思无涯咿呀咿呀</name></author>
	</entry>
</feed>