+
1
|
skin
|
login
|
edit
αwiki++
::
pythagoras_tree
user:anonymous
_h1 pythagoras tree _p After [[Allen Guo|http://aguo.us/writings/pythagoras-tree.html]], drawing a pythagoras tree using a Racket library, [[Turtle Graphics|http://docs.racket-lang.org/turtles]], {pre #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) } {img {@ src="data/pythagoras-tree-3.jpg" width="100%"}} _p ... my purpose is to write my own version in lambdatalk. {pre °° 1) logo library {def canvas.init {lambda {} ...} {def home {lambda {} ...}} {def turn {lambda {:a} ...}} {def move {lambda {:l} ...}} {def draw {lambda {:l} ...}} {def split {lambda {:l} ...}} 2) defining function {def pythagoras-tree {lambda {:side} {if {< :side 3} then {home} else {turn 90} {move {/ :side 2}} {turn 90} {draw {/ :side 2}} {turn 90} {draw :side} {turn 90} {draw :side} {turn 90} {draw :side} {turn 90} {draw {/ :side 2}} {turn -45} {split {turn -90}} {move {* {/ :side 2} {sqrt 2}}} {turn -90} {pythagoras-tree {* {/ {sqrt 2} 2} :side}} }}} 3) calling function (in some canvas to define) {{canvas.init ...} {pythagoras-tree 100} } °°} _p Following this first attempt in page [[logo]]. Coming soon...