Python龟背透明填充?
Python Turtle transparent fill?
我正在尝试使用 Turtle 库重新创建此图片:
但我在尝试制作内圈时遇到了困难 'transparent'。
我搜索了文档,但找不到更改圆圈填充颜色的不透明度的方法。
我试过的是:
colors = ["black", "magenta", "pink", "blue", "green", "yellow", "orange", "red", "white"]
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
my_turtle.end_fill()
my_turtle.fillcolor("white")
my_turtle.begin_fill()
my_turtle.circle(130)
my_turtle.end_fill()
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
my_turtle.end_fill()
my_turtle.fillcolor("white")
my_turtle.begin_fill()
my_turtle.circle(80)
my_turtle.end_fill()
my_turtle.rt(45)
但我得到的是:
有什么想法吗?
您可以反转圆圈颜色的每个较小的圆圈:
colors = ["black", "magenta", "pink", "blue", "green", "yellow", "orange", "red", "white"]
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
my_turtle.circle(130, -360)
my_turtle.end_fill()
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
my_turtle.circle(80, -360)
my_turtle.end_fill()
my_turtle.rt(45)
输出:
您可能不需要像@AnnZen 建议的那样添加负号,而是注释字符。您的代码(删除了以下行)对我来说工作正常:
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
# my_turtle.end_fill()
# my_turtle.fillcolor("white")
# my_turtle.begin_fill()
my_turtle.circle(130)
my_turtle.end_fill()
# my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
# my_turtle.end_fill()
# my_turtle.fillcolor("white")
# my_turtle.begin_fill()
my_turtle.circle(80)
my_turtle.end_fill()
my_turtle.rt(45)
完整的解决方案:
from turtle import Screen, Turtle
COLORS = ['black', 'magenta', 'pink', 'blue', 'green', 'yellow', 'orange', 'red']
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.width(6)
for color in COLORS:
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(145)
turtle.circle(130)
turtle.end_fill()
turtle.begin_fill()
turtle.circle(115)
turtle.circle(100)
turtle.end_fill()
turtle.right(45)
turtle.hideturtle()
screen.tracer(True)
screen.exitonclick()
The negative signs in Ann Zen's solution make each ring a polygon that
touches itself at one point, but never overlaps itself. Your solution
produces self-overlapping polygons, which have different results based
on the polygon fill rule being used ("even-odd" vs. "nonzero winding
number").
是的,我们以前见过这种情况,通常是填充的五角星。让我们进一步推动@AnnZen 的负范围解决方案,在填充之前绘制整个双弧:
from turtle import Screen, Turtle
COLORS = ['black', 'magenta', 'pink', 'blue', 'green', 'yellow', 'orange', 'red']
screen = Screen()
turtle = Turtle()
turtle.width(6)
for color in COLORS:
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(145)
turtle.circle(130, -360)
turtle.circle(115)
turtle.circle(100, -360)
turtle.end_fill()
turtle.right(45)
turtle.hideturtle()
screen.exitonclick()
这在绕组数方面表现良好吗?
我正在尝试使用 Turtle 库重新创建此图片:
但我在尝试制作内圈时遇到了困难 'transparent'。 我搜索了文档,但找不到更改圆圈填充颜色的不透明度的方法。
我试过的是:
colors = ["black", "magenta", "pink", "blue", "green", "yellow", "orange", "red", "white"]
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
my_turtle.end_fill()
my_turtle.fillcolor("white")
my_turtle.begin_fill()
my_turtle.circle(130)
my_turtle.end_fill()
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
my_turtle.end_fill()
my_turtle.fillcolor("white")
my_turtle.begin_fill()
my_turtle.circle(80)
my_turtle.end_fill()
my_turtle.rt(45)
但我得到的是:
有什么想法吗?
您可以反转圆圈颜色的每个较小的圆圈:
colors = ["black", "magenta", "pink", "blue", "green", "yellow", "orange", "red", "white"]
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
my_turtle.circle(130, -360)
my_turtle.end_fill()
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
my_turtle.circle(80, -360)
my_turtle.end_fill()
my_turtle.rt(45)
输出:
您可能不需要像@AnnZen 建议的那样添加负号,而是注释字符。您的代码(删除了以下行)对我来说工作正常:
for i in range(8):
my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(150)
# my_turtle.end_fill()
# my_turtle.fillcolor("white")
# my_turtle.begin_fill()
my_turtle.circle(130)
my_turtle.end_fill()
# my_turtle.fillcolor(colors[i])
my_turtle.begin_fill()
my_turtle.circle(100)
# my_turtle.end_fill()
# my_turtle.fillcolor("white")
# my_turtle.begin_fill()
my_turtle.circle(80)
my_turtle.end_fill()
my_turtle.rt(45)
完整的解决方案:
from turtle import Screen, Turtle
COLORS = ['black', 'magenta', 'pink', 'blue', 'green', 'yellow', 'orange', 'red']
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.width(6)
for color in COLORS:
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(145)
turtle.circle(130)
turtle.end_fill()
turtle.begin_fill()
turtle.circle(115)
turtle.circle(100)
turtle.end_fill()
turtle.right(45)
turtle.hideturtle()
screen.tracer(True)
screen.exitonclick()
The negative signs in Ann Zen's solution make each ring a polygon that touches itself at one point, but never overlaps itself. Your solution produces self-overlapping polygons, which have different results based on the polygon fill rule being used ("even-odd" vs. "nonzero winding number").
是的,我们以前见过这种情况,通常是填充的五角星。让我们进一步推动@AnnZen 的负范围解决方案,在填充之前绘制整个双弧:
from turtle import Screen, Turtle
COLORS = ['black', 'magenta', 'pink', 'blue', 'green', 'yellow', 'orange', 'red']
screen = Screen()
turtle = Turtle()
turtle.width(6)
for color in COLORS:
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(145)
turtle.circle(130, -360)
turtle.circle(115)
turtle.circle(100, -360)
turtle.end_fill()
turtle.right(45)
turtle.hideturtle()
screen.exitonclick()
这在绕组数方面表现良好吗?