numeric token primary
 ypart pair primary /
 numeric operator numeric primary numeric operator
numeric atom ’’ numeric variable sqrt
sind
 numeric token primary cosd
 ( numeric expression ) mlog
mexp
 normaldeviate ¬‚oor
numeric token primary ’’ numeric token / numeric token uniformdeviate
 numeric token not followed by ˜/ numeric token ™ scalar multiplication operator
numeric secondary
numeric operator ’’ sqrt  sind  cosd  mlog  mexp times or over
 floor  uniformdeviate  scalar multiplication operator *
/
scalar multiplication operator ’’ plus or minus numeric tertiary
 numeric token primary not followed by + or  or a numeric token plus or minus
numeric secondary ’’ numeric primary +

 numeric secondary times or over numeric primary Pythagorean plus or minus
times or over ’’ *  / ++
++
numeric tertiary ’’ numeric secondary numeric expression
 numeric tertiary plus or minus numeric secondary fractions
 numeric tertiary Pythagorean plus or minus numeric secondary mediation
oftheway
plus or minus ’’ +   ASCII
Pythagorean plus or minus ’’ ++  ++ xxpart
numeric expression ’’ numeric tertiary ceiling
**
All of the ¬nicky details about fractions and such things are made explicit by this
syntax. For example, we can use the rules to deduce that ˜sind1/3x2™ is interpreted
as ˜(sind((1/3x)))2™; notice that the ¬rst minus sign in this formula is considered
to be a “scalar multiplication operator,” which comes in at the primary level, while the
second one denotes subtraction and enters in the construction of numeric tertiary .
The mediation or “oftheway” operation ˜t[a, b]™ is handled at the primary level.
Several operations that haven™t been discussed yet do not appear in the syntax
above, but they ¬t into the same general pattern; for example, we will see later
that ˜ASCII string primary ™ and ˜xxpart transform primary ™ are additional cases of
the syntax for numeric primary . On the other hand, several operations that we have
discussed in this chapter do not appear in the syntax, because they are not primitives
of itself; they are de¬ned in the plain base (Appendix B). For
example, ˜ceiling™ is analogous to ˜floor™, and ˜**™ is analogous to ˜*™. Chapter 20
explains how allows extensions to its builtin syntax, so that additional
operations can be added at will.
Chapter 8: Algebraic Expressions 73
EXERCISE 8.7 pair primary
(
How does interpret ˜2 2™ ? (There™s a space between the 2™s.)
,
)
EXERCISE 8.8 (
According to expr.mf, the value of ˜1/2/3/4™ is 0.66667; the value of ˜a/2/3/4™ )
[
is 0.375a. Explain why.
,
]
The rules of pair expression are similar to those for numeric expression , so
point
it™s convenient to learn them both at the same time. of
pair secondary
pair primary ’’ pair variable *
 ( numeric expression , numeric expression ) transformer
rotated
 ( pair expression ) scaled
 numeric atom [ pair expression , pair expression ] shifted
 point numeric expression of path primary slanted
transformed
 scalar multiplication operator pair primary xscaled
pair secondary ’’ pair primary yscaled
zscaled
 pair secondary times or over numeric primary pair tertiary
 numeric secondary * pair primary pair expression
 pair secondary transformer GRIMM
¨
BRONTE
transformer ’’ rotated numeric primary
 scaled numeric primary
 shifted pair primary
 slanted numeric primary
 transformed transform primary
 xscaled numeric primary
 yscaled numeric primary
 zscaled pair primary
pair tertiary ’’ pair secondary
 pair tertiary plus or minus pair secondary
pair expression ’’ pair tertiary
EXERCISE 8.9
Try to guess the syntax rules for string primary , string secondary , string
tertiary , and string expression , based solely on the examples that have appeared in
this chapter. [Hint: The ˜&™ operation has the same precedence as ˜..™.]
A maiden was sitting there who was lovely as any picture,
nay, so beautiful that no words can express it.
” JAKOB and WILHELM GRIMM, Fairy Tales (1815)
He looked astonished at the expression.
¨
” EMILY BRONTE, Wuthering Heights (1847)
(page 74)
9
Equations
Chapter 9: Equations 75
The variables in a program receive their values by appearing in equations
mode
equations, which express relationships that the programmer wants to achieve. smoke
We™ve seen in the previous chapter that algebraic expressions provide a rich mode setup
baseline
language for dealing with both numerical and graphical relationships. Thus it is
possible to express a great variety of design objectives in precise form by stating
that certain algebraic expressions should be equal to each other.
The most important things a programmer needs to know
about equations are (1) how to translate intuitive design concepts into formal
equations, and (2) how to translate formal equations into intuitive design con
cepts. In other words, it™s important to be able to write equations, and it™s
also important to be able to read equations that you or somebody else has writ
ten. This is not nearly as di¬cult as it might seem at ¬rst. The best way to
learn (1) is to get a lot of practice with (2) and to generalize from speci¬c ex
amples. Therefore we shall begin this chapter by translating a lot of equations
into “simple English.”
Equation Translation
a = 3.14 The value of a should be 3.14.
3.14 = a The number 3.14 should be the value of a. (This
means the same thing as ˜a = 3.14™; the left and
right sides of an equation can be interchanged
without a¬ecting the meaning of that equation
in any way.)
mode = smoke The value of mode should be equal to the value
of smoke . (Plain assigns a special
meaning to ˜smoke ™, so that if mode setup is
invoked when mode = smoke the computer will
prepare “smoke proofs” as explained in Chapter 5
and Appendix H.)
y3 = 0 The y coordinate of point 3 should be zero; i.e.,
point 3 should be at the baseline. (Point 3 is
also known as z3 , which is an abbreviation for
the pair of coordinates (x3 , y3 ), if you are using
the conventions of plain .)
x9 = 0 The x coordinate of point 9 should be zero; i.e.,
point 9 should be at the left edge of the type box
that encloses the current character.
x1l = curve sidebar The x coordinate of point 1l should be equal to
the value of the variable called curve sidebar .
This puts z1l a certain distance from the left
edge of the type.
76 Chapter 9: Equations
x1 = x2 Points 1 and 2 should have the same x coordi mode setup
mm
nate; i.e., they should have the same horizontal beginchar
position, so that one will lie directly above or w
h
below the other. bounding box
d
y 4 = y5 + 1 Point 4 should be one pixel higher than point 5. distance
(However, points 4 and 5 might be far apart; this
equation says nothing about the relation between
x4 and x5 .)
y6 = y7 + 2mm Point 6 should be two millimeters higher than
point 7. (Plain ™s mode setup rou
tine sets variable mm to the number of pixels in
a millimeter, based on the resolution determined
by mode and mag .)
x4 = w ’ .01in Point 4 should be onehundredth of an inch inside
the right edge of the type. (Plain ™s
beginchar routine sets variable w equal to the
width of whatever character is currently being
drawn, expressed in pixels.)
y4 = .5h Point 4 should be halfway between the baseline
and the top of the type. (Plain ™s
beginchar sets h to the height of the current
character, in pixels.)
y6 = ’d Point 6 should be below the baseline, at the bot
tom edge of the type. (Each character has a
“bounding box” that runs from (0, h) at the up
per left and (w, h) at the upper right to (0, ’d)
and (w, ’d) at the lower left and lower right; vari
able d represents the depth of the type. The val
ues of w, h, and d might change from character
to character, since the individual pieces of type
in a computerproduced font need not have the
same size.)
y8 = .5[h, ’d] Point 8 should be halfway between the top and
bottom edges of the type.
w ’ x5 = 2 x6 The distance from point 5 to the right edge of the
3
type should be twothirds of the distance from
point 6 to the left edge of the type. (Since w
is at the right edge, w ’ x5 is the distance from
point 5 to the right edge.)
Chapter 9: Equations 77
z0 = (0, 0) Point 0 should be at the reference point of the reference point
origin
current character, i.e., it should be on the base top
line at the left edge of the type. This equation is vector
an abbreviation for two equations, ˜x0 = 0™ and
˜y0 = 0™, because an equation between pairs of
coordinates implies that the x and y coordinates
must both agree. (Incidentally, plain 
de¬nes a variable called origin whose value
is (0, 0); hence this equation could also have been
written ˜z0 = origin ™.)
z9 = (w, h) Point 9 should be at the upper right corner of the
current character™s bounding box.
top z8 = (.5w, h) If the pen that has currently been “picked up”
is placed at point 8, its top edge should be at
the top edge of the type. Furthermore, x8 should
be .5w; i.e., point 8 should be centered between
the left and right edges of the type. (Chapter 4
contains further examples of ˜top ™, as well as the
corresponding operations ˜bot ™, ˜lft ™, and ˜rt ™.)
z4 = 3 [z5 , z6 ] Point 4 should be threesevenths of the way from
7
point 5 to point 6.
z12 ’ z11 = z14 ’ z13 The vector that moves from point 11 to point 12
should be the same as the vector that moves from
point 13 to point 14. In other words, point 12
should have the same direction and distance from
point 11 as point 14 has from point 13.
z3 ’ z2 = Points 3 and 4 should be at the same distance
(z4 ’ z2 ) rotated 15 from point 2, but the direction to point 3 should
be 15 degrees counterclockwise from the direction
to point 4.
EXERCISE 9.1
(a) x7 ’ 9 = x1 ;
Translate the following equations into “simple English”:
(b) z7 = (x4 , .5[y4 , y5 ]); (c) lft z21 = rt z20 + 1.
EXERCISE 9.2
Now see if your knowledge of equation reading gives you the ability to write
equations that correspond to the following objectives: (a) Point 13 should be just
as far below the baseline as point 11 is above the baseline. (b) Point 10 should
be one millimeter to the right of, and one pixel below, point 12. (c) Point 43
should be onethird of the way from the top left corner of the type to the bottom
right corner of the type.
78 Chapter 9: Equations
Let™s return now to the six example points (z1 , z2 , z3 , z4 , z5 , z6 ) that were =
origin
used so often in Chapters 2 and 3. Changing the notation slightly, we might say
that the points are
(x1 , y1 ) = (0, h); (x2 , y2 ) = (.5w, h); (x3 , y3 ) = (w, h);
(x4 , y4 ) = (0, 0); (x5 , y5 ) = (.5w, 0); (x6 , y6 ) = (w, 0).
There are many ways to specify these points by writing a series of equations.
For example, the six equations just given would do ¬ne; or the short names z1
through z6 could be used instead of the long names (x1 , y1 ) through (x6 , y6 ).
But there are several other ways to specify those points and at the same time
to “explain” the relations they have to each other. One way is to de¬ne the x
and y coordinates separately:
x1 = x4 = 0; x2 = x5 = .5w; x3 = x6 = w;
y1 = y2 = y3 = h; y4 = y5 = y6 = 0.
allows you to state several equations at once, by using more than
one equality sign; for example, ˜y1 = y2 = y3 = h™ stands for three equations,
˜y1 = y2 ™, ˜y2 = y3 ™, and ˜y3 = h™.
In order to de¬ne the coordinates of six points, it™s necessary to write
twelve equations, because each equation contributes to the de¬nition of one value,
and because six points have twelve coordinates in all. However, an equation
between pairs of coordinates counts as two equations between single numbers;
that™s why we were able to get by with only six ˜=™ signs in the ¬rst set of
equations, while twelve were used in the second.
Let™s look at yet another way to specify those six points, by giving
equations for their positions relative to each other:
z1 ’ z 4 = z 2 ’ z 5 = z 3 ’ z 6
z2 ’ z 1 = z3 ’ z 2 = z5 ’ z 4 = z6 ’ z 5
z4 = origin ; z3 = (w, h).
First we say that the vectors from z4 to z1 , from z5 to z2 , and from z6 to z3 , are
equal to each other; then we say the same thing for the vectors from z1 to z2 ,
z2 to z3 , z4 to z5 , and z5 to z6 . Finally the corner points z4 and z3 are given
explicitly. That™s a total of seven equations between pairs of coordinates, so it
should be more than enough to de¬ne the six points of interest.
However, it turns out that those seven equations are not enough! For
example, the six points
z1 = z4 = (0, 0); z2 = z5 = (.5w, .5h); z3 = z6 = (w, h)
also satisfy the same equations. A closer look explains why: The two formulas
z1 ’ z 4 = z2 ’ z 5 z 2 ’ z 1 = z5 ’ z 4
and
actually say exactly the same thing. (Add z5 ’ z1 to both sides of the ¬rst
equation and you get ˜z5 ’ z4 = z2 ’ z1 ™.) Similarly, z2 ’ z5 = z3 ’ z6 is the
Chapter 9: Equations 79
same as z3 ’ z2 = z6 ’ z5 . Two of the seven equations give no new information, known
unknown
so we really have speci¬ed only ¬ve equations; that isn™t enough. An additional
relation such as ˜z1 = (0, h)™ is needed to make the solution unique.
EXERCISE 9.3
(For mathematicians.) Find a solution to the seven equations such that
z1 = z2 . Also ¬nd another solution in which z1 = z6 .
At the beginning of a program, variables have no values,
except that plain has assigned special values to variables like smoke
and origin . Furthermore, when you begin a new character with beginchar, any
previous values that may have been assigned to x or y variables are obliterated
and forgotten. Values are gradually established as the computer reads equations
and tries to solve them, together with any other equations that have already
appeared in the program.
It takes ten equations to de¬ne the values of ten variables. If you have
given only nine equations it may turn out that none of the ten variables has yet
been determined; for example, the nine equations
g0 = g1 = g2 = g3 = g4 = g5 = g6 = g7 = g8 = g9
don™t tell us any of the g values. However, the further equation
g 0 + g1 = 1
to deduce that all ten of the g™s are equal to 1 .
will cause 2
always computes the values of as many variables as possible,
based on the equations it has seen so far. For example, after the two equations
a + b + 2c = 3;
a ’ b ’ 2c = 1
the machine will know that a = 2 (because the sum of these two equations is
˜2a = 4™); but all it will know about b and c is that b + 2c = 1.
At any point in a program a variable is said to be either “known” or
“unknown,” depending on whether or not its value can be deduced uniquely
from the equations that have been stated so far. The sample expressions in
Chapter 8 indicate that can compute a variety of things with un
known variables; but sometimes a quantity must be known before it can be used.
For example, can multiply an unknown numeric or pair variable by
a known numeric value, but it cannot multiply two unknowns.
Equations can be given in any order, except that you might sometimes
need to put certain equations ¬rst in order to make critical values known in the
will ¬nd the solution (a, b, c) = (2, 7, ’3) to
others. For example,
the equations ˜a + b + 2c = 3; a ’ b ’ 2c = 1; b + c = 4™ if you give those equations
in any other order, like ˜b + c = 4; a ’ b ’ 2c = 1; a + b + 2c = 3™. But if the
equations had been ˜a + b + 2c = 3; a ’ b ’ 2c = 1; a — (b + c) = 8™, you would not
have been able to give the last one ¬rst, because would have refused
80 Chapter 9: Equations
to multiply the unknown quantity a by another unknown quantity b + c. Here top
bot
are the main things that can do with unknown quantities: lft
rt
’ unknown comparison
tracingequations
unknown + unknown
tracingonline
unknown ’ unknown hash hash
unknown — known
known — unknown
unknown / known
known [ unknown , unknown ]
unknown [ known , known ]
Some of the operations of plain , de¬ned in Appendix B, also work
with unknown quantities. For example, it™s possible to say top unknown ,
bot unknown , lft unknown , rt unknown , and even
penpos su¬x ( unknown , known ).
program can say ˜ unknown [a, b]™ when a ’ b is known, and
A
variable a can be compared to variable b in boolean expressions like ˜a < b™
when a ’ b is known. The quantity a ’ b might be known even when a and b aren™t
known by themselves.
You might wonder how is able to keep its knowledge uptodate,
based on scraps of partial information that it receives from miscellaneous
equations. The best way to understand this is to watch how it happens, by asking the
computer to show certain calculations that it usually keeps to itself. Here™s one way to
do it: Run and say
\tracingequations:=tracingonline:=1;
in response to the opening ˜**™. (Be sure to type the backslash ˜\™, and to use ˜:=™
instead of ˜=™. We will see in Chapter 27 that can be asked to “trace”
many aspects of what it™s doing.) Now type
a+b+2c=3;
the machine will reply by saying
## c=0.5b0.5a+1.5
since that is how it has digested your equation. (The ˜##™ in this line identi¬es diag
nostic information that comes from tracingequations .) Now type
ab2c=1;
will read this as if you had said ˜ab2(0.5b0.5a+1.5)=1™, since it has
previously learned how to replace c by an expression that involves only a and b. This
new equation can be simpli¬ed by multiplying out the lefthand side and collecting
terms. The result is ˜2a3=1™, hence will respond with
## a=2
Chapter 9: Equations 81
and it will be your turn to type something again. Say showdependencies
hash hash hash hash
dependent
showdependencies;
independent
known
™s response will be
c=0.5b+0.5
indicating that there is only one variable whose value depends on others, and that its
equation of dependency is now ˜c = ’0.5b + 0.5™. (The previous dependency equation
˜c = ’0.5b ’ 0.5a + 1.5™ has been simpli¬ed to take account of the newly discovered
value, a = 2.) Finally type
b+c=4;
this spurs the computer on to say