4

I've read through most of the Metafun manual, but I can't seem to find any mention of a relative positioning system. I have two use cases in mind. First, I would like to position points along a path in relation to previous points similar to the ++ notation in tikz. Second, I would like to position complex shapes a certain distance away from each other.

I don't know how to approach the first case with Metafun, but here is a working example (in ConTeXt) for a line drawn by successive additive increments in tikz:

%&context
\usemodule[tikz]
\starttext

\starttikzpicture
  \path[draw, black] (0cm, 0cm) -- ++(1cm, 0cm) -- ++(0cm, 1cm);
\stoptikzpicture

% vs. absolute positioning in Metafun

\startMPcode
  draw (0cm, 0cm) -- (1cm, 0cm) -- (1cm, 1cm);
\stopMPcode

\stoptext

The second case could be tackled with xshifted for the simplest cases, but it becomes necessary to make multiple edits if the dimensions of a path/picture change.

%&context 
\starttext

\startMPcode
  path P;  
  % Works with one set of sizes
  P = fullcircle;
  draw P scaled 1cm;
  draw P scaled 1cm xshifted 2cm;
\stopMPcode

\startMPcode
  path P;
  % xshift distance must be manually adapted with size changes
  P = fullcircle;
  draw P scaled 2cm;
  draw P scaled 1cm xshifted 1cm;
\stopMPcode

\stoptext

I found relative positioning examples in commands like label.rt and I'm aware of the boxes package, but I am looking for a more general approach if it exists -- like setting the distance between arbitrary points along a path rather than defining bounding boxes.

  • There is no relative position function in the underlying Metapost language. There is no reason why you could not create one. Indeed there have been several attempts in the past. For example the augment command that you get in mpgraph. But ++ is already used in MP for the Pythagorean addition operator, so you'd need to redefine it or find some other characters. – Thruston Sep 1 '17 at 17:51
  • I don't understand what you mean by "but it becomes necessary to make multiple edits if the dimensions of a path/picture change or set up a number of calculations if the shapes are more complex." The complexity of a particular path makes no difference to the transformation you then apply to it. You can apply any general transformation: shifted, rotated, scaled, slanted, etc.... – Thruston Sep 1 '17 at 18:21
  • Do you have access to a copy of Knuth's Metafont Book? that's probably the best source for understanding the construction of paths and application of transformations to them. – Thruston Sep 1 '17 at 18:24
  • @Thruston I'll remove the mention to complexity as it really is too vague. Is the use of variables the only way to keep two shapes x distance apart while their size changes? And thanks for the book suggestion, I'll look into it when I have the time. – ssokolen Sep 1 '17 at 18:54
  • 2
    that's probably a separate question, but the whole point of the declarative equations is that you can define a = 2b so that a is twice whatever b is... – Thruston Sep 1 '17 at 19:08
7

In my view, MetaPost is a more declarative language than TikZ. So, if relative coordinates were needed, I'd simply define them:

\starttext

\startMPpage[offset=1mm]
  z0 = origin;
  z1 = z0 + (1cm, 0cm);
  z2 = z1 + (0cm, 1cm);

  draw z0 -- z1 -- z2;
\stopMPpage

\stoptext

It is possible to assign variables along the path, but it is not pretty!

\starttext
\startMPpage[offset=1mm]
  z0 = origin;

  draw z0 -- hide (z1 = z0 + (1cm,0cm);) z1
       -- hide (z2 = z1 + (0cm, 1cm);) z2;
\stopMPpage
\stoptext

However, it is relatively straight-forward to wrap this behind a macro and get a nicer interface:

\starttext
\startMPdefinitions
  pair _interpolated_point;
  primarydef a --++ b = 
      a -- hide (_interpolated_point := a + b;)
      _interpolated_point
  enddef;
\stopMPdefinitions

\startMPpage[offset=1mm]
  draw origin --++ (1cm, 0) --++ (0, 1cm);
\stopMPpage

\stoptext

I am not really sure what is being asked in the second question.

  • 1
    Thanks, that's a very elegant solution! The second question came down to defining fixed distances between nodes that are part of different paths, but I can figure that part out based on the information that you provided. – ssokolen Sep 5 '17 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.