Python Forum
Turtle Graphics Help - Filling each shape in - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Turtle Graphics Help - Filling each shape in (/thread-14362.html)

Pages: 1 2


Turtle Graphics Help - Nate - Nov-14-2018

Hi, I'm trying to
fill each polygons with a different color and add side arrows to them, how would i do that?
thanks!
Here's an example of what i'm trying to accomplish
[Image: preview?verifier=Br9NnqBmZStC9jcKm28D4oU...0s8EQyxpS5]

My current code: (Feel free to help me clean it up, any help will be appreciated <3)
import turtle
window = turtle.Screen()
window.bgcolor("white")
turtle  = turtle.Turtle()
turtle.speed(300)
turtle.penup()
def drawingpoly() : #Polygon Drawings - 4 Angles

    four = 4 # number of sides
    five = 5 # number of sides
    six = 6 # number of sides
    seven = 7 # number of sides
    eight = 8 # number of sides
    x = -100 # x position
    y = 200 # y position
    turtle.goto(x, y) #move turtle to the coords above (x and y)


    side_length = 150
    angle = 360.0 / four #360 divided by sides
    angle2 = 360.0 / five  # 360 divided by sides
    angle3 = 360.0 / six  # 360 divided by sides
    angle4 = 360.0 / seven  # 360 divided by sides
    angle5 = 360.0 / eight  # 360 divided by sides

    for i in range(four): #amount of repeats
        turtle.pendown()
        turtle.begin_fill()
        turtle.forward(side_length)
        turtle.right(angle)
        turtle.end_fill()

    for i in range(five): #amount of repeats
        turtle.fillcolor('yellow')
        turtle.begin_fill()
        turtle.forward(side_length)
        turtle.right(angle2)
        turtle.end_fill()
    for i in range(six): #amount of repeats
        turtle.begin_fill()
        turtle.forward(side_length)
        turtle.right(angle3)
        turtle.end_fill()
    for i in range(seven): #amount of repeats
        turtle.begin_fill()
        turtle.forward(side_length)
        turtle.right(angle4)
        turtle.end_fill()
    for i in range(eight): #amount of repeats
        turtle.begin_fill()
        turtle.forward(side_length)
        turtle.right(angle5)
        turtle.end_fill()
        turtle.penup()

ans=True # Menu
loop=False #Loop option (true or false)
while ans:
    print ("""
    ***************************
    1. Draw polygons
    2. Draw a flower
    3. Exit
    ***************************
    """)
    ans=input ("Enter an option (1/2/3):")
    if ans=="1":
      print("Drawing polygons", drawingpoly())
    elif ans=="2":
      print("Drawing a flower")
    elif ans=="3":
      print("Exiting, see you again soon!")
      exit()
      window.exit()

    elif ans !="":
      print("Invalid Option. Try again")






window.exitonclick()



RE: Turtle Graphics Help - ichabod801 - Nov-14-2018

First of all, your figures aren't filling because you're starting and ending the fill every time you draw a side. That's because begin_fill and end_fill are inside your loops. You being the fill, draw a side, turn for the next side, then end the fill. And the penup being in the octogon loop is why it isn't getting drawn. Unindent that one level so the pen comes up after all of the sides are drawn, rather than coming up after the first (and every other) side is drawn.

Second, naming your turtle turtle blocks access to the turtle module. One line 1, the name turtle points to the turtle module. On line 4 you point it at an instance of the Turtle class. Now you have nothing pointing at the turtle module. It doesn't make a difference here, but it's bad programming style that could cause a problem in other programs you write. I would name your turtle instance something different and unique. Like george.

I would also just have one loop. Calculate the angle, and then loop through the number of sides. So it would look something like this:

angle = 360.0 / num_sides
george.pendown()
george.begin_fill()
for side in range(num_sides):
    george.foward(side_length)
    george.right(angle)
george.end_fill()
george.penup()
Then you can either add a parameter named num_sides to the drawingpoly function, or you can put the above loop into another for loop that goes through 4 to eight sides.

Note that to make the triangle shapes in the sample diagram, you would use george.stamp(). That would mean moving half of the length, stamping, and move the other half of the length.


RE: Turtle Graphics Help - Nate - Nov-15-2018

(Nov-14-2018, 11:14 PM)ichabod801 Wrote: First of all, your figures aren't filling because you're starting and ending the fill every time you draw a side. That's because begin_fill and end_fill are inside your loops. You being the fill, draw a side, turn for the next side, then end the fill. And the penup being in the octogon loop is why it isn't getting drawn. Unindent that one level so the pen comes up after all of the sides are drawn, rather than coming up after the first (and every other) side is drawn.

Second, naming your turtle turtle blocks access to the turtle module. One line 1, the name turtle points to the turtle module. On line 4 you point it at an instance of the Turtle class. Now you have nothing pointing at the turtle module. It doesn't make a difference here, but it's bad programming style that could cause a problem in other programs you write. I would name your turtle instance something different and unique. Like george.

I would also just have one loop. Calculate the angle, and then loop through the number of sides. So it would look something like this:

angle = 360.0 / num_sides
george.pendown()
george.begin_fill()
for side in range(num_sides):
    george.foward(side_length)
    george.right(angle)
george.end_fill()
george.penup()
Then you can either add a parameter named num_sides to the drawingpoly function, or you can put the above loop into another for loop that goes through 4 to eight sides.

