Python Forum
Turtle Star Fill Color Yellow-White Interchanging Color Effect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Star Fill Color Yellow-White Interchanging Color Effect
#1
https://docs.python.org/3/_images/turtle-star.png
Does anyone know how this interchanging color is achieved?
Reply
#2
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.
Reply
#3
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()
Reply
#4
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
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.
Reply
#6
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()
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
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.
Reply
#8
yes I agree, the alternative fill looks mostly like an infinite loop
Reply
#9
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()
Reply
#10
btw this insert code snippet thing doesn't work for me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  identify not white pixels in bmp flash77 17 2,509 Nov-10-2023, 09:21 PM
Last Post: flash77
  Color a table cell based on specific text Creepy 11 2,011 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Pixel color and action Sartre 4 2,105 Apr-13-2023, 03:26 AM
Last Post: Sartre
  Printing effect sizes for variables in an anova eyavuz21 2 991 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  color names jimrinaldo 8 136,876 Sep-12-2022, 02:53 PM
Last Post: motorland
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,927 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  simplekml change shape&color issac_n 2 2,849 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  color column in mail html df bnadir55 0 727 Aug-14-2022, 07:11 AM
Last Post: bnadir55
  Help with TypeWriter Effect in Python Rich Extra 0 1,180 May-23-2022, 09:44 PM
Last Post: Extra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020