sicp-manual.txt

The SICP Picture Language

This package provides support for the picture language used in SICP.
The non-standard primitives cons-stream and amb are
also provided.

    1 Introduction

    2 Reference

    3 Example

    4 Vectors

    5 Frames

    6 Segments

    7 Primitive Painters

    8 Higher Order Painters

    9 Simple Builtin Painters

    10 Painting

    11 Authors

    12 other

1. Introduction

The SICP Picture Language is a small language for drawing pictures.
It shows the power of data abstraction and closure. The picture language
stems from Peter Henderson’s 1982 paper "Functional Geometry" and was
included by Hal Abelson in "Structure and Interpretation of Computer
Programs".

Before using this package, read section 2.2.4 of SICP, which is
an excellent introduction to the ideas of the picture language.
The documentation below is only meant as a quick reference guide.

<http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4>

Peter Henderson has written an updated version of "Functional Geometry",
which explains how to construct the Escher fish image.

<http://eprints.ecs.soton.ac.uk/7577/01/funcgeo2.pdf>

Note: The primitives cons-stream and amb needed in other chapters
      of SICP is also provided.

2. Reference

The basic concept of the picture language is a _painter_. A painter draws
it’s image (shifted and scaled) within a frame given by a parallelogram.
Painters can be combined to construct new painters.

3. Example

  > >
  #<procedure:>>
  > (require (planet "sicp.ss" ("soegaard" "sicp.plt" 1 2)))
  eval:4:0: require: PLaneT could not find the requested
  package: Server had no matching package: No package matched
  the specified criteria in: (planet "sicp.ss" ("soegaard"
  "sicp.plt" 1 2))
  > >
  #<procedure:>>
  > (paint (number->painter 0))
  reference to undefined identifier: paint
  > >
  #<procedure:>>
  > (paint diagonal-shading)
  reference to undefined identifier: paint
  > >
  #<procedure:>>
  > (paint-hires  (below (beside diagonal-shading
                          (rotate90 diagonal-shading))
                  (beside (rotate270 diagonal-shading)
                          (rotate180 diagonal-shading))))
  reference to undefined identifier: paint-hires
  > >
  #<procedure:>>
  > (paint einstein)
  reference to undefined identifier: paint

4. Vectors

An mathematical vector is called a vect here, in order
to avoid confusion with the builtin vectors of Scheme.

> make-vect : number number -> vect
Constructs a vect with the given coordinates.

> vector-xcor : vect -> number
Returns the x-coordinate of the vect.

> vector-ycor : vect -> number
Returns the y-coordinate of the vect.

> vector-add : vect vect -> vect
Adds two vect by adding their coordinated pairwise.

> vector-sub : vect vect -> vect
Subtracts two vects by subtracting their coordinated pairwise.

> vector-scale : number vect -> vect
Scales the vect by multiplying each coordinate with the number.

5. Frames

A frame is descibed by three vectors.

^
      | frame edge2 vector
      |
     _|__________>
     /|         frame edge1 vector
    /
   /
  / frame origin pointer

> make-frame : origin edge1 edge2 -> frame
Constructs a frame from a frame origin vector and two frame edge vectors.

> frame-origin : frame -> vect
> frame-edge1 : frame -> vect
> frame-edge2 : frame -> vect
Extracts the origin, first edge or second edge from a frame.

> make-relative-frame : origin corner1 corner2 -> (frame -> frame)

The function make-relative-frame provides a convenient way to
transform frames.  Given a frame and three points : origin,
corner1, and corner2 (expressed in frame coordinates),
it returns a new frame with those corners.

> frame-coord-map : frame -> (vect -> vect)

Each frame determines a system of "frame coordinates" (x,y) where
(0,0) is the origin of the frame, x represents the displacement
along the first edge (as a fraction of the length of the edge) and
y is the displacement along the second edge.

The frame coordinate map is returned by frame-coord-map. E.g
these expression return the same value:
   ((frame-coord-map a-frame) (make-vect 0 0))
   (frame-origin a-frame)

6. Segments

A pair of vectors determines a directed line segment - the segment
running from the endpoint of the first vector to the endpoint of the
second vector.

> make-segment : vect vect -> segment
> segment-start : segment -> vect
> segment-end : segment -> vect

7. Primitive Painters

Painters take a frame and draw an image, transformed to fit inside the frame.

There are four ways to create painters:

1) from a constant:               number->painter
  2) from a list of line segments:  segment->painter
  3) form a procedure:              procedure->painter
  4) from a picture:                picture->painter

> number->painter : 0..255 -> painter
Constructs a painter that fills the frame with a gray color indicated
by the number. 0 is black and 255 is white.

> segments->painter : list-of-segment -> painter
Constructs a painter that draws a stick figure given by the
segments (wrt the unit square).

> procedure->painter : procedure -> painter

Creates painters from procedures.  We assume that the procedure
f is defined on the unit square.

Then to plot a point p in the target frame, we find the inverse image
T^-1(p) of p under the transformation that maps the unit square to the
target, and find the value of f at T-1(p).

> picture->painter : picture -> painter

The picture p is defined on some frame.

Given a point p in the target frame, we compute T^-1(p) where T
is the transformation that takes the picture frame to the
target frame, and find the picture value at the closest
integer point.

> load-painter : file-name -> painter
Uses the picture in file-name to create a painter.

8. Higher Order Painters

>  transform-painter : origin corner1 corner2 -> (painter -> painter)

A painter can be transformed to produce a new painter which, when
given a frame, calls the original painter on the transformed frame.

Transform-painter will given an origin and two corners, return
a function that takes a painter as argument and returns
a transformed painter.

> flip-horiz : painter -> painter
Returns a painter that flips the image horizontally.

> flip-vert : painter -> painter
Returns a painter that flips the image vertically.

> rotate90 : painter -> painter
> rotate180 : painter -> painter
> rotate270 : painter -> painter
Returns a painter that totates the image.

> beside : painter painter -> painter
Constructs a painter that paints the images side-by-side.

> below : painter painter -> painter
Constructs a painter that paints the second image
below the first.

> superpose : painter painter -> painter
Constructs a painter that paints the two images
on top of each other.

9. Simple Builtin Painters

The following painter values are buitin:

black, white and gray
     Fills the frame with black (0), white (255) or gray (150).

diagonal-shading
    Fills the frame with a shades of gray. The color transition
    goes from black in the upper left corner is black, to gray
    in the bottom right corner.

einstein
    Draws an image of Einstein.

10. Painting

The procedures paint and paint-hi-res takes a painter as input
and return a snip containing the painter’s image. A snip is
an image that DrScheme can display automatically.

> paint : painter -> snip
> paint-hi-res : painter -> snip

11. Authors

Abelson & Sussman:
  <http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html#%_sec_2.2.4>

Daniel Coore: Original MIT Scheme code

Mike Sperber: PLT port

Jens Axel Søgaard: Documentation

12. other

See also

<http://mitpress.mit.edu/sicp/psets/ps4hnd/readme.html>

for more documentation and exercises.

Peter Henderson’s "Functional Geometry":

<http://eprints.ecs.soton.ac.uk/7577/01/funcgeo2.pdf>

Keywords: _SICP_ _sicp_ _painter_ _geometry_ _picture_ _Escher_