1

所以我有这个程序使用这两个主要功能生成六边形科赫雪花:

def mvmt(length):

if length <= 10:
    voldemort.forward(length)
else:
    mvmt(length/3)
    voldemort.right(60)
    mvmt(length/3)
    voldemort.left(120)
    mvmt(length/3)
    voldemort.right(60)
    mvmt(length/3)


def whole(length = 300):
    voldemort.hideturtle()
    voldemort.penup()
    voldemort.goto(-300,-255)
    voldemort.pendown()
    voldemort.begin_fill()
    mvmt(length)
    voldemort.left(60)
    mvmt(length)
    voldemort.left(60)
    mvmt(length)
    voldemort.left(60)
    mvmt(length)
    voldemort.left(60)
    mvmt(length)
    voldemort.left(60)
    mvmt(length)
    voldemort.end_fill() 

如何使通过迭代过程添加的每组新三角形都是一种新颜色?

我宁愿不使用更改填充颜色的细致过程,然后运行“voldemort.beginfill()”和“voldemort.endfill()”。高度赞赏帮助。注意这是使用 Turtle 模块用 python 编写的。

4

1 回答 1

0

一种方法是在内部克隆海龟mvmt(),这样您就有了一个独立的海龟,您可以在不干扰外部海龟的填充的情况下对其进行填充。

要使其递归工作,您必须将当前海龟作为参数传递给mvmt().

这就是我将您的代码更改为:

#!/usr/bin/env python

from __future__ import division

import colorsys
import turtle

def mvmt(original, length):
    if length >= 10:
        fraction = length / 3
        h, s, v = colorsys.rgb_to_hsv(*original.fillcolor())

        clone = original.clone()
        clone.fillcolor(colorsys.hsv_to_rgb(h + 1/6, s, v))
        clone.begin_fill()
        mvmt(clone, fraction)
        clone.right(60)
        mvmt(clone, fraction)
        clone.left(120)
        mvmt(clone, fraction)
        clone.right(60)
        mvmt(clone, fraction)
        clone.end_fill()

    original.forward(length)

def whole(length = 300):
    voldemort = turtle.Turtle()
    voldemort.speed(0)
    voldemort.hideturtle()
    voldemort.penup()
    voldemort.goto(-200,-255)
    voldemort.pendown()

    voldemort.fillcolor((1.0, 0.5, 0.5))
    voldemort.begin_fill()
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.left(60)
    mvmt(voldemort, length)
    voldemort.end_fill()

turtle.delay(0)
turtle.speed(0)

whole()
turtle.exitonclick()
于 2013-06-06T06:04:59.373 回答