Note that to make the triangle shapes in the sample diagram, you would use george.stamp(). That would mean moving half of the length, stamping, and move the other half of the length.
Do you know how i can make the arrows on each sides like in the example, thanks


RE: Turtle Graphics Help - ichabod801 - Nov-15-2018

That's what I was talking about in the sentence above your question.


RE: Turtle Graphics Help - Nate - Nov-16-2018

(Nov-14-2018, 11:14 PM)ichabod801 Wrote: First of all, your figures aren't filling because you're starting and ending the fill every time you draw a side. That's because begin_fill and end_fill are inside your loops. You being the fill, draw a side, turn for the next side, then end the fill. And the penup being in the octogon loop is why it isn't getting drawn. Unindent that one level so the pen comes up after all of the sides are drawn, rather than coming up after the first (and every other) side is drawn.

Second, naming your turtle turtle blocks access to the turtle module. One line 1, the name turtle points to the turtle module. On line 4 you point it at an instance of the Turtle class. Now you have nothing pointing at the turtle module. It doesn't make a difference here, but it's bad programming style that could cause a problem in other programs you write. I would name your turtle instance something different and unique. Like george.

I would also just have one loop. Calculate the angle, and then loop through the number of sides. So it would look something like this:

angle = 360.0 / num_sides
george.pendown()
george.begin_fill()
for side in range(num_sides):
    george.foward(side_length)
    george.right(angle)
george.end_fill()
george.penup()
Then you can either add a parameter named num_sides to the drawingpoly function, or you can put the above loop into another for loop that goes through 4 to eight sides.

Note that to make the triangle shapes in the sample diagram, you would use george.stamp(). That would mean moving half of the length, stamping, and move the other half of the length.
How do i make it stop at the half length point to draw the arrows? Thanks


RE: Turtle Graphics Help - ichabod801 - Nov-16-2018

No, seriously. Read the last line before your question.


RE: Turtle Graphics Help - Nate - Nov-16-2018

(Nov-16-2018, 04:26 AM)ichabod801 Wrote: No, seriously. Read the last line before your question.

Sorry, i'm extremely new to this. I really don't understand, is there a way you can assist me? I'm willing to pay per hour.


RE: Turtle Graphics Help - ichabod801 - Nov-16-2018

george.forward(side_length / 2)   # move half the distance
george.stamp()                    # stamp the turtle symbol
george.forward(side_length / 2)   # move the other half of the distance



Turtle Graphics Help - Filling each shape in - Nate - Nov-26-2018

Hi, how can i fill each polygon with a different color? the current code i have only colors the drawings sides, but it doesn't fill the inside.
Thanks!
import turtle
window = turtle.Screen()
window.bgcolor("white")
vlad  = turtle.Turtle()
vlad.speed(300)
vlad.penup()
def drawingpoly() : #Polygon Drawings

    four = 4 # number of sides
    five = 5 # number of sides
    six = 6 # number of sides
    seven = 7 # number of sides
    eight = 8 # number of sides
    nine = 9 # number of sides
    x = -100 # x position
    y = 200 # y position
    vlad.goto(x, y) #move turtle to the coords above (x and y)




    side_length = 150
    angle = 360.0 / four #360 divided by sides
    angle2 = 360.0 / five  # 360 divided by sides
    angle3 = 360.0 / six  # 360 divided by sides
    angle4 = 360.0 / seven  # 360 divided by sides
    angle5 = 360.0 / eight  # 360 divided by sides
    angle6 = 360.0 / nine #360 divided by sides


    for i in range(four): #amount of repeats
        vlad.pencolor("red")
        vlad.begin_fill()
        vlad.pendown()
        vlad.forward(side_length / 2)
        vlad.stamp()  # stamp the turtle symbol
        vlad.forward(side_length / 2)  # move half the distance
        vlad.right(angle)
        vlad.end_fill()
    for i in range(five): #amount of repeats
        vlad.fillcolor("yellow")
        vlad.begin_fill()
        vlad.forward(side_length / 2)
        vlad.stamp()  # stamp the turtle symbol
        vlad.forward(side_length / 2)  # move half the distance
        vlad.right(angle2)
        vlad.end_fill()
    for i in range(six): #amount of repeats
        vlad.begin_fill()
        vlad.forward(side_length / 2)
        vlad.stamp()  # stamp the turtle symbol
        vlad.forward(side_length / 2)  # move half the distance
        vlad.right(angle3)
        vlad.end_fill()
    for i in range(seven): #amount of repeats
        vlad.begin_fill()
        vlad.forward(side_length / 2)
        vlad.stamp()  # stamp the turtle symbol
        vlad.forward(side_length / 2)  # move half the distance
        vlad.right(angle4)
        vlad.end_fill()
    for i in range(eight): #amount of repeats
        vlad.begin_fill()
        vlad.forward(side_length / 2)
        vlad.stamp()  # stamp the turtle symbol
        vlad.forward(side_length / 2)  # move half the distance
        vlad.right(angle5)
        vlad.end_fill()
    for i in range(nine): #amount of repeats
        vlad.begin_fill()
        vlad.forward(side_length)
        vlad.right(angle6)
        vlad.end_fill()
        vlad.penup()



RE: Turtle Graphics Help - Filling each shape in - ichabod801 - Nov-26-2018

As I already said, and illustrated, you need to move the begin_fill and end_fill commands outside the loops. You are currently starting and ending the fill for each side. So each side is filled, but not the whole figure. You need to start the fill before you draw any sides (before the loop) and end the fill after you have drawn all of the side (after the loop).