Drawing regular polygons inside each other with specific specing using turtle module

Here is what the following code snippet draws; it is not an absolutely perfect reproduction of the gif you posted (some tweaks for the size progression for 3, 4, and 5-gons must be done to avoid the smaller neighbors to touch at some points – i/e the gif maker somewhat cheated!), but it follows mathematical symmetry and perfection.

enter image description here

The following code has some magic numbers; I may come back to it later, but not at this time. The resources I used to calculate a regular polygon can be found here, and there.

import turtle 
import math

def reg_polygon(start_pos, number_of_angles, side):
    interior_angle = (180 * (number_of_angles - 2)) / number_of_angles
    turtle.setheading(180 - interior_angle//2)

    for i in range(number_of_angles):
        turtle.left(180 - interior_angle)
def reset_start_point():
    global start_pos, startx, starty, initial_size, number_of_angles, side
    startx += 8
    starty -= 0
    initial_size += 8
    number_of_angles += 1
    side = 2 * initial_size * math.sin(math.radians(180/number_of_angles))
    start_pos = startx, starty
    turtle.goto((startx, starty))
start_pos = startx, starty = 0, 0
number_of_angles = 2
initial_size = 15  # radius
side = 0

while number_of_angles < 21:
    reg_polygon(start_pos, number_of_angles, side)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top