Chapter 2

Exploding Stars

Consider the following data definitions:

;; an animation is:
;;  - empty
;;  - (cons frame animation)

;; a frame is:
;;   (make-frame number number stage)
(define-struct frame (x y stage))

;; a stage is:
;;   - (make-explosion number number color)
(define-struct explosion (iradius oradius color))

An animation represents a firework flying in the sky. Each animation is a list of frames, and each frame is a stage in the animation, placed at a particular point in the night sky. The x and y fields of a frame indicate where it is placed. The coordinates start from the upper-left and x indicates how far to the right and y indicates how far down. Finally, each stage is a explosion (drawn as a star) with an inner radius, an outer radius, and a color. As an example, this animation:

(list (make-frame 100 100 (make-explosion 10 20 'blue))
      (make-frame 140 140 (make-explosion 10 30 'blue))
      (make-frame 180 180 (make-explosion 10 40 'blue))
      (make-frame 220 220 (make-explosion 10 50 'blue)))

represents a star that moves and grows. It would look something like this:

To begin, design a function that builds the stages for an animation of an explosion that grows. Note. This function returns a list of stages, not frames.

;; expanding-stage : number number color -> list-of-stage
;; to build the stages starting at step number `step-num'
;;  running for `total-steps' steps.
(define (expanding-stage step-num total-steps color) ...)

It should produce a list whose length matches step-num, thus its template should be based on step-num. An important helper function for this function (and for others) is interoplate that accepts four numbers: a minimum, a maximum, a step number, and a total step count. It produces a number between the maximum and minimum, based on the step number and the total number of steps.

;; interpolate : number number number number -> number
(define (interpolate low high step total-steps)
  (+ (* low (/ step total-steps))
     (* high (- 1 (/ step total-steps)))))

Next, design a function that places a stage, always at the same coordinates.

;; place-stage : list-of-stage number number -> animation
(define (place-stage los x y) ...)

and a function that renders a frame:

;; frame->image : frame -> image
(define (frame->image frame) ...)

The frame->image should render the image on the (nearly) blank rectangle backdrop, where the x and y coordinates in the frame indicate how far down and to the right of the pinhole the image should be placed:

;; world-size : number
(define world-size 400)

;; backdrop : image
(define backdrop
  (put-pinhole
    (rectangle (+ world-size 2)
               (+ world-size 2)
               'outline
               'black)
    1 1))