My code isn't able to produce a symmetric pythagorean tree as shown above.
from turtle import *from math import sin, cosdef main(): t = Turtle() # Initiate turtle to t t.speed(5) # Set turtle speed window = Screen() # Initialize window to draw window.bgcolor('white') window.tracer(True) # -- Parameters -- level = 3 # Set the degree here length = 200 a = 45 # Set the angle "alpha" here, must be less than 90 points = [Vec2D(0, 0), Vec2D(0, length), Vec2D(length, length), Vec2D(length, 0)] pythTree(t, length, level, a, points) # Program ends window.exitonclick()def pythTree(t, length, level: int, angle: int, points) -> None: if level > 0: drawSquare(t, points) # Calculate the lengths of the next two squares lengths = scaleLength(length, angle) # Calculate the position of the squares in the recursive call # -- Recursive Calls -- pythTree(t, lengths[0], level - 1, angle, [x.rotate(angle) * cos(angle) for x in points]) # Left Side BC pythTree(t, lengths[1], level - 1, angle, [x.rotate(-angle) * sin(angle) for x in points]) # Right Side ACdef drawSquare(t, points) -> None: t.begin_fill() for point in points: t.goto(point) t.goto(points[0]) t.end_fill() update()def scaleLength(length, angle: int) -> tuple[int | float, int | float]: # LEFT SIDE: BC BC = cos(angle) * length # RIGHT SIDE: AC AC = sin(angle) * length return BC, ACif __name__ == '__main__': main()
The squares are scaled (using cos / sin) and moved using the list comprehension.
However I couldn't manage to move the rotated and scaled squares to their proper position.
I tried:
- [x.rotate(angle) * cos(angle) + points[1] for x in points] to move the square to its proper position but it breaks past two levels.