如何使用begin_fill()分别填充每个花瓣?

时间:2015-03-01 20:58:31

标签: python python-2.7 turtle-graphics

我有以下代码为我正在尝试构建的花朵生成花瓣图案。但问题是填充部分。

每个花瓣应该单独填充:

enter image description here

相反,会发生什么:

enter image description here

import turtle
import math

wn = turtle.Screen()
wn.bgcolor("white")

def draw_leaf(turtle, side, theta = 0):
    angle = 2
    turtle.color("#67bd3c")
    for x in range(-180,180):
        y = math.sin(math.radians(angle))
        angle += 1
        y = y * side
        x_axis = (x % 180) * math.cos(math.radians(theta)) + y * math.sin(math.radians(theta))
        y_axis = (x % 180) * (-1 * (math.sin(math.radians(theta)))) + y * math.cos(math.radians(theta))
        turtle.goto(-1 * x_axis, -1 * y_axis)
    return

def draw_flower(turtle, petals):
    for x in range(petals):
        theta = 180/(petals - 1)
        turtle.pendown()
        turtle.begin_fill()
        draw_leaf(turtle, 35, theta * x)
        turtle.end_fill()
        turtle.penup()
        turtle.left(theta)
    return

draw_flower(turtle,4)

wn.exitonclick()

1 个答案:

答案 0 :(得分:2)

当乌龟位于之前绘制的叶子的远端时,看起来每个draw_leaf调用都会开始。因此,在当前draw_leaf期间填充的多边形包括该终点。如果用不同的颜色绘制每个叶子,这一点就更明显了。

一种可能的解决方案是在goto之后penup花的中心,然后画下一片叶子。

def draw_flower(turtle, petals):
    start = turtle.pos()
    for x in range(petals):
        theta = 180/(petals - 1)
        turtle.pendown()
        turtle.begin_fill()
        draw_leaf(turtle, 35, theta * x)
        turtle.end_fill()
        turtle.penup()
        turtle.goto(*start)
        turtle.left(theta)
    return