(define *file-port* (open-output-file "bubbles3d.rot"))
(load "graphics3D-lib.scm")

(define *pi* 3.1415926)
(define bottom1V (mP3 .9414705 0 -.337095))
(define bottom2V (mP3 -.47073527 .8153374 -.337095))
(define bottom3V (mP3 -.47073527 -.8153374 -.337095))
(define top-centerV (mP3 0 0 1))

(define (generate-theta number-of-shapes index)
  (* index (/ *pi* number-of-shapes)))


(define (z-point center pole circles iteration)
  (coord3 center (addP3 center (mP3 (* (length3 center pole) (cos (generate-theta circles iteration)))
                      (* (length3 center pole) (sin (generate-theta circles iteration)))
                      0)) pole (mP3 0 0 1)))


;;Draws a 3d bubbles with center at the center point
;;and a pole at the pole point, with circles number of 
;;circles per sphere
(define (bubble3d centerP poleP circles n)
  (define (helper centerP poleP circles iteration n)
    (if (< iteration circles) 
        (begin (draw-arcP3 centerP (z-point centerP poleP circles iteration) poleP 360 360 (+ 1 (modulo n 2)))
               (helper centerP poleP circles (+ iteration 1) n))
        (if (= n 0) (begin (print " n: ") (print n)) ;;do nothing
            (let* ((scale-factor-top (* (length3 centerP poleP) 1.5))
                   (scale-factor-radius (* (length3 centerP poleP) 0.5))
                   (center-poleP    (addP3 centerP (scaleP3 scale-factor-radius (mP3 0 1 0))))
                   (top-centerP     (addP3 centerP (scaleP3 scale-factor-top (mP3 0 0 1))))
                   (top-poleP       (addP3 top-centerP (scaleP3 scale-factor-radius (mP3 0 1 0))))
                   (bottom1-centerP (addP3 centerP (scaleP3 scale-factor-top bottom1V)))
                   (bottom1-poleP   (addP3 bottom1-centerP (scaleP3 scale-factor-radius (mP3 0 1 0))))
                   (bottom2-centerP (addP3 centerP (scaleP3 scale-factor-top bottom2V)))
                   (bottom2-poleP   (addP3 bottom2-centerP (scaleP3 scale-factor-radius (mP3 0 1 0))))
                   (bottom3-centerP (addP3 centerP (scaleP3 scale-factor-top bottom3V)))
                   (bottom3-poleP   (addP3 bottom3-centerP (scaleP3 scale-factor-radius (mP3 0 1 0)))))
              (begin (bubble3d centerP center-poleP circles (- n 1))
                     (bubble3d top-centerP top-poleP circles (- n 1))
                     (bubble3d bottom1-centerP bottom1-poleP circles (- n 1))
                     (bubble3d bottom2-centerP bottom2-poleP circles (- n 1))
                     (bubble3d bottom3-centerP bottom3-poleP circles (- n 1)))))))
  (helper centerP poleP circles 0 n))
    
(define (myRotater n) (bubble3d *C* *N* 4 n))

(write-rotater "bubbles3d1.rot" myRotater 3 "Bubbles 3D"
               "2001-10-10"
               "James Liao (jamliao@uclink)"
               "http://www.geocities.com/enufjames/")

(close-output-port *file-port*)