2D Shapes using Python Turtle

In this challenge we will use Python Turtle to draw regular polygons including:

  • An equilateral triangle
  • A square
  • A pentagon
  • An hexagon
  • etc.

Did you know?


In Euclidean geometry, a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). Regular polygons may be convex or star.

Convex Regular Polygons


Looking at the following three polygons, we can work out a formula to calculate the external angle of a convex regular polygon based on its number of sides.
exterior-angle-triangle

exterior-angle-square

exterior-angle-pentagon

Exterior Angle = 360 / number of sides

Python Code


Based on this formula we have implanted a function called drawPolygon() which takes one parameter called numberOfSides. This function will draw the corresponding convex regular polygon on screen.

Star Regular Polygons


star-polygons
Your challenge consists of coding a similar function, called drawStar(), used to draw star regular polygons on the screen. This function will take two parameters called numberOfSides and multiplier.

This time we will use a different formula to calculate the exterior angle:

Exterior Angle = multiplier * (360 / number of sides)

You can test your code with the following parameter values:

Star # Number of Sides Multiplier
#1 5 2
#2 8 3
#3 9 4
#4 10 3
#5 12 5
Share Button
Posted in Computer Science, Python - Advanced, Python Challenges Tagged with:

Our Latest Book

Follow this blog