Building a House of Code

We often write programs like we build a house. We start with simple pieces, assemble them into more complex pieces, then put everything together to make a house. In this exercise you will first learn to use the overlay/offset function, then use that function to assemble images and create a house.

The overlay/offset function

(overlay/offset foreground-image dx dy background-image) -> image

This function produces a composite image, by placing a foreground image on top of a background image. It moves the background image dx pixels right and dy pixels down, relative to the center of both images.


;; define two rectangles
(define orange-rectangle (rectangle 60 30 "solid" "orange"))
(define blue-rectangle (rectangle 100 50 "solid" "blue"))

;; dx = 0 and dy = 0
;; orange rectangle is centered on top of the blue rectangle
(overlay/offset orange-rectangle 0 0 blue-rectangle)

;; dx = 10 and dy = 0
;; blue rectangle is moved right by 10 pixels
(overlay/offset orange-rectangle 10 0 blue-rectangle)

;; dx = 0 and dy = 20
;; blue rectangle is moved down by 20 pixels
(overlay/offset orange-rectangle 0 20 blue-rectangle)

Warm up Exercise

  1. Define the orange and blue rectangles shown in the above examples.
  2. Use the overlay/offset function to create the composite images in the examples.
  3. Create the images shown below. You may need to use negative values for dx or dy.

Build a House

For each step, define a variable that represents part of the house. Then use the overlay/offset function to assemble the parts. Choose your own colors for each part. Use the interactions window to verify each part as you build it. Your finished house should look something like the image below.

  1. Use the rectangle function to produce the front-wall, about 120 pixels wide x 70 pixels tall.
  2. Create a rectangle for the door-core.
  3. Use the circle function to create a door-knob.
  4. Assemble the doorknob and door-core to create a door.
  5. Create a rectangular window-frame.
  6. Create a rectangular glass-pane that is a little smaller than the window-pane.
  7. Assemble the window-pane and window-frame to create a window.
  8. Use the isosceles-triangle function to create the roof.
  9. Create a rectangular chimney.
  10. Assemble the roof and chimney to create roof-and-chimney.
  11. Assemble the foor-and-chimney and the front-wall to create the wall-and-roof.
  12. Assemble the window and wall-and-roof to create the almost-done-house.
  13. Assemble the door and almost-done-house to create the finished-house.
  14. Save your definitions to your class folder, and drag it to your teacher's dropbox.
Computer Programming Unit 3.1 - Challenge Exercise