At My Fingertips

Documentation

Star

Description

In this activity we will implement a function called star that will allow us to create n-pointed stars for n larger than 2. This means three-pointed stars, four-pointed stars, five-pointed stars, and so on.

different pointed stars

The star function will also take as parameter the size of each vertex and the angle of each vertex. In our implementation, if the angle is too obtuse the results are not acceptable, therefore we will set a maximum_angle of 360 / n for each n being the number of vertices of the star.

Example: for five-pointed stars, the maximum angle is of 360° / 5 = 72°. In the image below you will see a five-pointed stars with vertices of 30°, one with vertices of 50° and one of vertices of 72°.

five pointed stars

Remember to assert that the given angle is lesser or equal to 360 / n!

Analysis of the problem

As shown in the image below, we notice that stars can be composed by creating n isosceles triangles, progressively rotated by a rotation angle of 180 - (180 * n - 360) / n degrees, and pinning them all in the bottom center.

triangle approach

The vertical isosceles triangle

Let's start by creating an isosceles_triangle function that returns an isosceles triangle rotated vertically. Keep in mind that calling triangle(160, 160, 40, STAR_COLOR) generates the following triangle.

isosceles triangle not rotated

To make it stand up straight you have to rotate it by the angle represented here below!

isosceles triangle rotation

Hint: the pytamaro rotate function rotates a graphic counter-clockwise by the given angle. To rotate clock-wise you have to provide a negative angle!

Loading...

The star

Now you can implement the star function. Use the given STAR_COLOR color to obtain the same color that we used.

  • parameter n is the number of vertices of the star, assert that it's at least 3
  • parameter size is approximately the size of each vertex, assert that it's a positive number
  • parameter angle is the angle that each of the vertices should have, assert that it's bigger than 0 and it's less than 360 / n

Remember that each vertex is rotated by a rotation angle of 180 - (180 * n - 360) / n from the previous one

Loading...

This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Star

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 1cd5229 (Tue, 05 Nov 2024 16:55:57 GMT)