In this activity we will implement a function named star that will allow us to create n-pointed stars (for any n larger than 2).
This means three-pointed stars, four-pointed stars, five-pointed stars, and so on.
The star function will also take as parameter the "internal" angle of the vertices.
Weird results occur if the "internal" angle is too obtuse: we should accept only angles up to 360° / n, where n is the number of vertices of the star.
As an example, for five-pointed stars the maximum angle is 360° / 5 = 72°. Below we show three different five-pointed stars with vertices of 30°, 50° and 72°.
As shown in the image below, stars can be composed with n isosceles triangles, positioned in a circular fashion:
Let's start by creating an isosceles_triangle function that creates an upward-pointing isosceles triangle, with the two equal sides going from the top vertex to the two bottom vertices, and the bottom side being parallel to the horizontal axis.
We use the triangle function to create the triangle.
For example, calling triangle(160, 160, 40, STAR_COLOR) generates the following triangle:
We need to rotate it to make it point upward: the vertex currently at the bottom-left corner should become the top vertex. In short, we need to rotate it by the gray angle shown below:
It can be shown that the gray angle is a full right angle (90°), plus half of the internal angle of the triangle.
(Small hint: to rotate clockwise with rotate function, use a negative angle.)
Now we can implement the star function.
The meaning of its parameters is as follows:
The implementation already contains the formula to compute the side length of the isosceles triangles that compose the star, given the diameter and the internal angle.
Create each vertex by calling the isosceles_triangle function, and then rotate it by the appropriate angle to position it correctly.
Remember to pin each triangle at the bottom center before rotating it, so that it can be composed correctly with the other triangles.
Now experiment with different values of n, diameter and angle (and maybe even with other colors?) to create many different stars!
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Star

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version ee0bcd9a (Tue, 08 Sep 2026 07:32:33 GMT)