Cover page images (keyboard)

Project 1 - Regular Polygons


due on Sunday, 11/22/09, before 11:59 PM

Sue Evans

Hit the space bar for next page

The Objective



The projects in this course give you an opportunity to work on larger problems.

Experience working on a larger problem using good design practices including:

For this project, you'll be required to use the design shown in this description.
For project 2, you will have to develop your own design.

The Background

A REGULAR POLYGON is defined as a convex polygon with all sides congruent and all angles congruent. The labeled drawings below show what is meant by the terms: exterior angle, interior (or vertex angle), central angle, and diagonal.

Realize that any regular polygon, not just the regular hexagon shown, can be divided into the same number of triangles as it has sides, with the base of each triangle being a side of the polygon and that the "tips" of the triangles join in the center of the polygon.

All formulae shown in the drawings and also those for perimeter and area shown later are courtesy of mathwords.com

For all formulae shown:
n - number of sides
s - length of a side
A - area of the polygon
P - perimeter of the polygon
P = n * s
A = 1/4 * n * s2 * cot(180° / n)

Names of Regular Polygons

Number of Sides Name
3Equilateral Triangle
4Square
5Regular Pentagon
6Regular Hexagon
7Regular Heptagon
8Regular Octagon
9Regular Nonagon
10Regular Decagon

The Task

Write a program that after printing a greeting to the user which contains information about regular polygons, presents the user with a nicely-aligned table that shows :

for all n-gons from 3 to 10, inclusive. The values in this table may NOT be hard coded. All of the values in the table MUST be calculated by the modular functions described in the design.

After presenting the table, your program must direct the user to input the number of sides of a polygon and the length of the sides. Use this information to calculate the perimeter and area of that polygon. Display the name of the polygon with its perimeter and area to the user. Restrict the user to regular polygons with n of 3 - 10, inclusive. The user should be allowed to continue to enter polygons as long as s/he wants and should signal the end by entering the sentinel 0 for n.

You are required to use Python's dictionary type to hold the polygon's names with the number of sides as the key.

Your program must be robust checking user input for validity and reprompt for a new value when the user is in error. This should be accomplished in the functions getValidNumSides() and getPositiveValue(), both of which should use post-test loops. See the sample output for examples of testing these functions.

The Design

The function headers given below must be used and may NOT be altered in anyway.
# printGreeting() provides information to the user about the program
# Inputs:  None
# Outputs: None
def printGreeting():

# generateTable() prints a table of data for regular polygons
# whose number of sides are from MIN to MAX
# Inputs:  min, the minimum number of sides
#          max, the maximum number of sides
# Outputs: None
def generateTable(min, max):

# printInstructions() explains that the user can now enter information
# about specific polygons (# of sides and length of the sides), so that 
# the perimeter and area can be calculated.
# Inputs:  None
# Outputs: None
def printInstructions():

# getValidNumSides() gets a valid number of sides from the user or the
# sentinel to quit.
# Inputs:  sentinel, the value to enter to quit
#          min, the minimum allowable number of sides
#          max, the maximum allowable number of sides
# Outputs: a valid numSides (either the sentinel or a number between
#          min and max, inclusive        
def getValidNumSides(sentinel, min, max):

# getPositiveValue() gets a value greater than 0 from the user
# Inputs:  None
# Outputs: a positive real number
def getPositiveValue():

# findExteriorAngles() calculates and returns the exterior angles
# of this polygon.
# Input:  numSides, the number of sides of this polygon
# Output: the exterior angle of the polygon in degrees
def findExteriorAngles(numSides):

# findAngleSum() calculates the sum of the interior angles
# of this polygon.
# Input:  numSides, the number of sides of this polygon
# Output: the sum of the interior angles
def findAngleSum(numSides):

# findInteriorAngles() calculates and returns the size of the interior 
# or vertex angles of the polygon.
# Inputs: angleSum, the sum of the interior angles
#         numSides, the number of sides of this polygon
# Output: the interior (or vertex angle) in degrees
def findInteriorAngles(angleSum, numSides):

# findNumDiagonals() caluclates and returns the number of diagonals
# of this polygon.
# Input:  numSides, the number of sides of this polygon
# Output: the number of diagonals
def findNumDiagonals(numSides):

# findPerimeter() calculates and returns the perimeter of the polygon
# Inputs: numSides, the number of sides of this polygon
#         length, the length of each of the sides
# Output: the perimeter of the polygon. 
def findPerimeter(numSides, length):

