Python Forum
Circle Turtle - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: Circle Turtle (/thread-19274.html)



Circle Turtle - JoeLamond - Jun-20-2019

This program uses python turtle to draw regular polygons with up to 25 sides. Take a look:
import math
import turtle
import time

def cosineRule(b,c,A):
    aSquared = (b**2) + (c**2) - (2*b*c*math.cos(math.radians(A)))
    a = aSquared ** 0.5
    return a
       
def draw(t,sides):
    interiorAngles = 180*(sides-2)
    global interiorAngle
    interiorAngle = interiorAngles/sides
    t.setheading(90-((360-interiorAngle)/2))
    A = 360/sides
    sideLength = cosineRule(250,250,A)
    for i in range(sides-1):
        t.forward(sideLength)
        t.right(180-interiorAngle)
    t.forward(sideLength)

print("The circle turtle can draw any regular polygon with up to 25 sides.")
print("How many sides do you want the polygon to have?")

while True:
    try:
        sides = int(input())
        if sides < 3 or sides > 25:
            print("Please try again. Enter a whole number between 3 and 25.")
        else:
            break
    except ValueError:
        print("Please try again. Enter a whole number between 3 and 25.")

print("Do you want the circle turtle to draw a circumcircle? (Y/N/INFO)")
circumcircle = input().upper()
if circumcircle == "INFO":
    print("According to Merriam-Webster, a circumcircle is a circle which")
    print("passes through all the vertices of a polygon (such as a triangle).")
    print("So do you say yes? (Y/N)")
    circumcircle = input().upper()
    
while circumcircle != "Y" and circumcircle != "N":
    print("Please try again. Enter 'Y' or 'N' only.")
    circumcircle = input().upper()

print("The circle turtle is feeling generous today and wants to do an")
print("animation with every single polygon up to " + str(sides) + " sides.")
print("Do you say yes? (Y/N)")
animation = input().upper()
while animation != "Y" and animation != "N":
    print("Please try again. Enter 'Y' or 'N' only.")
    animation = input().upper()

turtle.hideturtle()
turtle.penup()
turtle.speed(3)
turtle.left(90)
turtle.forward(250)
turtle.pendown()

if circumcircle == "Y":
    circleTurtle = turtle.clone()
    circleTurtle.pencolor("#FF0000")
    circleTurtle.showturtle()
    draw(circleTurtle,100)
    if animation == "Y":
        circleTurtle.setheading(-60)
    else:
        circleTurtle.setheading(90-((360-interiorAngle)/2))

if animation == "Y":
    if circumcircle == "Y":
        turtle.setheading(-60)
        turtle.showturtle()
        circleTurtle.hideturtle()
    for i in range(3,sides):
        draw(turtle,i)
        time.sleep(0.5)
        turtle.clear()
    draw(turtle,sides)
    turtle.hideturtle()
else:
    if circumcircle == "Y":
        turtle.setheading(90-((360-interiorAngle)/2))
        turtle.showturtle()
        circleTurtle.hideturtle()
    draw(turtle,sides)