Metapost:如何放置宏的多个实例

Metapost:如何放置宏的多个实例

我编写了一个绘制笑脸的 Metapost 宏。我想在第一个实例旁边放置另一个实例,可能稍大一些(大小是一个参数)。我如何放置多个单独的实例?

以下是我目前所掌握的信息:

prologues := 3;
outputtemplate := "%j-%c.png";
outputformat := "png";

def face(expr x) =
  begingroup
    u := 8bp;
    p := 3u;
    d := x*u;
    r := 0.3*d;
    pickup pencircle scaled p;
    fill fullcircle scaled d withcolor black;
    draw fullcircle scaled d withcolor white;
    drawdot (0,r) rotated -35 withcolor white;
    drawdot (0,r) rotated 35 withcolor white;
    draw (0,r) rotated 110 .. (0,r) rotated 180 ..
      (0,r) rotated -110 withcolor white;
  endgroup
enddef;

beginfig(1)
  face(50)
endfig;
end

答案1

您可以使用将另一幅图包装face到临时图片中image( ... ),然后移动和缩放整个图片。

您可能还想将所有内容全部缩小一点,因为您的单位已经接近算术溢出。

prologues := 3;
outputtemplate := "%j-%c.png";
outputformat := "png";

def face(expr x) =
  begingroup
    u := 8bp;
    p := 3u;
    d := x*u;
    r := 0.3*d;
    pickup pencircle scaled p;
    fill fullcircle scaled d withcolor black;
    draw fullcircle scaled d withcolor white;
    drawdot (0,r) rotated -35 withcolor white;
    drawdot (0,r) rotated 35 withcolor white;
    draw (0,r) rotated 110 .. (0,r) rotated 180 ..
      (0,r) rotated -110 withcolor white;
  endgroup
enddef;

beginfig(1)
  face(50);
  draw image( face(50); ) scaled (1.2) shifted (500bp,0) ;
endfig;
end

在此处输入图片描述

答案2

image这是带有宏中的运算符的版本...

在此处输入图片描述

prologues := 3;
outputtemplate := "%j-%c.png";
outputformat := "png";

vardef face(expr diameter) = image(
    save p; pair p; p = up scaled 0.3 diameter;
    fill fullcircle scaled diameter withcolor black;
    draw fullcircle scaled diameter withcolor white;
    drawdot p rotated -35 withcolor white;
    drawdot p rotated 35 withcolor white;
    draw p rotated  110 
      .. p rotated  180 
      .. p rotated -110 withcolor white;
    ) 
enddef;

beginfig(1)
    pickup pencircle scaled 20;
    draw face(400) rotated 20 shifted 200 left;
    pickup pencircle scaled 10;
    draw face(200) scaled 1.618 shifted 200 right;
endfig;
end

笔记

  • vardef类似于def但会自动为您添加begingroupendgroup

  • 如果您希望恢复的值p,则endgroup需要save p;在组的开头进行。

  • 明确定义局部变量是一种很好的做法,而不是使用:=

  • 在这里定义一对比一直写 更容易(0, r)。我个人觉得使用它up比记住(0, 1)意味着 up 更容​​易...

  • 您可以将任何翻译应用于,picture并将其传递给drawlabel放在页面上

  • 但不要尝试draw face(200) withcolor red;——或者更确切地说尝试一下,你会发现这可能不是一个好主意。

  • 你也可以考虑这样做

    picture Smiler;
    Smiler = face(360);
    draw Smiler shifted 40 right;
    draw Smiler shifted 100 right rotated 90;
    % etc
    

相关内容