The metapict library provides functions and data structures useful
for generating picts. The library includes support for points, vectors, Bezier curves,
and, general curves.
The algorithm used to calculate a nice curves based on points and tangents is the
same as the one used in MetaPost.
With this library I to hope narrow the gap between Scribble and LaTeX + MetaPost/Tikz.
If you find any features in MetaPost or Tikz that you like includes, don’t hessitate
to mail me.
2Guide
3Coordinates
3.1Points
In order to make a computer draw a shape, a way to specify the key points of
the shape is needed.
Note: This is different from racket/pict
which reverses the direction of the y-axis.
MetaPict uses standard (x,y)-coordinates for this purpose.
The location of a point is always relative to the reference point (0,0).
The x-coordinate of a point is the number of units to the right of the reference point.
The y-coordinate of a point is the number of units upward from the reference point.
Consider these points:
The coordinates of these points are:
p1=(0,100)
p2=(100,100)
p3=(200,100)
p4=(0,0)
p5=(100,0)
p6=(200,0)
Notice that the point p4=(0,0) is the reference point.
The point p3=(200,100) is located 200 units to the right of p4
and 100 units upwards.
In order to write a MetaPict program to draw a shape, a good strategy is
to draw the shape on paper. Determine the coordinates for the key points, and
then write the MetaPict program that draws lines or curves between the points.
Let us write such a program, that connects point p1 and p6.
> (with-window(window-10210-5105)
(draw(curve(pt0100)..(pt2000))))
The .. between the two points connects the two points with a line.
If we are to use the points repeatedly, it is better give them names.
Let us connect the point p2 with p5 and p3 with p4.
> (with-window(window-10210-5105)
(draw(curvep1..p6)
(curvep2..p5)
(curvep3..p4)))
If you zoom, you will see that the lines have a thickness and
that the ends are rounded. Imagine that you have a pen with
a circular nib. The drawings produced by MetaPict will try
to mimick the result you get by drawing with such a pen.
In the chapter on pens you will learn to the control the
thickness of the pen and the shape of the ends of lines.
3.2Displacements
In the example above the point p2=(100,100) was described as being 100 to
the right and 100 upwards relative to the reference point (0,0).
An alternative way of describing the location of p2 would be to say
that is located 100 to the right of p1 (and 0 upwards).
Such a displacement can be described with a vector. Since Racket uses the name
"vector", we will represent displacement vectors with a vec structure.
To displace a point p with a vector v, use pt+.
The displacements left, right, up, and, down.
are predefined. As are the vector operations vec+,vec-, and, vec*.
The displacement that moves a point a to point b is given by (pt-ba).
It is common to need points that lies between two point A and B.
The mediation operation is called med. The call (med0.25AB)
will compute the point M on the line from A to B whose
distance from A is 25% of the length of AB.
Note: (medxAB) is equivalent to (pt+A(vec*x(pt-BA))).
Let us the knowledge from this section to write a small program to generate
the character A. The shape depends on the parameters w (width),
h (height) and the placement of the bar α.