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

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

最佳答案

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

一个可能的解决方案是在画下一片叶子之前,在提笔之后转到花的中心。

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

关于python - 如何使用 begin_fill() 分别填充每个花瓣?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798993/

相关文章:

python - Perl 中 $1 的 Python 等价物是什么,或者正则表达式中的任何其他特殊变量?

python - 按可以为 None 的属性排序

python - 函数缺少 2 个必需的位置参数 : 'x' and 'y'

python - 如何访问存储在 Python 描述符上的属性?

python - 根据给定的概率有效地变异二进制字符串中的字符

python - 在旧版 (EOL) SNI 上安装 certbot-auto 时需要加密 403 错误

python - 如何让 turtle 记住它在 L 系统中的位置?

python - 使用字典理解将列表转换为具有重复键的字典

linux - 在 linux 命令和 Python 之间传递密码

python - 玩家与敌人碰撞的问题