Last active
December 25, 2015 04:47
-
-
Save guoguo12/305d583c0f3e47461bfd to your computer and use it in GitHub Desktop.
Draws a Pythagoras tree using Racket's turtle graphics library. Read the article at http://aguo.us/writings/pythagoras-tree.html.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket | |
| ; pythagoras-tree.rkt: | |
| ; Draws a Pythagoras tree using Racket's turtle graphics library. | |
| ; Run this program from the terminal with `racket pythagoras-tree.rkt`. | |
| (require graphics/turtles) ; raco pkg install htdp-lib | |
| (turtles #t) | |
| (define (pythagoras-tree side) ; starting position: center of square, facing east | |
| (if (< side 3) ; this seems like a good stopping point... | |
| (home) ; clear all turtles except the original | |
| (let ((half-side (/ side 2))) | |
| (turn 90) ; now facing north | |
| (move half-side) ; move north | |
| (turn 90) ; now facing west | |
| (draw half-side) ; draw west | |
| (turn 90) ; now facing south | |
| (draw side) ; draw south | |
| (turn 90) ; now facing east | |
| (draw side) ; draw east | |
| (turn 90) ; now facing north | |
| (draw side) ; draw north | |
| (turn 90) ; now facing west | |
| (draw half-side) ; draw west | |
| (turn -45) ; turn turtle 1 (current turtle) to face northwest | |
| (split (turn -90)) ; create and turn turtle 2 to face northeast | |
| (move (* half-side (sqrt 2))) ; move both turtles to the centers of the new squares | |
| (turn -90) ; and adjust them to face east | |
| (pythagoras-tree (* (/ (sqrt 2) 2) side))))) ; recurse! | |
| (pythagoras-tree 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment