CS 131 pic Tutorial
Introduction

pic is a domain-specific language for describing simple shape-and-line diagrams.

The following video shows a very simple pic diagram being drawn. (The green arrow indicates pic's current position and direction on the page.)

In general, a pic program is a sequence of elements. An element can be a 2D shape such as box or circle, or a linear shape such as arrow or line (or move, which is like an invisible line), or a command to change the current direction such as left or right or up or down.

Direction commands

The commands right, left, up, and down can be used to change the current direction

These are absolute directions, so left means “toward the left-hand-edge of the page ” and not “turn 90° counterclockwise.”

Note that boxes have the same width and height, whether they are being drawn to the right of a current position or below a current position.

(While you are watching all these videos, you should predict what is going to happen at each step, and then check whether you were correct.)

Direction attributes

By default, linear shapes such as arrows move one unit in the current direction.

You can override this behavior by appending direction attributes to the arrow command; the far end of the arrow is determined by summing these direction vectors (and ignoring the default direction):

If a linear shape has any direction attributes, then the last one becomes the new default direction. Make sure you understand the difference between the following two examples!

2D shapes (boxes and circles) should not have direction attributes, but if there are any, they should be completely ignored. In particular, they do not change the current default direction.

Label attributes

Any shape can have one or more string attributes. Each string becomes a separate line; the lines are centered at the middle of the shape.

Linear shapes such as arrows can have both direction and string attributes:

Interleaving of string and direction attributes doesn't matter; we could also have said arrow right down "step 3" down.

Other linear shapes

In addition to arrow, you can use a line (an arrow without the arrowhead), or a move (an arrow without the arrowhead and without the line):