Python code explanation for 5-Pointed Star (Pentagram)

When we enter the programming world, we start our journey with small programming experiments such as printing sequence of numbers and texts. Later we’re given to print several designs such as rectangles, triangles, and pyramids. But, how many of us tried to print a 5-pointed star?

At first, it seems very simple but when we look deeper it becomes more and more complicated. You cannot even find any published popular article for an algorithm to print symmetric 5-pointed star i.e. pentagram. In this article, you’ll find the python implementation of the pentagram.

Note: You need a basic mathematical understanding about the Cartesian coordinate system, equations of a straight line and a circle to understand the algorithm.

Intuition:

1. Pentagram can be drawn using 5 separate lines (L1-L5).

2. All the vertices of the pentagram can be placed on a circle.

Steps:

1. Choose a circle of radius r then divide its circumference into 5 parts and get coordinates of the 5 points.

2. Build 5 equations for straight lines of the pentagram using the above coordinates.

example: equation of the L1

3. Define n х n empty 2D list (matrix) where n is greater than the diameter of the chosen circle.

4. Fill the empty matrix using a symbol by checking whether each cell (xᵢ, yᵢ) fulfills the equations with a predefined error rate. Note that the circumference of the chosen circle should be taken as the boundary of those lines.

Python implementation for the 5-pointed star

Congratulations!!! You can get a 5-pointed star as below…

5-pointed star for the radius of 16

Software Engineer @WSO2 | Undergraduate @Department of Computer Science and Engineering of University of Moratuwa