;;; CS 61A - Scheme Contest ;;; ;;; Richard Stauffer ;;; 4/16/2012 ;;; ;;; Title: ;;; The Summer Tree ;;; ;;; Description: ;;; I should not be here ;;; Demography due today ;;; Procrastination ;Total Scheme word count: 586 ;(quotes counted as words) ;I modified scheme.py and scheme_primitives.py to include turtle functions: ; mode (allows shitching to standard direction mode which I think is easier to deal with) ; width (allows different sized lines to be drawn) ; color2 (expansion of pythons turtle.color function, takes 3 number arguments, r g b) ; color3 (expansion of pythons turtle.color function, takes list of (r g b)) ;(color2 and color3 could have been derived from color(colorstring) in scheme but this is very unnecessary) (mode '|standard|) (speed 0) (ht) ;6 words (define SIZE 360) ;default size is 360, but any positive number works (define SIZEN (- SIZE)) (define SPAN (* 2 SIZE)) (define SEED (+ SIZE 7)) ;every different SIZE generates a different tree ;17 words (define GRASSY (* 0.8 SIZEN)) (define GRASSC (list 0.06275 0.66275 0.15294)) (define GRASSSIZE (/ SIZE 20)) ;16 words (define SUNRAD (* 0.2 SIZE)) (define SUNX (- SIZE (* 1.5 SUNRAD))) (define SUNY (- SIZE (* 2.5 SUNRAD))) ;19 words (define LEAFSCALE (/ SIZE 60)) (define LEAFDIAG (* 1.41421356 LEAFSCALE)) ;10 words ;goes to x y without drawing anything (define (go x y) (pu) (goto x y) (pd)) ;9 words ;generates random number in [0, n - 1] (define (rand n) (set! SEED (modulo (* 16807 SEED) 2147483647)) (modulo SEED n)) ;generates random number in [a, b] (define (rnd a b) (+ (rand (- (+ b 1) a)) a)) ;25 words ;sets color to p percent between c1 and c2 (define (grad p c1 c2) ;returns number p percent between n1 and n2 (define (pb n1 n2) (+ n1 (* p (- n2 n1)))) (color2 (pb (car c1) (car c2)) (pb (cadr c1) (cadr c2)) (pb (caddr c1) (caddr c2)))) ;32 words ;fills linear vertical gradient c1 to c2 from y1 to y2 (define (fillgrad start y1 y2 c1 c2) (cond ((>= y1 y2) (grad (/ (- start y1) (- start y2)) c1 c2) (go SIZEN y1) (fd SPAN) (fillgrad start (- y1 1) y2 c1 c2)))) ;34 words ;draw line gradient (define (linegrad dist start s stop c1 c2) (cond ((> s stop) (grad (/ s start) c2 c1) (width s) (fd dist) (bk dist) (linegrad dist start (- s 1) stop c1 c2)))) ;33 words ;draws random grass right of x to end of screen (define (grass x) (cond ((< x SIZE) (goto x GRASSY) (goto x (+ GRASSY (rand GRASSSIZE))) (grass (+ x 1))))) ;20 words ;draw sun (define (sun rad c1 c2 stop) (cond ((> rad stop) (grad (/ rad SUNRAD) c1 c2) (begin_fill) (circle rad) (end_fill) (goto SUNX (+ SUNY SUNRAD (- rad))) (sun (- rad 1) c1 c2 stop)))) ;34 words ;draws leaf (define (treeleaf) (define (rndd a b) (/ (rnd (* 100 a) (* 100 b)) 100)) (color2 (rndd 0.0 0.3) (rndd 0.7 1) (rndd 0.0 0.2)) (begin_fill) (width 1) (lt 45) (fd (* 2 LEAFDIAG)) (rt 135) (fd LEAFSCALE) (lt 45) (fd LEAFDIAG) (rt 90) (fd LEAFDIAG) (lt 45) (fd LEAFSCALE) (rt 135) (fd (* 2 LEAFDIAG)) (rt 135) (end_fill)) ;59 words ;draws tree, assume turtle is pointing in direction of desired growth (define (tree branches levels height base scale-height scale-base b-min b-max tree-ang tree-rand draw-leaf col-out col-in) (color3 col-out) (cond ((<= levels 0) (draw-leaf) (color3 col-out)) (else ;25 words ;draws a new branch (define (branch b) (cond ((> b 0) (define ang (+ (- (/ (* 2 tree-ang b) (+ 1 branches)) tree-ang) (rnd (- tree-rand) tree-rand))) (rt ang) (tree (rnd b-min b-max) (- levels (rnd 1 2)) (* scale-height height) (* scale-base base) scale-height scale-base b-min b-max tree-ang tree-rand draw-leaf col-out col-in) (lt ang) (branch (- b 1)) ))) ;56 words (width base) (fd height) (branch branches) (bk height) (linegrad height base base (* 0.25 base) col-out col-in) (width 1) ))) ;19 words ;clockwise square (define (sq w) (define (drawsq x s) (cond ((> s 0) (fd (* 2 x)) (lt 90) (drawsq x (- s 1)) ))) (go w w) (seth 180) (drawsq w 4)) ;30 words (define (colblack) (color2 0 0 0)) (define (colred) (color2 0.9294 0.1098 0.1412)) ;12 words ;stuff is actually drawn below ;background (fillgrad SIZE SIZE GRASSY (list 0.14118 0.48627 0.83529) (list 0.54118 0.94902 0.94902)) ;12 words ;tree (lt 90) (go 0 GRASSY) (tree 3 9 (* 0.30 SIZE) (* 0.1 SIZE) 0.8 0.6 2 3 80 10 treeleaf (list 0.4 0.1 0.05) (list 0.6 0.2 0)) ;29 words ;grass (rt 90) (color3 GRASSC) (grass SIZEN) (fillgrad GRASSY GRASSY SIZEN GRASSC (list 0.3 0.1 0.05)) ;15 words ;sun (go SUNX SUNY) (sun SUNRAD (list 1 1 0) (list 1 0.7 0.1) GRASSSIZE) ;14 words ;border (colblack) (sq SIZE) (colred) (sq (+ 1 SIZE)) (colblack) (sq (+ 2 SIZE)) (colred) (sq (+ 3 SIZE)) (sq (+ 4 SIZE)) (sq (+ 5 SIZE)) (colblack) (sq (+ 6 SIZE)) (colred) (sq (+ 7 SIZE)) (sq (+ 8 SIZE)) (sq (+ 9 SIZE)) (colblack) (sq (+ 10 SIZE)) (colred) (sq (+ 11 SIZE)) (colblack) (sq (+ 12 SIZE)) ;59 words ;(exitonclick) ;1 word ;word total calculation ;586 = 6 + 17 + 16 + 19 + 10 + 9 + 25 + 32 + 34 + 33 + 20 + 34 + 59 + 25 + 56 + 19 + 30 + 12 + 12 + 29 + 15 + 14 + 59 + 1