Skip to content

Instantly share code, notes, and snippets.

@myst729
Last active April 1, 2018 06:04
Show Gist options
  • Save myst729/2cddf731915cc9655c13 to your computer and use it in GitHub Desktop.
Save myst729/2cddf731915cc9655c13 to your computer and use it in GitHub Desktop.
Pythagorean Triangle Tree
from turtle import *
import math
def square(a, b, level):
if level == 0:
return
side = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
angle = math.asin(0.6)
direction = math.atan2(b[1] - a[1], b[0] - a[0])
c = (b[0] - b[1] + a[1], b[0] + b[1] - a[0])
d = (a[0] + a[1] - b[1], b[0] - a[0] + a[1])
e = (d[0] + (math.sin(angle - direction) * side * 0.6), d[1] + (math.cos(angle - direction) * side * 0.6))
scale = 200 - 10 * level
hideturtle()
width(1)
color(scale, scale, scale)
speed(0)
begin_fill()
penup()
goto(*a)
pendown()
left(math.degrees(direction))
forward(side)
left(90)
forward(side)
left(90)
forward(side)
left(90)
forward(side)
left(90 - math.degrees(direction))
end_fill()
square(e, c, level - 1)
square(d, e, level - 1)
def main():
colormode(255)
square((-120, -200), (0, -200), 7)
if __name__ == '__main__':
main()
mainloop()
@zswang
Copy link

zswang commented Jun 19, 2015

强迫症排版了一下

from turtle import *
import math

def square(a, b, level):
    if level == 0:
        return

    side = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
    angle = math.asin(0.6)
    direction = math.atan2(b[1] - a[1], b[0] - a[0])

    c = (b[0] - b[1] + a[1], b[0] + b[1] - a[0])
    d = (a[0] + a[1] - b[1], b[0] - a[0] + a[1])
    e = (d[0] + (math.sin(angle - direction) * side * 0.6), d[1] + (math.cos(angle - direction) * side * 0.6))

    scale = 200 - 10 * level

    hideturtle()
    width(1)
    color(scale, scale, scale)
    speed(0)
    begin_fill()
    penup()
    goto(*a)
    pendown()
    direction = math.degrees(direction)
    left(direction)
    forward(side)
    left(90)
    forward(side)
    left(90)
    forward(side)
    left(90)
    forward(side)
    left(90 - direction)
    end_fill()

    square(e, c, level - 1)
    square(d, e, level - 1)

def main():
    colormode(255)
    square((-120, -200), (0, -200), 7)

if __name__ == '__main__':
    main()
    mainloop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment