Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Applications

Drawing with pict

Create graphics as values in Racket with the pict and 2htdp/image libraries: shapes, hc-append, vc-append, recursion for fractals, and a runnable SVG-generating JavaScript stand-in.

Most drawing libraries are imperative: you call moveTo, lineTo, fill and mutate a canvas. Racket's picture libraries take a different path. A picture is a value. You build small pictures, combine them with functions like hc-append or overlay, and get a new picture back. Nothing is mutated, so drawing becomes ordinary function composition, and the techniques from earlier chapters (recursion, higher-order functions) apply directly.

This lesson shows the idea with the pict library (used by Slideshow) and 2htdp/image (used in How to Design Programs), then runs a JavaScript stand-in that builds pictures as data and renders them to SVG text you can inspect. The practice problems in Recursion Practice include a picture exercise.

Racket's graphics libraries need a window system, so the Racket code is read-only, and I show no images for it. The JavaScript block is a stand-in: it generates SVG source text, so you can see the structure without a canvas.

Pictures as values

With pict:

RACKET
#lang racket
(require pict)

(define dot (disk 20))
(define frame (rectangle 30 30))
(define row (hc-append 10 dot frame dot))   ; horizontal, 10 pixels apart
(define column (vc-append 5 row row))       ; vertical
column                                      ; displays as an image in DrRacket

Constructors such as disk, circle, rectangle, text, filled-rectangle make basic pictures. Combinators such as hc-append, vc-append, ht-append, cc-superimpose and lt-superimpose place pictures next to or on top of each other, with an alignment letter in the name (h, v direction; l, c, r, t, b alignment). colorize and scale change a picture, and pin-over places one picture at a point relative to another.

RACKET
(require 2htdp/image)

(overlay (circle 30 "solid" "orange")
         (square 80 "solid" "navy"))
(beside (triangle 40 "solid" "red")
        (star 40 "solid" "gold"))

2htdp/image uses a slightly different vocabulary, circle with mode and color, above, beside, overlay, rotate, but the idea is identical. Both libraries show their results directly in the DrRacket REPL, because a picture is a value that knows how to print itself.

Recursion makes fractals

Because a picture can be an argument, recursive drawing is concise. A Sierpinski triangle in 2htdp/image:

RACKET
(define (sierpinski n size)
  (if (zero? n)
      (triangle size "solid" "darkgreen")
      (let ([t (sierpinski (- n 1) (/ size 2))])
        (above t (beside t t)))))

(sierpinski 4 200)

Level n is built from three copies of level n-1. There is no loop over pixels, no coordinates, and the picture's size is handled by the library. A tree, a spiral, or a checkerboard works the same way: define the base picture, then define a bigger one in terms of smaller ones.

flowchart TD
  L0["level 0: one triangle"] --> L1["level 1: above t, beside t t"]
  L1 --> L2["level 2: three copies of level 1"]
  L2 --> L3["level 3: three copies of level 2"]

Each level is the previous level used three times.

Stand-in: pictures as data, rendered to SVG

In JavaScript we can mimic this design. A picture is an object with a width, a height and a list of SVG elements positioned at the origin. beside and above combine two pictures by shifting the second. The render function prints SVG source text. The core point carries over: pictures are values and combinators return new pictures.

JS Stand-in: picture combinators that generate SVG

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Copy any of the printed SVG into an .svg file to view it. Notice that pattern(2, 10) is 40 by 40 pixels and produces ten SVG parts (two level-1 blocks of four squares each, plus two gold squares): sizes and counts follow from the recursion, without the programmer computing a single coordinate.

Animation and interactive programs

The 2htdp/universe library extends this idea to animation and games with big-bang. You supply a state, a function to-draw from state to picture, an on-tick function that returns the next state, and handlers for keys and the mouse:

RACKET
(require 2htdp/universe 2htdp/image)

(define (draw-world x) (place-image (circle 10 "solid" "red") x 50 (empty-scene 200 100)))
(big-bang 0
  [on-tick add1]
  [to-draw draw-world]
  [stop-when (lambda (x) (> x 200))])

This is a functional-reactive style: the program is a set of pure functions, and big-bang owns the loop. It makes games with no mutation, easy to test by calling draw-world directly and comparing images with equal?.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does it mean that a pict is a "value"?

    Pictures are ordinary data, so combinators return new pictures instead of mutating a canvas.
  2. 2. What does (hc-append 10 a b) do?

    hc-append puts pictures in a horizontal row, centered vertically, with the given spacing.
  3. 3. How is a Sierpinski triangle defined recursively?

    The base case is a triangle; each higher level combines three smaller copies with above and beside.
  4. 4. In big-bang, what does to-draw do?

    The state is plain data; to-draw renders it, and on-tick or key handlers create the next state.

Summary

  • In pict and 2htdp/image, pictures are immutable values combined with functions like hc-append, beside and overlay.
  • Recursion plus combinators draws fractals and patterns without coordinates.
  • big-bang builds interactive programs from a state, a draw function and update handlers.
  • The JavaScript stand-in shows the same design by generating SVG text.

Next: Project: A Mini Scheme Interpreter