首页 > 解决方案 > Python Turtles 颜色填充

问题描述

这可能是我的三角形的问题,也许边缘没有正确连接,但我的三角形没有按照我想要的方式填充:

#Draw
bob.pen(fillcolor="#b11874")
bob.pensize(3)
#Sierpinski 1
bob.begin_fill()
bob.forward(150)
bob.left(120)
bob.forward(150)
bob.left(120)
bob.forward(150)
bob.end_fill()
#colouring2
bob.pen(fillcolor="#ff6600")
bob.begin_fill()
bob.left(180)
bob.forward(75)
bob.right(60)
bob.forward(75)
bob.right(120)
bob.end_fill()

第一次填充效果很好,并将三角形着色为紫色,但是当我开始第二次填充时,它将三角形切成两半。

我正在画谢尔宾斯基,并试图用一种颜色为外部三角形着色,而用另一种颜色为内部三角形着色。这是整个代码:

https://drive.google.com/file/d/1BaPrU0N4AaVL9w4zp9WIe-c4LOFp9EPO/view?usp=sharing如果您想自己测试一下。

标签: pythonturtle-graphics

解决方案


您没有显示您希望如何为三角形着色,因此很难确定正确的答案。基本上,如果你没有填充一个封闭的多边形,而你第二次没有填充,那么海龟会在填充之前为你连接第一个和最后一个点。因此,如果我们希望底部填充不同的颜色,我们可以这样做:

import turtle         
bob = turtle.Turtle()
window = turtle.Screen()

# Draw
bob.pen(fillcolor="purple")
bob.pensize(3)

# Sierpinski 1
bob.begin_fill()
bob.forward(150)
bob.left(120)
bob.forward(150)
bob.left(120)
bob.forward(150)
bob.end_fill()

# colouring 2
bob.pen(fillcolor="orange")
bob.begin_fill()
bob.left(180)
bob.forward(75)
bob.right(60)
bob.forward(75)
bob.right(60)
bob.forward(75)
bob.end_fill()

window.exitonclick()

我也没有在这里关闭多边形,我只是让乌龟连接端点。

在此处输入图像描述


推荐阅读