My code isn't able to produce a symmetric pythagorean tree as shown above.
from turtle import *
from math import sin, cos
def 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 AC
def 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, AC
if __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.

This isn't exactly the right way to use stackoverflow, but instead of giving a full answer, I'll just help you start debugging
First, I turned off the fill command so you can see the outlines instead Then I removed the second called to the tree drawing command. It's better off to start by debugging just one call
This is what I got
So it appears you are on the right track. You just need calculate how tall each square is in the y direction and offset by that
Good luck debugging and you can comment with more questions as you go along