2019-12-07 02:38:45 -05:00
|
|
|
#lang racket
|
|
|
|
|
|
|
|
(require graphics/value-turtles)
|
|
|
|
(require file/convertible)
|
|
|
|
|
2019-12-07 03:52:14 -05:00
|
|
|
(define start '(X))
|
|
|
|
(define forward 4)
|
2019-12-07 02:38:45 -05:00
|
|
|
(define theta 20)
|
2019-12-07 03:52:14 -05:00
|
|
|
(define generations 7)
|
|
|
|
(define thick 8)
|
2019-12-07 02:38:45 -05:00
|
|
|
|
|
|
|
(define (tend sym)
|
|
|
|
(cond
|
|
|
|
[(eq? sym 'F) '(F F)]
|
2019-12-07 03:52:14 -05:00
|
|
|
[(eq? sym 'X) '(d F m l l X r p X r p F l p F X r m X)]
|
2019-12-07 02:38:45 -05:00
|
|
|
[else `(,sym)]))
|
|
|
|
|
|
|
|
(define (season plant)
|
|
|
|
(foldr append '()
|
|
|
|
(map tend plant)))
|
|
|
|
|
|
|
|
(define (grow seed years)
|
|
|
|
(if (<= years 0)
|
|
|
|
seed
|
|
|
|
(grow (season seed) (- years 1))))
|
|
|
|
|
|
|
|
(define init-turtle
|
|
|
|
(set-pen-width (turtles 600 800 300 400 (/ pi -2))
|
2019-12-07 03:52:14 -05:00
|
|
|
thick))
|
2019-12-07 02:38:45 -05:00
|
|
|
(define state '())
|
|
|
|
|
|
|
|
(define (step-turtle op turtle)
|
|
|
|
(cond
|
|
|
|
[(eq? op 'F) (draw forward turtle)]
|
|
|
|
[(eq? op 'm) (turn theta turtle)]
|
|
|
|
[(eq? op 'p) (turn (* -1 theta) turtle)]
|
2019-12-07 03:52:14 -05:00
|
|
|
[(eq? op 'u) (set-pen-width turtle
|
|
|
|
(+ 1 (turtles-pen-width turtle)))]
|
|
|
|
[(eq? op 'd) (set-pen-width turtle
|
|
|
|
(- (turtles-pen-width turtle) 1))]
|
2019-12-07 02:38:45 -05:00
|
|
|
[(eq? op 'l) (begin (set! state
|
|
|
|
(cons (turtle-state turtle)
|
|
|
|
state))
|
2019-12-07 03:52:14 -05:00
|
|
|
(set! thick
|
|
|
|
(cons (turtles-pen-width turtle)
|
|
|
|
thick))
|
2019-12-07 02:38:45 -05:00
|
|
|
turtle)]
|
2019-12-07 03:52:14 -05:00
|
|
|
[(eq? op 'r) (let ([s (car state)]
|
|
|
|
[t (car thick)])
|
|
|
|
(begin
|
|
|
|
(set! state (cdr state))
|
|
|
|
(set! thick (cdr thick))
|
|
|
|
(set-pen-width
|
|
|
|
(restore-turtle-state turtle s)
|
|
|
|
t)))]
|
2019-12-07 02:38:45 -05:00
|
|
|
[else turtle]))
|
|
|
|
|
|
|
|
(define (run-turtle plant turtle)
|
|
|
|
(if (null? plant)
|
|
|
|
turtle
|
|
|
|
(run-turtle (cdr plant)
|
|
|
|
(step-turtle (car plant) turtle))))
|
|
|
|
|
|
|
|
(define (write-plant turtle filename)
|
|
|
|
(let ([file (open-output-file filename #:exists 'replace)]
|
|
|
|
[pict (convert (turtles-pict turtle) 'png-bytes)])
|
|
|
|
(write-bytes pict file)))
|
|
|
|
|
|
|
|
(define (test-plant)
|
2019-12-07 03:52:14 -05:00
|
|
|
(let ([plant (grow start generations)])
|
2019-12-07 02:38:45 -05:00
|
|
|
(begin (set! state '())
|
|
|
|
(write-plant (run-turtle plant init-turtle)
|
|
|
|
"test-plant.png"))))
|
|
|
|
|
|
|
|
(test-plant)
|