@masonjargon/

Fractal Turtle

Python (with Turtle)

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3562928215253178","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"JycnClVzZSBSZWN1cnNpb24gdG8gZHJhdyBhICJmcmFjdGFsIiB0cmlhbmdsZQpOT1RFOiAgdHJpYW5nbGUgY2FsbHMgdHJpZm9yY2Ugd2hpY2ggY2FsbHMgdHJpYW5nbGUgZXRjLgonJycKCmltcG9ydCBtYXRoCmltcG9ydCB0dXJ0bGUKCnQgPSB0dXJ0bGUuVHVydGxlKCkKCnQuc3BlZWQoMCkKdC5wZW51cCgpCgojdC5iZ2NvbG9yKCd0YW4nKQpjb2xzID0gKCdyZWQnLCAnYmx1ZScsICdnb2xkJywgJ29yYW5nZScsICdibGFjaycsICd3aGl0ZScsICd0dXJxdW9pc2UnKQoKZGVmIHRyaWZvcmNlKHgsIHksIHNpZGUsIGk9MCk6CiAgICAjIGRyYXcgdGhlIGJvdHRvbS1sZWZ0IHRyaWFuZ2xlLCB0aGVuIGJvdHRvbS1yaWdodCwgdGhlbiB0b3AsIHRoZW4gaW52ZXJ0ZWQgbWlkZGxlCiAgICB0cmlhbmdsZSh4LCAgICAgICAgICB5LCAgICAgICAgICAgICAgICAgICAgICBzaWRlLCBpKSAKICAgIHRyaWFuZ2xlKHgrc2lkZSwgICAgIHksICAgICAgICAgICAgICAgICAgICAgIHNpZGUsIGkpCiAgICB0cmlhbmdsZSh4K3NpZGUvMiwgICB5K3NpZGUvMiptYXRoLnNxcnQoMyksICBzaWRlLCBpKQogICAgIyBPUFRJT05BTC4uLiBGaWxsIGluIHRoZSAiaW52ZXJ0ZWQgbWlkZGxlIiB0cmlhbmdsZS4uLgogICAgaWYgRmFsc2U6CiAgICAgIHQubGVmdCgxODApCiAgICAgIHRyaWFuZ2xlKHgrMS41KnNpZGUsIHkrc2lkZS8yKm1hdGguc3FydCgzKSwgIC1hYnMoc2lkZS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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))

# start with a centered, large triangle...
side = 500
height = side/2*math.sqrt(3)
triangle(-side/2, -height/2, side)

# Park Turtle somewhere dramatic
t.setposition(0,-height/5)
left(90)
result
console