简体   繁体   English

我将如何制作它,以使此koch雪花中的每个新一代三角形都是不同的颜色? (Python +龟)

[英]How would I go about making it so that each new generation of triangles in this koch snowflake is a different color? (Python + Turtle)

So I have this program that generates a hexagonal koch snowflake using these two main functions: 因此,我有一个使用以下两个主要功能生成六角形科赫雪花的程序:

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() 

How do I make it so that each new set of triangles added through the iterative process is a new color? 我如何使通过迭代过程添加的每组新三角形都是一种新颜色?

I'd rather not use meticulous processes of changing the fill color, then running "voldemort.beginfill()" and "voldemort.endfill()". 我宁愿不使用更改填充颜色的细致过程,然后运行“ voldemort.beginfill()”和“ voldemort.endfill()”。 Help is highly appreciated. 非常感谢您的帮助。 NOTE This is written in python using the Turtle Module. 注意这是使用Turtle模块以python编写的。

One way to do it is to clone the turtle inside mvmt() , so you have an independent one that you can fill without interfering with the filling of the outer one. 一种方法是在mvmt()克隆乌龟,这样您就可以独立填充一个乌龟,而不会干扰外部的填充。

To make that work recursively, you have to pass the current turtle as a parameter to mvmt() . 为了使该递归工作,您必须将当前的turtle作为参数传递给mvmt()

That's what I changed your code to: 那就是我将您的代码更改为:

#!/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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM