[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/rheehot/python_example/main/turtle_triangle_recursion.py [Back]  [Original]

from turtle import *
import math

step = int(input("     : "))
size = 400
if step < 1:
    min = size
elif step == 1:
    min = size / 2
else:
    min = size / (2**step)

#pf = 0.8660254  # : sqrt(3)/2
pf = math.sqrt(3)/2

# r,g,b    
def getRGB():
    r, g, b = 0, 0, 0
    r = random.random()
    g = random.random()
    b = random.random()
    return (r, g, b)

def D(l, x, y):
    if l > min:  #       (    )
        l = l / 2  #    
        D(l, x, y)  #   
        D(l, x + l, y)  #   
        D(l, x + l / 2, y + l * pf)  #  
    else:  #  
        goto(x, y);

        r, g, b = getRGB()
        color(r, g, b)

        pendown()  #  
        forward(l); # l 
        left(120)  # 120 
        forward(l); # l 
        left(120)  # 120 
        forward(l)  # l 
        setheading(0)  #   
        penup(); #  
        goto(x, y) #   


penup()
speed('fastest') #  
D(size, -size / 2, -size * pf / 2.0)
goto(0, 0)
done()

Web Proxy Viewer  |  New URL  |  Original Page