Python Forum
Turtle Star Fill Color Yellow-White Interchanging Color Effect - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Turtle Star Fill Color Yellow-White Interchanging Color Effect (/thread-40975.html)



Turtle Star Fill Color Yellow-White Interchanging Color Effect - codelab - Oct-23-2023

https://docs.python.org/3/_images/turtle-star.png
Does anyone know how this interchanging color is achieved?


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - deanhystad - Oct-23-2023

I think you achieve that affect by using an older turtle version that has a different fill algorithm. I tried looking for pull request related to the fill algorithm and WOW! There is a lot of Turtle development activity.

I assume you tried using this program:
from turtle import *

color("red", "yellow")
begin_fill()

while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()
Which creates a star of the same shape, but completely filled with yellow.


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - codelab - Oct-24-2023

I used this:
Virtually impossible to achieve that kind of color effect:
tutu.pencolor('red')

tutuWalk = 200
tutu.speed(9)

colors = ["yellow", "white"]

for _ in range(36):
tutu.begin_fill()
color_index = _ % 2
print(colors[color_index])
tutu.fillcolor(colors[color_index])
tutu.forward(tutuWalk)
tutu.left(170)
tutu.end_fill()
tutu_screen.exitonclick()


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - DPaul - Oct-24-2023

Hi,
Dont't know about alternative fill effects.
I would simply draw a second, smaller star in the middle, and fill it with white.
Works great.
Paul


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - deanhystad - Oct-24-2023

Drawing another star is not exactly easy. You need to compute a new move length and turn angle, move to the new starting location and a star filled with a different fill color., repeat 3 times toggling between white and yellow fill.

I think it more likely that the star was created using the code on this page:

https://docs.python.org/3/library/turtle.html
Quote:Let’s draw the star shape at the top of this page. We want red lines, filled in with yellow:

color('red')
fillcolor('yellow')
Just as up() and down() determine whether lines will be drawn, filling can be turned on and off:

begin_fill()
Next we’ll create a loop:

while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
abs(pos()) < 1 is a good way to know when the turtle is back at its home position.

Finally, complete the filling:

end_fill()
The image is most likely from an older version of Turtle that had a bug in the fill algorithm.


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - DPaul - Oct-24-2023

This needs some extra tweaking, but it goes some way towards the desired effect.
Paul

from turtle import *
import turtle
turtle.speed(8)
turtle.setpos(0,0)
color("red", "yellow")
begin_fill()
 
while True:
    forward(400)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()

color("red","white")
turtle.setpos(100,10)
begin_fill()
counter = 0
while True:
    counter += 1
    forward(200)
    left(170)
    if counter == 18:
        break
    
end_fill()
hideturtle()
done()



RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - deanhystad - Oct-24-2023

That is not even close. This is closer, but it exposes a problem.
import turtle

turtle.speed(8)
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(36):
    turtle.forward(400)
    turtle.left(170)
turtle.end_fill()

turtle.color("red", "white")
turtle.forward(100)
turtle.begin_fill()
for _ in range(9):
    turtle.forward(200)
    turtle.left(160)
turtle.end_fill()
turtle.hideturtle()
turtle.done()
The inner star is half the size of the outer star. To have the same size opening in the center of the star, the interior angle at the points of the inner star must be twice that of the outer star, a turn of 160 degrees (for an interior angle of 20 degrees). When I draw a star like this, it repeats after 9 points. I need the inner star to have 36 points, so I would need to move to a new corner and repeat the pattern 3 more times. Like this:
import turtle

turtle.speed(8)
turtle.color("red", "yellow")
turtle.begin_fill()
for _ in range(36):
    turtle.forward(400)
    turtle.left(170)
turtle.end_fill()

turtle.color("red", "white")
turtle.forward(100)
turtle.begin_fill()
for _ in range(9):
    turtle.forward(200)
    turtle.left(160)

turtle.penup()
turtle.left(5)
turtle.forward(202)
turtle.left(165)
turtle.pendown()

for _ in range(9):
    turtle.forward(200)
    turtle.left(160)

turtle.penup()
turtle.left(5)
turtle.forward(202)
turtle.left(165)
turtle.pendown()

for _ in range(9):
    turtle.forward(200)
    turtle.left(160)

turtle.penup()
turtle.left(5)
turtle.forward(202)
turtle.left(165)
turtle.pendown()

for _ in range(9):
    turtle.forward(200)
    turtle.left(160)

turtle.end_fill()
turtle.hideturtle()
turtle.done()
You can see while the white star is drawn that is not quit right. The starting position for the white star is just a tiny bit off and you can see see that it is not tracing perfectly over the existing red lines.

Even with all this complexity we are only half way to making a star like the image in the first post. There has to be a yellow star inside the white star. This star is not half the size of the white star, more like 2/3rds. And inside that star is another white star, and inside that a yellow star and so on until the size of the star and the screen resolution result in starts that are so similar to the previous that the difference cannot be seen.

It seems like it should be easy, but it is not easy.


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - codelab - Oct-24-2023

yes I agree, the alternative fill looks mostly like an infinite loop


RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - codelab - Oct-25-2023

indeed appears to be a version issue, my student uses python 10.x version and he managed to reproduce the exact image.
I am using python 11.x and by copying the same code, the color fill was mono yellow:
from turtle import Turtle, Screen
abc = Turtle()
print(abc)
abc.color("red", "yellow")
abc.begin_fill()
abcSquare = Screen()
abc.shape("turtle")
i = 0
while i < 37:
    abc.speed(0)
    abc.forward(200)
    abc.left(175)
    abc.forward(200)
    abc.right(360)
    i += 1
abc.end_fill()
abcSquare.exitonclick()



RE: Turtle Star Fill Color Yellow-White Interchanging Color Effect - codelab - Oct-25-2023

btw this insert code snippet thing doesn't work for me.