Python Forum
Turtle Graphics Help - Filling each shape in
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Graphics Help - Filling each shape in
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
That's what I was talking about in the sentence above your question.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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
Reply
#6
No, seriously. Read the last line before your question.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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.
Reply
#8
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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()
Reply
#10
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Project 4 Shape Calculator FreeCodeCamp btownboy 5 2,880 Sep-14-2020, 01:38 AM
Last Post: btownboy
  python idle turtle graphics help jojo 5 4,057 Apr-14-2019, 12:28 PM
Last Post: ichabod801
  Turtle Graphics Card Values in a List Muzz 0 2,379 Apr-11-2019, 12:55 PM
Last Post: Muzz
  why is this not filling my empty list? Siylo 4 3,231 Jan-21-2019, 05:27 PM
Last Post: Siylo
  ValueError: shape mismatch: value array of shape... ulrich48155 2 23,825 Jul-10-2017, 02:17 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

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