Antonia
Antonia

Reputation: 13

How to use jupyturtle in JupyterLab to generate different polygons

I have written a function using jupyturtle in JupyterLab that creates a triangle and then used that to create different polygons with triangles inside of them.

It works well with hexagons correct image of a hexagon, however when I try it with different shapes (e.g. polygons - incorrect image of a pentagon due to spaces between triangles, heptagons), the angles aren't correct. There are gaps in between the triangles when the triangles should adjust to the size of the shape.

It seems like I need to change the triangle function so that it can create triangles that fit in the different shapes but I cannot seem to work out how to do this. I have tried amending the angle in draw pie but then found that the triangle lines do not align properly.

Here's my code:

    import jupyturtle
from jupyturtle import make_turtle, forward, left, right

def triangle():
    for _ in range(3):
        right(120)
        forward(-20)
        left(240)
# make_turtle()
# triangle()

def draw_pie(sides, side_length):
    angle = round(360 / sides)
    for _ in range(sides):
        # Draw triangle at the new position
        left(angle)
        triangle()

make_turtle()
draw_pie(6, 10)

Could someone please help?

As you can probably tell, I'm still pretty new to coding.

Upvotes: 1

Views: 53

Answers (0)

Related Questions