FancyPokerChipStack

From MHCGraphics

Jump to: navigation, search
; Mead example file -- stack some poker chips, but not
; perfectly -- have some random "noise" in the stacking
;
; but this time, we'll also make better poker chips
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
;
; $Id: FancyPokerChipStack.scm 603 2008-04-02 04:38:31Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; our rand function from previous examples
(define (rand low high)
   (+ low (* (random) (- high low)))
  )

; a brand new function: imperfectStack
; stacks up a collection of objects
; parameters:
; n - how many objects to stack
; obj - the objects to stack
; group - the group where copies of obj are added
; initialXform - a transformation to apply to obj to get
;      it to its initial position
; deltaY - the distance in the y dimension to translate
;      successive objects as they're stacked
; jitter - the amount in the x and z directions that each
;      object can randomly be translated to make our
;      stacks imperfect
(define (imperfectStack n obj group initialXform deltaY jitter)
  (if (<= n 0) group  ; nothing more to add
      (begin
        (tell group
              (add obj
                   ; to add this instance of obj, we apply
                   ; the initialXform then translate by
                   ; small random amounts in x and z
                   (compose 
                    initialXform
                    (translate (rand (- jitter) jitter)
                               0
                               (rand (- jitter) jitter))
                    )
                   )
              )
        ; to add the rest, we have n-1 to go, and we apply
        ; a translation by deltaY in the y dimension to
        ; the initialXform to get the starting position
        ; for the next item we'll add -- other parameters
        ; just come along for the ride
        (imperfectStack (- n 1) obj group
                        (compose initialXform
                                 (translate 0 deltaY 0))
                        deltaY jitter)
        )
      )
  )

; we'll stack some poker chips, which start as 
; 100x10x100 cylinders
(define chipDiameter 100)
(define chipThickness 10)
(object chipBasic Cylinder
        (scale .01 .01 .01)
        (scale chipDiameter chipThickness chipDiameter)
        (translate 0 (/ chipThickness 2) 0)
        )

; now we make the chips look more realistic -- put some
; holes and grooves in the top and bottom
; we start building a Difference object by adding the 
; basic chip -- this is "positive" matter.  Subsequent
; adds to this Difference will be "negative" matter and
; will result in some of the object being cut away.
(object chip Difference
        (add chipBasic)
        )

; make little round indentations on each side, 1/5 of the
; overall thickness
(define indentationPct .2)
(tell chip
      (add cylinder 
           (compose
            ; scale to appropriate size in x and z
            (scale .01 1 .01)
            (scale (/ chipDiameter 2) 1 (/ chipDiameter 2))
            ; translate down below the xz plane
            (translate 0 -50 0)
            ; translate up just enough to cut out the hole
            (translate 0 (* chipThickness indentationPct) 0)
            )
           )
      (add cylinder 
           (compose
            ; scale to appropriate size in x and z
            (scale .01 1 .01)
            (scale (/ chipDiameter 2) 1 (/ chipDiameter 2))
            ; translate above the xz plane
            (translate 0 50 0)
            ; translate up just enough to cut out the hole
            (translate 0 
                       (* chipThickness (- 1 indentationPct))
                       0)
            )
           )
      )

; now add some little grooves

; define a squashed down cube that will be used to cut out
; the grooves
(object grooveCutter Cube
        (scale .01 1 .01)
        (scale (* 1.1 chipDiameter) 1
               (* chipThickness indentationPct))
        (translate 0 50 0)
        )

; it's the same multiAdd we've seen before, but used for
; a different purpose
(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)
        )
      )
  )

; define the number of grooves on each half cylinder
(define numGrooves 18)

; below, we use multiAdd to add to a Difference object, so
; the things we're "adding" are really subtracting off
; some of the object!  Now THAT'S programming.
; first, the ones on top
(multiAdd numGrooves grooveCutter chip 
          (translate 0 
                     (* chipThickness (- 1 indentationPct)) 0)
          (yRot (/ 180 numGrooves))
          )

; then the ones on the bottom
(multiAdd numGrooves grooveCutter chip 
          (compose 
           (translate 0 -100 0)
           (translate 0 (* chipThickness indentationPct) 0)
           )
          (yRot (/ 180 numGrooves))
          )

; create a group of 10 chips to be red
(object redStack Group)
(imperfectStack 10 chip redStack 
                (translate 0 0 0) chipThickness 5)

; and a group of 15 that we'll color black
(object blackStack Group)
(imperfectStack 15 chip blackStack 
                (translate 0 0 0) chipThickness 7)

(tell scene
      (add redStack redPlastic
           (translate -100 0 0))
      (add blackStack blackPlastic
           (translate 100 0 0))
      (add bulb (translate 0 500 -700))
      )

(tell image
      (background white)
      )

(tell camera
      (pos '(0 300 -500))
      (shoot))
Personal tools