capodeicapi icon

Pythagorean Tree in Python Turtle Trinket

capodeicapi | PRO | 12/05/25 12:14:00 PM UTC | 0 ⭐ | 13263 👁️ | Never ⏰ | [python, turtle, trinket]
Python |

1 KB

|

Source Code

|

0 👍

/

0 👎

import turtle  
import math 
 
def fractal(aturt, depth, maxdepth):  
   if depth > maxdepth:  
     return  
   length = 100*((math.sqrt(2)/2)**depth)  
   anotherturt = aturt.clone()  
   aturt.forward(length)  
   aturt.left(45)  
   fractal(aturt, depth+1, maxdepth)  
   anotherturt.right(90)  
   anotherturt.forward(length)  
   anotherturt.left(90)  
   anotherturt.forward(length)  
   if depth != maxdepth:  
     turt3 = anotherturt.clone()  
     turt3.left(45)  
     turt3.forward(100*((math.sqrt(2)/2)**(1+depth)))  
     turt3.right(90)  
     fractal(turt3, depth+1, maxdepth)  
   anotherturt.left(90)  
   anotherturt.forward(length)  
 
def draw_fractal():  
   window = turtle.Screen()  
   turt = turtle.Turtle()  
   turt.hideturtle()  
   turt.penup()  
   turt.goto(-40, -180)  
   turt.pendown()  
   turt.speed(0)  
   turt.left(90)  
   #change the second argument to get a bigger tree!
   fractal(turt, 1, 2)  
   window.exitonclick()  
 
draw_fractal()  

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