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 Installation
Open in Package Manager in DrRacket: "File | Package Manager...". In the tab "Do What I Mean" find the text field and enter: sicp
Then click the Install button.
2 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. This manual is meant as a reference guide.
Peter Henderson has written an updated version of "Functional Geometry", which explains how to construct the Escher fish image.
Note: The primitives cons-stream and amb needed in other chapters of SICP are also provided.
3 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.
4 Example
> (require sicp-pict) > (paint (number->painter 0)) > (paint diagonal-shading) > (paint-hires (below (beside diagonal-shading (rotate90 diagonal-shading)) (beside (rotate270 diagonal-shading) (rotate180 diagonal-shading)))) > (paint einstein)
5 Vectors
A mathematical vector is called a vect here, in order to avoid confusion with the builtin vectors of Scheme.
procedure
(vector-xcor v) → number?
v : vect?
procedure
(vector-ycor v) → number?
v : vect?
procedure
(vector-add v w) → vect?
v : vect? w : vect?
procedure
(vector-sub v w) → vect?
v : vect? w : vect?
procedure
(vector-scale s v) → vect?
s : number? v : vect?
6 Frames
^ |
| frame edge2 vector |
| |
_|__________> |
/| frame edge1 vector |
/ |
/ |
/ frame origin pointer |
procedure
(make-frame origin edge1 edge2) → frame?
origin : vect? edge1 : vect? edge2 : vect
procedure
(frame-origin f) → vect?
f : frame?
procedure
(frame-edge1 f) → vect?
f : frame?
procedure
(frame-edge2 f) → vect?
f : frame?
procedure
(make-relative-frame origin corner1 corner2) → (frame? -> frame?)
origin : vect? corner1 : vect? corner2 : vect?
procedure
(frame-coord-map f) → (vect? -> vect?)
f : frame?
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)
7 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.
procedure
(make-segment from to) → segment?
from : vect? to : vect?
procedure
(segment-start s) → vect?
s : segment?
procedure
(segment-end s) → vect?
s : segment?
8 Primitive Painters
Painters take a frame and draw an image, transformed to fit inside the frame.
from a constant: number->painter
from a list of line segments: segment->painter
form a procedure: procedure->painter
from a picture: picture->painter
procedure
(number->painter color) → painter?
color : 0..255
procedure
(segments->painter los) → painter?
los : list-of-segment?
procedure
(procedure->painter p) → painter?
p : procedure?
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).
procedure
(picture->painter p) → painter?
p : picture
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.
procedure
(load-painter filename) → painter?
filename : path?
9 Higher Order Painters
procedure
(transform-painter origin corner1 corner2)
→ (painter? -> painter?) origin : vect? corner1 : vect? corner2 : vect?
Transform-painter will given an origin and two corners, return a function that takes a painter as argument and returns a transformed painter.
procedure
(flip-horiz p) → painter?
p : painter
procedure
(flip-vert p) → painter?
p : painter
procedure
(rotate90 p) → painter?
p : painter
procedure
(rotate180 p) → painter?
p : painter
procedure
(rotate270 p) → painter?
p : painter
procedure
(beside p1 p2) → painter?
p1 : painter p2 : painter
procedure
(below p1 p2) → painter?
p1 : painter p2 : painter
procedure
(superpose p1 p2) → painter?
p1 : painter p2 : painter
10 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.
11 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.
procedure
(paint p) → snip?
p : painter?
procedure
(paint-hi-res p) → snip?
p : painter?
12 Authors
Abelson & Sussman: Structure and Interpretation of Computer Programs.
Daniel Coore: Original MIT Scheme code.
Mike Sperber: PLT port.
Jens Axel Søgaard: Documentation.
Javier Olaechea: Fixed amb.
13 Other
See also the readme.html from the SICP web-site for more documentation and exercises.
Peter Henderson’s "Functional Geometry".