Project 1 · Lecture 01 · 10 min
Project Description
Design an immutable regular Polygon class and a finite, sliceable sequence of polygons, then find the polygon with the best area-to-perimeter ratio.
The problem
You’re going to build two classes.
Polygon: a regular, strictly convex polygon. “Regular” means all edges have the same length and all interior angles are equal. Every polygon is centered at the origin and defined by just two numbers:n, the number of vertices (at least3);R, the circumradius: the distance from the center to any vertex.
Polygons: a finite, read-only sequence ofPolygonobjects that share the same circumradiusR, starting with a triangle (n = 3) and going up to, and including, a polygon withmvertices.
Both should be immutable: once created, nothing about a polygon or the sequence can be changed.
Goal 1: the Polygon class
Polygon(n, R) should expose these read-only properties:
| Property | Meaning |
|---|---|
count_vertices |
number of vertices, n |
count_edges |
number of edges (also n) |
circumradius |
R |
interior_angle |
angle between two adjacent edges, in degrees |
side_length |
length of one edge |
apothem |
distance from the center to the midpoint of an edge |
area |
surface area |
perimeter |
total length of all edges |
It should also:
- have a
reprof the formPolygon(n=3, R=1); - raise
ValueErrorifn < 3; - support equality (
==): two polygons are equal when they have the same number of vertices and the same circumradius; - support ordering (
>and<) based on the number of vertices only.
The formulas
Picture the polygon as n identical isosceles triangles meeting at the center. Each one has two sides of length R and an angle of 2π/n at the center. Cutting one in half gives a right triangle with angle π/n, hypotenuse R, and legs equal to half an edge and the apothem. From that:
- interior angle =
(n - 2) × 180 / n - side length
s = 2 R sin(π / n) - apothem
a = R cos(π / n) - area =
n / 2 × s × a - perimeter =
n × s
Here are some reference values you can test against (rounded to 5 decimal places):
import math
def reference(n, R):
s = 2 * R * math.sin(math.pi / n)
a = R * math.cos(math.pi / n)
return dict(n=n, R=R,
interior_angle=(n - 2) * 180 / n,
side=round(s, 5), apothem=round(a, 5),
area=round(n / 2 * s * a, 5), perimeter=round(n * s, 5))
for n, R in [(3, 1), (4, 1), (6, 2), (12, 3)]:
print(reference(n, R))A square with R = 1 has side √2 ≈ 1.41421 and area 2; a hexagon’s side equals its circumradius. Those are good sanity checks.
Goal 2: the Polygons sequence
Polygons(m, R) is a sequence of the polygons with 3, 4, ..., m vertices, all with circumradius R. It should:
- raise
ValueErrorifm < 3; - support
len(): there arem - 2polygons; - support full indexing and slicing: positive and negative indices, ordinary and extended slices (with
IndexErrorfor an out-of-range index), and therefore iteration; - have a
reprof the formPolygons(m=6, R=1); - provide a
max_efficiency_polygonproperty returning the polygon with the highest area-to-perimeter ratio.
Hints
- Every property except
nandRis computed, so@propertymethods are a natural fit. A property without a setter is read-only, which gives you immutability almost for free. - Floating-point results like
side_lengthwon’t match reference values exactly. Compare them withmath.iscloseand a sensible tolerance, never with==. - For ordering, think about what Python does with
a < bwhenahas no__lt__, butbhas a__gt__. - The sequence doesn’t need to re-implement indexing and slicing. If you keep the polygons in a built-in sequence, you can delegate to it.
- Test as you go: write small
assert-based checks for each property before moving on.
Before reading the solution walkthroughs, open the Practice page for this section. Each goal is an auto-graded exercise with starter code and tests, so you can check your implementation as you build it.
Key takeaways
- A regular polygon is fully defined by its vertex count
nand circumradiusR; everything else is computed. - Goal 1 is an immutable
Polygonclass with geometric properties, equality and ordering. - Goal 2 is a read-only
Polygonssequence with full slicing support and amax_efficiency_polygonproperty.