[plt-scheme] Image identity hackery

From: Jordan Johnson (jmj at fellowhuman.com)
Date: Thu Dec 3 20:21:54 EST 2009

Hi all,

Last year I had a student ask me:  Hey, if

     (sum empty)

is 0, and

     (product empty)

is 1, what can we use for some function that makes an image?

This led to a neat little discussion of inverses with respect to a given
operation, and we didn't pursue the image question much beyond musing
that there's no obvious image I such that

     (overlay X I) = X
     (overlay I X) = X

for all x (in the sense of image=?).

Today I gave the students an assignment involving color-lists, and one
student observed that

     (image->color-list (color-list->image empty 0 0 0 0))

produces the empty list.  We experimented, and found that the image
given by

     (color-list->image empty 0 0 0 0)

satisfies the overlay equations given above.  For that matter, it also

     (overlay/xy I 0 0 X) = X
and (overlay/xy X m n I) = X

for all m in [0, (image-width X)] and n in [0, (image-height X)].

I get the impression the HtDP image code wasn't intentionally written to
handle this as an "identity image" -- it breaks rather gracelessly when
used as a background scene for place-image, for example -- but it still
afforded me a neat conversation with the student and some interesting
coding after class (see below).

Has anybody else played with this at all?


;; ----- sample code -----

;; loc = listof[color]

(define id-image (color-list->image empty 0 0 0 0))

;; make-swatches : loc number number -> image
;; creates a rectangle showing the colors in the list from left to
;; right, each as rectangle with width w and height h.
(define (make-swatches aloc w h)
     [(empty? aloc) id-image]
     [(cons? aloc)
      (overlay/xy (nw:rectangle w h 'solid (first aloc))
                  w 0
                  (make-swatches (rest aloc) w h))]))

(check-expect (make-swatches (list 'red 'blue 'green) 20 30)
               (overlay/xy (nw:rectangle 20 30 'solid 'red)
                           20 0
                           (overlay/xy (nw:rectangle 20 30 'solid 'blue)
                                       20 0
                                       (nw:rectangle 20 30 'solid 'green))))

Posted on the users mailing list.