How to fill color in kite using turtle

Question:

I tried to create a kite using turtle in Python. I drew it correctly but it doesn’t fill in the color in all four parts.

This is my code:

import turtle
turtle.fillcolor('orange')
turtle.begin_fill()
turtle.goto(0,100)
turtle.goto(-100,0)
turtle.goto(0,0)
turtle.end_fill()
turtle.fillcolor('pink')
turtle.begin_fill()
turtle.goto(100,0)
turtle.goto(0,100)
turtle.goto(0,-100)
turtle.end_fill()
turtle.fillcolor('black')
turtle.begin_fill()
turtle.goto(-100,0)
turtle.goto(0,-100)
turtle.goto(100,0)

turtle.fillcolor('green')
turtle.begin_fill()
turtle.goto(0,-100)
turtle.goto(0,-150)
turtle.goto(0,-100)
turtle.end_fill()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.goto(50,-150)
turtle.goto(-50,-150)
turtle.goto(0,-100)
turtle.end_fill()
turtle.done()

How can I fix it to get the correct fills?

Asked By: Darshan Joshi

||

Answers:

Other than a missing end_fill associated with turtle.fillcolor('black'), your drawing is sensible in that you’ve saved work by moving on to the next starting point, but this causes the fills to be incomplete. Instead, be precise about your starting and ending points for each filled shape. Insist on starting and ending each shape at the same place so the fill is complete, in this case, (0, 0).

For example:

import turtle

t = turtle.Turtle()
t.fillcolor("orange")
t.begin_fill()
t.goto(0, 100)
t.goto(-100, 0)
t.goto(0, 0)
t.end_fill()

t.fillcolor("pink")
t.begin_fill()
t.goto(100, 0)
t.goto(0, 100)
t.goto(0, 0)
t.end_fill()

t.fillcolor("black")
t.begin_fill()
t.goto(-100, 0)
t.goto(0, -100)
t.goto(0, 0)
t.end_fill()

t.fillcolor("green")
t.begin_fill()
t.goto(100, 0)
t.goto(0, -100)
t.end_fill()

t.fillcolor("yellow")
t.begin_fill()
t.goto(50, -150)
t.goto(-50, -150)
t.goto(0, -100)
t.end_fill()

turtle.exitonclick()

Now, this should strike you as repetitive. 100 appears many times in the code and each triangle drawing follows similar logic. You can use a loop and introduce a variable to save work and generalize the pattern:

import turtle

t = turtle.Turtle()
colors = "orange", "pink", "black", "green"
d = 100

for color in colors:
    t.fillcolor(color)
    t.begin_fill()
    t.forward(d)
    t.left(135)

    # pythagorean theorem
    t.forward((d ** 2 + d ** 2) ** 0.5)

    t.left(135)
    t.forward(d)
    t.end_fill()
    t.left(180)

t.fillcolor("yellow")
t.goto(0, -d)
t.begin_fill()
t.goto(d / 2, -d * 1.5)
t.goto(-d / 2, -d * 1.5)
t.goto(0, -d)
t.end_fill()

turtle.exitonclick()

When you generalize patterns, you can avoid copy-paste errors like forgetting turtle.fillcolor('black').

Other approaches are possible.

Answered By: ggorlen