@masonjargon/

# Fractal Turtle

## No description

main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Use Recursion to draw a "fractal" triangle
NOTE:  triangle calls triforce which calls triangle etc.
'''

import math
import turtle

t = turtle.Turtle()

t.speed(0)
t.penup()

#t.bgcolor('tan')
cols = ('red', 'blue', 'gold', 'orange', 'black', 'white', 'turquoise')

def triforce(x, y, side, i=0):
# draw the bottom-left triangle, then bottom-right, then top, then inverted middle
triangle(x,          y,                      side, i)
triangle(x+side,     y,                      side, i)
triangle(x+side/2,   y+side/2*math.sqrt(3),  side, i)
# OPTIONAL... Fill in the "inverted middle" triangle...
if False:
t.left(180)
triangle(x+1.5*side, y+side/2*math.sqrt(3),  -abs(side), i-2 )
t.left(180)

def triangle(x, y, side, i=0):
# The triangle starts with bottom-left corner at (x, y) ...
# ... it will (recursively) fill itself with 3 sub-triangles
col = cols[i % len(cols)]
t.color(col)
t.setposition(x, y)
t.begin_fill()
for s in range(3):
t.forward(abs(side))
t.left(120)
t.end_fill()
# Fill the triangle with a "tri-force" (recursion)
# if side-length is sufficiently large, draw the 3 sub-triangles
if abs(side/2) >= 6:
triforce(x, y, side/2.0, i+1)
print ("Debug: {}".format(side))