Growth Example Code


(define (draw-trunk)
 (separator
  (rotate -90 (vec3 1 0 0))
  (cylinder 'radius .1)
  ))

(define (draw-leaf)
  (separator
   (color (vec3 0 .5 0))
   (rotate 45 (vec3 1 0 0))
   (scale (vec3 .125 .25 .125))
   (translate (vec3 0 1 0))
   (disk)
   ))

; seed passed so we can get the same thing for each frame
(define (randR seed low high) 
  (begin
    (srand seed)
    (+ low (* (rand) (- high low)))))

; the maximum depth the tree will grow to
(define max-depth 7)

; age remains constant through the drawing of the entire tree
; and determines how fully grown the tree is.

(define (tree start-depth end-depth age)
  
  ; Depth-age is used in determining the scale amount.
  ; It takes into consideration:
  ;      * how "deep" in the tree the branch we are now drawing is
  ;      * the age of the tree
  ; 
  ; The deeper the branch the smaller it should be      ->  da ~ 1/sd
  ; The older the tree the larger the branch should be  ->  da ~ age
  ;
  ;     age - (sd-1) / md
  ;   ---------------------
  ;    (md - sd - 1) / md)

  (define depth-age (/ (- age (/ (- start-depth 1) max-depth))
		       (/ (- max-depth (- start-depth 1)) max-depth)))
	
  ; amount to shrink down trunk: want branches to grow fast, then slow down
  ; so used sine of 0->90 degrees
  (define scale-amount 
    (sin (* 1.57 (clamp (lerp .01 1 depth-age) .01 1))))

  
  (cond 
   
   ; if drawing the final depth, then draw leaves
   ((and (= end-depth max-depth) 
	 (= start-depth end-depth))
    (separator
      (uscale scale-amount)
      (draw-leaf depth-age)
      )
    )
   
   ((<= start-depth end-depth)
    (separator
      
      (separator
	(uscale scale-amount)
	(draw-trunk)
	)
   
      (if (< start-depth end-depth)
	  (begin
	    
	    (separator
	      (translate 0 scale-amount 0)
	      (rotate (+ 0 (randR (+ start-depth 1) -30 30)) (vec3 0 1 0))
	      (rotate (+ 22.7 (randR (+ start-depth 2) -15 15)) (vec3 0 0 1))
	      (uscale (+ .8 (randR (+ start-depth 3) -.1 .1)))
	      (tree (add1 start-depth) end-depth age)
	      )
	    
	    (separator
	      (translate 0 scale-amount 0)
	      (rotate (+ 120 (randR (+ start-depth 4) -30 30)) (vec3 0 1 0))
	      (rotate (+ 22.7 (randR (+ start-depth 5) -15 15)) (vec3 0 0 1))
	      (uscale (+ .8 (randR (+ start-depth 6) -.1 .1)))
	      (tree (add1 start-depth) end-depth age)
	      )
	    
	    (separator
	      (translate 0 scale-amount 0)
	      (rotate (+ 240 (randR (+ start-depth 7) -30 30)) (vec3 0 1 0))
	      (rotate (+ 22.7 (randR (+ start-depth 8) -15 15)) (vec3 0 0 1))
	      (uscale (+ .8 (randR (+ start-depth 9) -.1 .1)))
	      (tree (add1 start-depth) end-depth age)
	      )
	    )
	  )
      )
    )
   
    (else ())
    )
  )

;
; age ranges from 0 -> 1
;
(define (make-tree age)

  ; the depth of the tree is based on it's age
  (define end-depth (floor (+ (* age max-depth) 1)))
  (if (>= age 1) (set! end-depth max-depth))

  (tree 1 end-depth age)
 )


Return to growth section
mrl