;Draws simple l-system consisting of an arbitrary ; list of +, -, and f where + and - are always ; right and left turns of 90 degrees and f is ; always a straight line of 20 pixels. The state ; of the turtle is updated with the use of set!. (load "writeln.scm") (define drawSystem (lambda (cmdList) ;Turtle state (define x 0) (define y 0) (define angle 0) ;; Make an 800 x 500 frame (define frame (instantiate frame% ("L-System") (width 800) (height 500)) ) (define draw-SetUp (lambda (dc) ;Set the background color to lavender. ;(send dc set-background ; (make-object color% 220 200 255) ;) (send dc clear) (send dc set-smoothing 'smoothed) (send dc set-pen red-pen) (set! x 400) (set! y 250) (set! angle 0) ) ) (define processf (lambda (dc) (define dx 0) (define dy 0) (cond ((eq? angle 0) (set! dy -20)) ((eq? angle 90) (set! dx 20)) ((eq? angle 180) (set! dy 20)) ((eq? angle 270) (set! dx -20)) ) (let ((x1 (+ x dx)) (y1 (+ y dy))) (send dc draw-line x y x1 y1) ;(writeln x y x1 y1 dx dy) (set! x x1) (set! y y1) ) ) ) (define setAngle (lambda (delta) ;The use of modulo adds the delta to the ;current value of angle and sets it to ;290 of angle = -90 and to 0 if angle = 360. ;(set! angle (+ angle x)) ;(if (= angle -90) (set! angle 270)) ;(if (= angle 360) (set! angle 0)) (set! angle (modulo (+ angle delta) 360)) ;(writeln x y angle) ) ) (define draw-cmdList (lambda (dc mylist) (cond ((null? mylist) 0) (else (let ((c (car mylist))) (cond ((eq? c '+) (setAngle 90)) ((eq? c '-) (setAngle -90)) ((eq? c 'f) (processf dc)) ) (draw-cmdList dc (cdr mylist)) ) ) ) ) ) ;; Make the drawing area with a paint callback (define canvas (instantiate canvas% (frame) (paint-callback (lambda (canvas dc) (reDrawEverything dc)) ) ) ) (define (reDrawEverything dc) (draw-SetUp dc) (draw-cmdList dc cmdList) ) ;Create a pen with color red, thickness 2, ; and line type solid (define red-pen (instantiate pen% ("RED" 2 'solid)) ) ;; Show the frame (send frame show #t) ) ) (drawSystem '(f + f - f + f - f + f)) (drawSystem '(f - f - f - f f + f + f + f)) (drawSystem '(f + f + f f + f f + f f f + f f f + f f f f + f f f f)) (drawSystem '(f - f - f f - f f - f f f - f f f - f f f f - f f f f))