# findArea() calculates and returns the area of the polygon
# Inputs: numSides, the number of sides of this polygon
#         length, the length of each of the sides
# Output: the area of the polygon
def findArea (numSides, length):
The following symbolic constants should be used and they should be placed at the top of your proj1.py file, just after the imports:
# Symbolic constants
SENTINEL = 0
MIN = 3
MAX = 10
HALF_ROTATION = 180
FULL_ROTATION = 360
PI = 3.14159

The math library

  1. You must import math
  2. The math library does not have a cotangent function, but it does have a tangent function called tan(), which takes a float and returns a float. You should remember and use the trig identity:
    cot(theta) = 1/tan(theta)
  3. All of the math library trig functions take the angle measurements in radians, not degrees, so you'll have to make that conversion using the math.radians(x) function. It converts angle x from degrees to radians. It shouldn't surprise you to learn that there is also a math.degrees(x) function which converts the angle x from radians to degrees.

Sample Run

linux1[106] python proj1.py

Your greeting goes here

                        Table of Regular Polygon Facts

        Name          # Sides  Ext Ang  Int Ang Sum   Int Ang    Diags
Equilateral triangle       3   120.000      180        60.000       0
              Square       4    90.000      360        90.000       2
    Regular pentagon       5    72.000      540       108.000       5
     Regular hexagon       6    60.000      720       120.000       9
    Regular heptagon       7    51.429      900       128.571      14
     Regular octagon       8    45.000     1080       135.000      20
     Regular nonagon       9    40.000     1260       140.000      27
     Regular decagon      10    36.000     1440       144.000      35

Your instructions about entering the number of sides
and the length of one side for the calculations of perimeter
and area go here.


Enter number of sides, 0 to quit : 4
    Enter the length of one side : 4

              Square with sides of 4.000
                      Perimeter : 16.000
                           Area : 16.000

Enter number of sides, 0 to quit : 3
    Enter the length of one side : 4

Equilateral triangle with sides of 4.000
                      Perimeter : 12.000
                           Area : 6.928

Enter number of sides, 0 to quit : 6
    Enter the length of one side : 4

     Regular hexagon with sides of 4.000
                      Perimeter : 24.000
                           Area : 41.569

Enter number of sides, 0 to quit : 2
Sorry 3 - 10 or 0 only
Try again.
Enter number of sides, 0 to quit : 1
Sorry 3 - 10 or 0 only
Try again.
Enter number of sides, 0 to quit : -5
Sorry 3 - 10 or 0 only
Try again.
Enter number of sides, 0 to quit : -1
Sorry 3 - 10 or 0 only
Try again.
Enter number of sides, 0 to quit : 5
    Enter the length of one side : 2.5

    Regular pentagon with sides of 2.500
                      Perimeter : 12.500
                           Area : 10.753

Enter number of sides, 0 to quit : 7
    Enter the length of one side : 3.75

    Regular heptagon with sides of 3.750
                      Perimeter : 26.250
                           Area : 51.102

Enter number of sides, 0 to quit : 8
    Enter the length of one side : 25.67

     Regular octagon with sides of 25.670
                      Perimeter : 205.360
                           Area : 3181.690

Enter number of sides, 0 to quit : 9
    Enter the length of one side : 15.29

     Regular nonagon with sides of 15.290
                      Perimeter : 137.610
                           Area : 1445.214

Enter number of sides, 0 to quit : 10
    Enter the length of one side : 17

     Regular decagon with sides of 17.000
                      Perimeter : 170.000
                           Area : 2223.628

Enter number of sides, 0 to quit : 6
    Enter the length of one side : 0
Positive values only! Try again : -3
Positive values only! Try again : 6.2

     Regular hexagon with sides of 6.200
                      Perimeter : 37.200
                           Area : 99.870

Enter number of sides, 0 to quit : 0
linux1[107]

Although your output need not be identical to the above, all information (including the greeting) must be present.

Extra Credit

For 5 points of Extra Credit, when the user enters a specific polynomial for calculation of its perimeter and area, also open a graphics window and draw that polygon to scale. To prove the polygon is to scale, you'll need to have a grid shown. Have the user click on the window to close it.

For 5 more points of Extra Credit, make this graphical display fancier in some fashion. Be as creative as you like. It needs to be significantly different from the regular extra credit portion described above.

Submitting your work

You must be logged into your account and in the same directory as the file you are trying to submit.

To submit your project, type the following at the linux prompt:

submit cs201 Proj1 proj1.py

To verify that your project was submitted, you can execute the following command at the Unix prompt. It will show all files that you submitted in a format similar to the Unix 'ls' command.

submitls cs201 Proj1