filmov
tv
Creating a Beautiful Fractal Tree using Python and Turtle Graphics

Показать описание
This is a Python script that generates a fractal tree using the turtle module in Python. The fractal tree is generated using a recursive function that starts with a main trunk and then branches out into smaller branches. The angle between the branches is chosen randomly, and the length of the branches is scaled based on the golden ratio.
The script starts by importing the turtle, math, and random modules, as well as the PIL (Python Imaging Library) module for saving the final image. It then sets up the turtle screen with the desired dimensions, sets the turtle speed to maximum, and hides the turtle cursor.
The golden ratio is calculated using the mathematical formula (1+5^0.5)/2. This value is used to determine the length of the branches in the fractal tree.
The golden_fractal_tree() function is defined to generate the fractal tree. It takes four arguments: x, y, direction, and length. These parameters define the starting position, direction, and length of the first branch.
Inside the function, the turtle is moved to the starting position, and the pen size and color are set based on the length of the branch. The branch is then drawn, and if the length of the branch is below a certain threshold, the function returns without generating any more branches.
If the length of the branch is above the threshold, the turtle's current position is saved, and a random angle is generated for the next branches. The function is then called recursively three times, with the new branches generated at the saved position and with the appropriate angles and lengths.
Finally, the golden_fractal_tree() function is called with the starting parameters to generate the full fractal tree, and the turtle is set to "done" mode to display the final result.
This code will generate a fractal tree with different branches and colors every time it is run, due to the random angles and colors generated. The final image can be saved using the PIL module by adding the following code at the end:
.py link:
The script starts by importing the turtle, math, and random modules, as well as the PIL (Python Imaging Library) module for saving the final image. It then sets up the turtle screen with the desired dimensions, sets the turtle speed to maximum, and hides the turtle cursor.
The golden ratio is calculated using the mathematical formula (1+5^0.5)/2. This value is used to determine the length of the branches in the fractal tree.
The golden_fractal_tree() function is defined to generate the fractal tree. It takes four arguments: x, y, direction, and length. These parameters define the starting position, direction, and length of the first branch.
Inside the function, the turtle is moved to the starting position, and the pen size and color are set based on the length of the branch. The branch is then drawn, and if the length of the branch is below a certain threshold, the function returns without generating any more branches.
If the length of the branch is above the threshold, the turtle's current position is saved, and a random angle is generated for the next branches. The function is then called recursively three times, with the new branches generated at the saved position and with the appropriate angles and lengths.
Finally, the golden_fractal_tree() function is called with the starting parameters to generate the full fractal tree, and the turtle is set to "done" mode to display the final result.
This code will generate a fractal tree with different branches and colors every time it is run, due to the random angles and colors generated. The final image can be saved using the PIL module by adding the following code at the end:
.py link: