I was given a homework assignment in one of my courses that asked us to google the Metapost language and find the use for the equation solving feature in the language. After going through the first dozen or so pages of the Metapost user manual I found only one reason as to why it's useful and it's that "allows many programs to be written in a largely declarative style." Besides stating that it makes the programming more "declarative" (which from what I understood means that we tell the language what to do as opposed to how to do it) I couldn't think of any other reason why the equations solving is useful. Can anyone help me out?
Here is an illustration of how solving equations in MetaPost — and declarative programming, for that matter — might be useful.
Suppose we want to draw a die:
To do that, let us first define a macro which will draw a single face of the die: a square with number
son it.Now we can draw it and get the picture:
Next, we need an upper face and a right face. To draw them, we will have to compose an affine transformation to skew them. This can be tricky since the only readily available primitive transformation for skewing is
slanted awhich transforms a point(x, y)into(x + ay, y). Here is our picture slanted 1:We will then (or rather, before that) have to scale by one of the coordinates:
The same approach does not work for the third face right away:
After a bit of experimentation, we find the right code:
But why all the tedium? We know exactly what transformation we need. One natural way to express it is by using primitives. But if that proves unintuitive, as our last line was, it may be more comfortable to just specify which points of the plane transform to which.
This basically tells MetaPost: there is a transform
t, under which the three points we specified move to the other three points we specified. Turns out this uniquely determines a plane transformation, and we get the same picture:Putting all that together (the code is
beginfig (7)at end of post) allows us to finally see our die:In this simple case, the “coordinates and equations” approach is comparable in difficulty to the “primitive transformations” approach. Now, imagine we wanted a slight tilt for our cube. With the same declarative approach, it would be still possible without invoking three-dimensional geometry (the code is
beginfig (8)at end of post):The complete program is below.