Linear Equations and Intersections
Defining points by linear equations
In METAPOST, points and paths may be described by a set of linear equations that are solved by the program.
Assume you want to draw a simple rectangle. Then you know it consists of four corners (e. g. with the lower left one in the origin), that we will describe by the following equations:
path p[]; z0 = origin; x0 = x3; x1 = x2; y1 = y0; y3 = y2; x1-x0 = 3cm; y3-y0 = 2cm; p0 = z0--z1--z2--z3--cycle; fill p0 withcolor blue; draw p0 withpen pencircle scaled 1bp;
You see, all corners except for z0 aren't defined directly as (x, y) pairs but described by their relations. While describing a rectangle with linear equations seems rather like overkill, this METAPOST feature becomes really powerful for the construction of complex paths.
Intersection points
Finding the intersection points of paths is another nice METAPOST feature.
Assume you have a triangle. Mathematical theory says that if you draw three lines, each of them from one corner of the triangle to the midpoint of the opposite side, all these lines will intersect at the same point.
The following code shows how this can be demonstrated in a METAPOST drawing:
pickup pencircle scaled 1bp; path p[]; z0 = origin; z1 - z0 = 3cm*right; z2 - z0 = 2.7cm*dir(40); p0 = z0--z1--z2--cycle; p1 = .5[z0,z1]--z2; p2 = .5[z1,z2]--z0; p3 = .5[z2,z0]--z1; draw p1 withcolor blue; draw p2 withcolor blue; draw p3 withcolor blue; draw p1 intersectionpoint p2 withpen pencircle scaled 3bp; draw p0;
This code is more straightforward than it may appear. It consists of three parts.
First, the three points z0 . . . z2 are defined and path p0 is defined as the triangle with these points as corners.
Second, the paths p1 . . . p3 are defined. Each consists of a line from one corner to the midpoint of the opposite side, named a median of the triangle. This may be easily expressed in METAPOST, since e. g. the statement .5[z1,z2] is just the point on halfway along the line from z1 to z2.
Finally, after drawing all the paths defined above, we mark the intersection point of p1 and p2. This is directly given by the command
p1 intersectionpoint p2
It may be a bit more complicated if two paths have more than one intersection point.
Whatever it is . . .
Coming back to the triangle in the example above, another interesting task is the following: draw the altitude of the triangle, that is a line perpendicular to the base line through the opposite vertex z2.
Thus, the altitude line has to fulfill the following conditions:
- It is orthogonal to the base line (connection of z0 and z1).
- The starting point is in z2, the end point shall be on the base line.
This may be directly expressed in METAPOST:
z10-z2= whatever*((z1-z0) rotated 90); z10 = whatever[z0,z1];
In the code above the end point of the altitude on the base line is named z10.
Here we see both conditions listed before: first, the distance vector between z10 and z2 is given by the distance vector between z1 and z0 (i. e. the base line), rotated by 90 degrees, scaled by an arbitrary factor.
Second, z10 is located somewhere on the line defined by the points z0 and z1.
METAPOST finds the correct position for z10 as starting point of a perpendicular line to the base line, with z2 as end point.