python - 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如果您想自己测试一下。
解决方案
您没有显示您希望如何为三角形着色,因此很难确定正确的答案。基本上,如果你没有填充一个封闭的多边形,而你第二次没有填充,那么海龟会在填充之前为你连接第一个和最后一个点。因此,如果我们希望底部填充不同的颜色,我们可以这样做:
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()
我也没有在这里关闭多边形,我只是让乌龟连接端点。
推荐阅读
- python - 重新排列结果编号升序
- c# - 如何在 ReactiveUI 9.3.5 中全局处理异常?
- django - 需要在 Django 管理界面上显示用户计数其他数据计数
- xml - XML 语法检查 DTD 文件时发生错误
- xamarin - 从 Xamarin 跨平台连接到 Oracle 数据库
- c# - 在 Xamarin 表单和 ASP.NET MVC 5 (.NET 4.6.1) 项目之间共享代码
- java - 带有完整构造函数调用的方法引用作为 Java 中的 lambda 表达式
- c++ - 使用 set 方法时,protobuf 成员的地址会改变吗?
- macos - mac mini 进入睡眠状态后如何让 ngrok 保持活力
- c - 如何在 Windows 10 上安装 dr memory