Recursive Decagons in a Row using Python Turtle Graphics

preview_player
Показать описание
This is a Python script that uses the Turtle graphics library to draw a pattern of decagons in a row using recursion. The script starts by importing the necessary libraries and defining the initial setup for the Turtle graphics screen.

The function decagonsinarow is then defined, which is the recursive function that draws the pattern of decagons. This function takes four parameters: x, y, length, and n. x and y are the starting coordinates for the decagon, length is the length of the sides of the decagon, and n is the number of levels of recursion.

The function starts by checking if n is equal to 0. If it is, the function exits the recursive call. Otherwise, the function sets the Turtle graphics pen to the starting coordinates, generates a random color for the decagon, and then draws the decagon.

The function then recursively calls itself twice to draw the next row of decagons. The next row of decagons has a smaller size (length/2) and is positioned at different x, y coordinates. The function also decrements n by 1 to ensure that the recursion ends after the specified number of levels.

Finally, the function decagonsinarow is called with an initial size of 600 and 6 levels of recursion. The screen is updated to show the completed pattern, and the Turtle graphics window is kept open until it is closed by the user.

.py link:
Рекомендации по теме
visit shbcf.ru