Python Turtle transparent fill?

Viewed 1765

I'm trying to recreate this picture using the Turtle library:

enter image description here

but I'm struggling when trying to make the inner circles 'transparent'. I searched the documentation but found no way to change the opacity of the circle's fill color.

What I tried was:

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)

But what I got was:

enter image description here

Any ideas?

2 Answers

You can reverse every smaller circle of the circle colors:

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)

Output:

enter image description here

You may not need to add negative signs as @AnnZen suggests, but rather comment characters. Your code, with the following lines removed, works fine for me:

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)

A complete solution:

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

enter image description here

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").

Yes, we've seen this before, usually with filled five-pointed stars. Let's push @AnnZen's negative extent solution even further, drawing an entire double arc before filling it:

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

Is this well behaved winding-number-wise?

Related