Ask Your Question
2

How can turtle be used to fill polygons with hatch lines?

asked 2021-11-06 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-06-04 02:00:00 +0000

qstack gravatar image

One way to fill polygons with hatch lines using the turtle module in Python is to use a loop to iterate over the vertices of the polygon and draw diagonal lines inside it.

Here's an example code:

import turtle

# create a turtle object
t = turtle.Turtle()

# define the vertices of the polygon
vertices = [(0, 0), (200, 0), (200, 200), (0, 200)]

# draw the outline of the polygon
for v in vertices:
    t.goto(v)
t.goto(vertices[0])

# fill the polygon with hatch lines
t.begin_fill()
for x in range(0, 200):
    for v in vertices:
        t.goto(v)
        t.setheading(t.towards((v[0], x)))
        t.forward(x)
t.end_fill()

# hide the turtle object
t.hideturtle()

# get the window to stay open until closed manually
turtle.mainloop()

In this code, the vertices of the polygon are defined and the turtle object is used to draw the outline of it. Then, a loop is used to draw diagonal lines inside the polygon to fill it with hatch lines. Finally, the turtle object is hidden and the window is kept open until closed manually.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-06 11:00:00 +0000

Seen: 7 times

Last updated: Jun 04 '22