LoftedSpring

From MHCGraphics

Jump to: navigation, search
; Mead example file -- a lofting example -- a spring
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
; based directly on a similar example by Duane A. Bailey
;
; $Id: LoftedSpring.scm 643 2008-04-20 19:04:45Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; our old friend multiAdd
(define (multiAdd n obj group initialXform deltaXform)
  (if (<= n 0) group
      (begin
        (tell group
              (add obj initialXform)
              )
        (multiAdd (- n 1) obj group
                  (compose initialXform deltaXform)
                  deltaXform)
        )
      )
  )

; first, a function, similar to multiAdd, that can create
; a list of polygons based on an initial transformation
; to place the first and a delta transformation to place the next
(define (multigon n poly initXform delXform)
  (if (= n 0)  ; is there more to do?
    '() ; an empty list of polygons
    (begin
      (cons ; cons takes an item (call it x) and a list (call it
            ; l), and builds a new list that has x as its first
            ; item (the car of the new list) and has l as the
            ; rest of the list (the cdr of the new list)
        ; the first item is this polygon:
        (polyXform poly initXform) ; we need polyXform for polygons
        ; the rest is this list of polygons, computed recursively
        (multigon (- n 1) poly
          (compose initXform delXform)
          delXform
        )
      )
    )
  )
)

; next, build a template polygon that will be the cross-sections
; of our lofted spring object:
(define springXsect (2to3d '((-10 -1) (10 -1) (10 1) (-10 1))))

; use multigon to make a list of polygons which are the
; translated and rotated copies of the template polygon
(define springFrame
  (multigon 37 springXsect
            (translate 100 0 0)
            (compose
             (translate 0 1 0)
             (yRot 10))))

; define a spring by applying the loft function to the list
; of polygons we've constructed -- this gives us a Mesh object
; which is one "loop" of the spring
(define springLoop (apply loft springFrame))

; let's add several loops -- we know that each loop
; has a vertical component of 36, so successive loops
; are offset by 36
(object spring Group)
(multiAdd 20 springLoop spring identity (translate 0 36 0))

; add one to the scene
(define ourSpring
  (tell scene
        (add spring magentaPlastic)
        )
)

; a floor
(tell scene
      (add (new Plane) yellowPlastic)
      )

(tell image
      (background white)
      (fileName "LoftedSpring")
      (frameNumber 0)
      (viewResult #f)
      )

; adjustment function to compress and uncompress the string
(define (squashSpring scalingFactor)
  (tell ourSpring
        (absoluteXform
         (scale 1 scalingFactor 1)
         )
        )
  )

(tell camera
      (pos '(0 300 -2000))
      (film 60 squashSpring '(1 .001 1))
      (buildMovie)
      )
Personal tools