On this page:
1 Calder mobiles
2 Challenge (extra credit)

Problem Set 6: Calder mobiles

Submit this assignment to ps6 on Handin.

Note: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.

1 Calder mobiles

Wikipedia describes a mobile as "a number of rods, from which weighted objects or further rods hang. The objects hanging from the rods balance each other, so that the rods remain more or less horizontal."

Our goal is to represent Calder mobiles with a self-referential data definition.

Below is Les Mouettes (The Seagulls), 1965:

Sheet metal, paint, metal rods and steel wire

Here are some places to view Calder mobiles online:

Typically, each rod in a mobile has two sub-mobiles hanging off of it. Let’s represent such a mobile using this data definition:
; A Mobile is one of:
; - (make-leaf Number)
; - (make-rod Mobile Number Number Mobile)
(define-struct leaf [weight])
(define-struct rod [left-mobile left-dist right-dist right-mobile])

Problem 1. Define three examples of Mobiles as constants, a leaf and two rods. Call them mobile1, mobile2 and mobile3.

Problem 2. Design a function weight that takes a Mobile as input and computes its total weight. For simplicity, assume that the connection between the two sub-mobiles of a rod adds no weight.

Hint: you will write several functions that process a Mobile in this assignment, so it’s useful to write the process-mobile template now and re-use it for all of the functions.

Problem 3. Design a function average-leaf-weight that takes a Mobile as input and computes its leaves’ average weight. You’ll need a helper function that also takes a Mobile as input.

Problem 4. First, provide the missing tests for the following helper function:
; balanced? : Mobile -> Boolean
; determines whether the given mobile is balanced at the top
(define (balanced? m)
  (cond [(leaf? m) true]
        [(rod? m) (= (* (rod-left-dist m) (weight (rod-left-mobile m)))
                     (* (rod-right-dist m) (weight (rod-right-mobile m))))]))

Next, design a function all-balanced? that takes a Mobile as input and returns a Boolean indicating whether it is balanced everywhere. Use balanced? as a helper function to compute and compare torques.

Problem 5. Design a function lighten that takes a Mobile as input and returns a new Mobile that is like the given one except that everything weighs half as much. (Your all-balanced? function should produce the same result for the given and returned mobiles.)

Problem 6. Design a function enlarge that takes a Number n and a Mobile as inputs and returns a new Mobile that is like the given one except that all the distances are n times longer. (Your weight, average-leaf-weight and all-balanced? functions should produce the same results for the given and returned mobiles.)

2 Challenge (extra credit)

Here are the data and structure definitions for a Painting:
; A Painting is one of:
; - (make-solid String)
; - (make-hsplit Painting Painting)
; - (make-vsplit Painting Painting)
(define-struct solid [color])
(define-struct hsplit [top bottom])
(define-struct vsplit [left right])

Every Painting can be drawn as a rectangle of any size. A solid Painting is drawn as a rectangle filled entirely with the given color. An hsplit Painting is divided into two rectangles of equal size, one on the top and one on the bottom. A vsplit Painting is also divided into two rectangles of equal size, but one on the left and one on the right. So h stands for "horizontal" and v stands for "vertical".

After you have defined several example Paintings, finish designing the following function so that you may visualize them:

; draw-painting : Painting Number Number -> Image
; draws the given painting with the given width and height
; (define (draw-painting p w h) ...)

In this challenge, you will design an interactive big-bang program that allows you to view a Painting and update the colors by clicking. For example, when I click on the rightmost rectangle of this picture

I see

.

To get started, we suggest that you design a program which turns any rectangle you click on to a particular color (e.g. "black"). This should be done with a function update which takes a Painting, a width, a height, an x-coordinate and a y-coordinate. The latter two inputs are understood as the point inside the rectangle whose color you would change. Note that the functions draw-painting and update do not have the required signatures to be used directly by big-bang; so you will have to design two corresponding functions which call them as helpers using a width and a height that you pick.

Once you have this working, define an enumeration which contains a selection of colors (your palette) and a function next-color which can be used to cycle the color of a rectangle by repeated clicking.