Milan Mishra
Milan Mishra

Reputation: 3

Why is turtle not filling in a section of a closed shape

I am trying to fill in a shape with turtle, but when I do, a small section ends up not getting filled in. How could I fix this? I tried making it so the line going up goes past the point where the empty area starts but that didn't work. As you can probably see from my code, I am pretty new to python. Here is my code...

import turtle

turtl = turtle.Turtle()
turtle.screensize(852, 508, 'white')
turtl.penup()
turtl.fillcolor("royal blue")      #Every closed shape after this untill endfill() will be filled with royal blue
turtl.begin_fill()

for i in range(1):
    turtl.hideturtle()
    turtl.penup()
    turtl.setx(-213)
    turtl.sety(-75)      #Moves the turtle to the correct place to start the "plus"
    turtl.right(180)
    turtl.pendown()
    turtl.forward(427)      #Makes the first line going left
    turtl.right(90)
    turtl.forward(150)      #Width of plus
    turtl.right(90)
    turtl.forward(427)
    turtl.left(90)
    turtl.forward(253)      #Going to top of plus
    turtl.right(90)
    turtl.forward(150)      #Width of top of plus
    turtl.right(90)
    turtl.forward(253)      #Going back down from right side of top of plus
    turtl.left(90)
    turtl.forward(697)      #Going to the right side of the screen
    turtl.right(90)
    turtl.forward(150)      #Width of plus / Start of bottom half of plus
    turtl.right(90)
    turtl.forward(697)      #Going back left to to the bottom bit of the plus
    turtl.left(90)
    turtl.forward(253)     #Going to the bottom part of plus
    turtl.right(90)
    turtl.forward(150)
    turtl.right(90)
    turtl.forward(253)      #Traveling back up to center bar

turtl.end_fill()

print(turtl)
turtle.mainloop()

Upvotes: 0

Views: 1526

Answers (3)

furas
furas

Reputation: 142939

You should move turtle to start position before begin_fill because it seems it sees this move setx(), sety() as line - even if you use penup() - and this line creates white triange inside figure.

import turtle

turtl = turtle.Turtle()

turtle.screensize(852, 508, 'white')

turtl.penup()
turtl.setx(-213)
turtl.sety(-75)      #Moves the turtle to the correct place to start the "plus"
turtl.right(180)
turtl.pendown()

turtl.fillcolor("royal blue")      #Every closed shape after this untill endfill() will be filled with royal blue
turtl.begin_fill()

for i in range(1):
    turtl.hideturtle()
    turtl.forward(427)      #Makes the first line going left
    turtl.right(90)
    turtl.forward(150)      #Width of plus
    turtl.right(90)
    turtl.forward(427)
    turtl.left(90)
    turtl.forward(253)      #Going to top of plus
    turtl.right(90)
    turtl.forward(150)      #Width of top of plus
    turtl.right(90)
    turtl.forward(253)      #Going back down from right side of top of plus
    turtl.left(90)
    turtl.forward(697)      #Going to the right side of the screen
    turtl.right(90)
    turtl.forward(150)      #Width of plus / Start of bottom half of plus
    turtl.right(90)
    turtl.forward(697)      #Going back left to to the bottom bit of the plus
    turtl.left(90)
    turtl.forward(253)     #Going to the bottom part of plus
    turtl.right(90)
    turtl.forward(150)
    turtl.right(90)
    turtl.forward(253)      #Traveling back up to center bar

turtl.end_fill()

turtle.mainloop()

Upvotes: 1

BanHelsing
BanHelsing

Reputation: 15

Firstly, I think the for loop you have is unnecessary, since it has a range(1), meaning that it will run just once, which is the same has having no for loop. Regarding your question, just replace the setx(x) and sety(y) with a single goto(x,y) like so:

Original:

for i in range(1):
    turtl.hideturtle()
    turtl.penup()
    turtl.setx(-213)
    turtl.sety(-75)
    turtl.right(180)

Revised:

for i in range(1):
    turtl.hideturtle()
    turtl.penup()
    turtl.goto(-213,-75)
    turtl.right(180)

Upvotes: 1

cdlane
cdlane

Reputation: 41905

You've been provided two workable solutions (+1 each), so I want to address another issue in your code: don't use screensize(), it doesn't do what you want. Use setup() instead. Example usage below along with a completely different way to draw your figure using stamping instead of drawing:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 852, 508

THICKNESS = 150

CURSOR_SIZE = 20

screen = Screen()
screen.setup(WIDTH, HEIGHT)

turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.color("royal blue")
turtle.penup()

turtle.setx(-WIDTH/6)

turtle.shapesize(HEIGHT/CURSOR_SIZE, THICKNESS/CURSOR_SIZE)

turtle.stamp()

turtle.shapesize(THICKNESS/CURSOR_SIZE, (7*WIDTH/6 + THICKNESS)/CURSOR_SIZE)

turtle.stamp()

screen.mainloop()

enter image description here

Upvotes: 0

Related Questions