;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; NAME:        sierpinski-square-anim.scm
;;
;; DESCRIPTION: An example of the sierpinski square fractal animated.
;;              Evaluate the entire file.
;;
;; AUTHOR:      Dan Garcia  -  University of California at Berkeley
;;              Copyright (C) Dan Garcia, 2001. All rights reserved.
;;
;; DATE:        2001-09-21
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; In stk, we need to define all the fractions we use in dr. scheme/macgambit

(if *stk* 
    (begin
      (eval '(define  1/4  (/  1 4 )))
      (eval '(define  1/3  (/  1 3 )))
      (eval '(define  2/3  (/  2 3 )))
      (eval '(define -1/3  (/ -1 3 )))
      (eval '(define  1/20 (/  1 20)))
      (eval '(define  1/10 (/  1 10)))))

;;;;;;;;;;;;;;;;;;;
;; sierpinski-square-anim
;;
;;       Input: C [P] "Center"
;;              r [float] "radius of square"
;;              n [float] "recursion iteration level"
;; Side-Effect: Draw the sierpinski-square centered at C with radius r
;;              at "n" recursion depth
;;;;;;;;;;;;;;;;;;;

(define (sierpinski-square-anim C r n)
  (cond ((= n 0)
         (draw-squareP C r))
        
        ((< n 1) 
         (let* ((t  (- n (truncate n)))
                (rt (lin-interp r (/ r 3) t))
                (CNWt (lin-interpP C (addP C (scaleP 2/3 (mP (- r)    r ))) t))
                (CNEt (lin-interpP C (addP C (scaleP 2/3 (mP    r     r ))) t))
                (CSWt (lin-interpP C (addP C (scaleP 2/3 (mP (- r) (- r)))) t))
                (CSEt (lin-interpP C (addP C (scaleP 2/3 (mP    r  (- r)))) t)))
           (draw-squareP CNWt rt)
           (draw-squareP CNEt rt)
           (draw-squareP CSWt rt)
           (draw-squareP CSEt rt)
           (draw-squareP C    rt)))
           
      
        ;; Just recurse, as normal
        (else
         (let ((new-r (/ r 3))
               (CNW (addP C (scaleP 2/3 (mP (- r)    r  ))))
               (CNE (addP C (scaleP 2/3 (mP    r     r  ))))
               (CSW (addP C (scaleP 2/3 (mP (- r) (- r) ))))
               (CSE (addP C (scaleP 2/3 (mP    r  (- r) )))))
           (sierpinski-square-anim CNW new-r (- n 1))
           (sierpinski-square-anim CNE new-r (- n 1))
           (sierpinski-square-anim CSW new-r (- n 1))
           (sierpinski-square-anim CSE new-r (- n 1))
           (sierpinski-square-anim C   new-r (- n 1)) ))))

;;;;;;;;;;;;;;;;;;;
;; demo-sierpinski-square-alt-anim
;;
;;       Input: dragon? [bool] #t = dragon, #f = c-curve
;;            :    step [float] how much to advance fractal each step
;;            : filename [string] In stk, filename to save. If "", just display.
;;            : max-level [float] The max recursion level
;; Side-Effect: Animate c-curve/dragon through 'step' steps
;;;;;;;;;;;;;;;;;;;

(define (demo-sierpinski-square-anim step max-level filename animate?)
  (grow-fractal
    (lambda (i) 
      (sierpinski-square-anim *C* (- *Radius* 1) i))
    step max-level filename animate?))

;(demo-sierpinski-square-anim 1/10 4.09 "ssa" #t)  ;; write to file
;(demo-sierpinski-square-anim 1/4  4    ""    #f)  ;; just step
 (demo-sierpinski-square-anim 1/10 4.09 ""    #t)  ;; just animate