; Solution to fractals -- project 2, part 2 (load "snapl.scm") ; pick the Nth element of a list (N=1 means the car) ; (assumes n is an integer greater than 0) (define pick (lambda (n l) (if (= n 1) (car l) (pick (- n 1) (cdr l))))) ; pick a random element of the list (define random-pick (lambda (l) (pick (+ 1 (random (length l))) l))) ;; (choose P1 .. Pk) ;; randomly do one snapl program P1 .. Pk (define choose (lambda (tail env rest) (cond ((null? tail) (cons env rest)) (else (snapl (random-pick tail) env rest))))) ;; (split P1 .. Pk) ;; do all programs P1 .. Pk, leaving robot in starting configuration (define split (lambda (tail env rest) (cond ((null? tail) (cons env rest)) (else (split (cdr tail) env (cdr (snapl (car tail) env rest))))))) ; ------------------------- ; How much of each branch/leaf is branch (define base 0.6) (define tree-angle (/ pi 3)) ; a tree starts as a branch and a leaf (define tree-start '((turn (/ pi -2)) (mark branch base) (mark leaf (- 1 base)))) ; one of the tree branches in the leaf rule, with a little ; randomness in its direction added in (define tree-branch '((choose ((turn (/ pi 30))) ((turn (/ pi -30))) ()) (mark branch base) (mark leaf (- 1 base)))) ; editing rule for the tree (define tree-program '((switch (leaf (split ((turn (- tree-angle)) (call tree-branch)) ((call tree-branch)) ((turn tree-angle) (call tree-branch)))) (branch (mark branch 1))))) ; draw the tree (snapl-show (snapl-edit tree-program (snapl-edit tree-program (snapl-edit tree-program (snapl-edit tree-program (snapl-run tree-start))))))