![Metapost:如何放置宏的多个实例](https://linux22.com/image/404854/Metapost%EF%BC%9A%E5%A6%82%E4%BD%95%E6%94%BE%E7%BD%AE%E5%AE%8F%E7%9A%84%E5%A4%9A%E4%B8%AA%E5%AE%9E%E4%BE%8B.png)
我编写了一个绘制笑脸的 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
但会自动为您添加begingroup
和endgroup
。如果您希望恢复的值
p
,则endgroup
需要save p;
在组的开头进行。明确定义局部变量是一种很好的做法,而不是使用
:=
在这里定义一对比一直写 更容易
(0, r)
。我个人觉得使用它up
比记住(0, 1)
意味着 up 更容易...您可以将任何翻译应用于,
picture
并将其传递给draw
或label
放在页面上但不要尝试
draw face(200) withcolor red;
——或者更确切地说做尝试一下,你会发现这可能不是一个好主意。你也可以考虑这样做
picture Smiler; Smiler = face(360); draw Smiler shifted 40 right; draw Smiler shifted 100 right rotated 90; % etc