Drawing regular polygons using polar coordinates and angle division.
- Author
- Sukesh Ashok Kumar
This example demonstrates how to create a regular hexagon (6-sided polygon) by placing vertices evenly around a circle. Hexagons appear in nature (honeycombs, snowflakes), engineering (bolt heads), and design (tiles).
Mathematical Concepts:
- Regular polygon: all sides equal length, all angles equal measure
- Vertices on a circle: all vertices equidistant from center
- Central angle: 360°/n = 60° for hexagon (n=6)
- Interior angle: (n-2)×180°/n = (6-2)×180°/6 = 120°
- Exterior angle: 360°/n = 360°/6 = 60°
- Sum of interior angles: (n-2)×180° = 4×180° = 720°
- Angle in radians: 2π/n = 2π/6 = π/3 ≈ 1.047 radians
Hexagon Properties:
- 6 vertices, 6 sides, 6 angles
- Each interior angle: 120°
- 6-fold rotational symmetry
- Tessellates perfectly (tiles plane without gaps)
- Found in honeycombs (optimal space-filling structure)
Programming Concepts:
- Regular polygon generation algorithm
- Polar to Cartesian coordinate conversion
- Circle division into equal sectors
- Modular arithmetic for wrapping (connecting last to first)
- Line drawing using linear interpolation
What you'll learn:
- How to create any regular polygon (change 'sides' variable)
- The relationship between circles and polygons
- Dividing a circle into equal angular segments
- Converting angular positions to screen coordinates
- Drawing closed shapes by connecting vertices
- Why regular polygons have vertices on a circle
Regular Polygon Algorithm:
- Choose number of sides (n)
- Calculate angular spacing: 2π/n
- For each vertex i (0 to n-1):
- Calculate angle: i × 2π/n
- Convert to coordinates: x = r·cos(angle), y = r·sin(angle)
- Draw line from current vertex to next vertex
- Last line connects back to first vertex (closing the shape)
Why This Works:
- Evenly spacing n points around a circle
- Connecting consecutive points with straight lines
- Creates polygon inscribed in circle
- All sides automatically equal length (chords of equal arcs)
Experiment Ideas:
- Change sides to 3: creates equilateral triangle
- Change sides to 4: creates square
- Change sides to 5: creates pentagon
- Change sides to 8: creates octagon
- Change sides to 20: approximates circle
Compile:
gcc m-hexagon.c gfx/simplegfx.c -o output/m-hexagon -lX11 -lm
Note: -lm links the math library for cos() and sin() functions
Run: