如何使用 MetaPost 创建波浪线?

如何使用 MetaPost 创建波浪线?

我正在寻找一个宏或任何可以用 MetaPost 绘制波浪曲线的代码。

换句话说,我需要下面的波浪/弹簧版本:

beginfig(1);
z1 = (0,0);
z2 = (3,2);
z3 = (6,0);
draw z1 .. z2 .. z3;
endfig;

这将产生一条从z1到 的曲线z3。我需要的是如何使这条曲线呈波浪形?

我需要的是一个带有弹簧的曲线,像下面的弹簧一样,但是是弯曲的......

在此处输入图片描述

答案1

也许你应该首先指定路径节点处要遵循的方向,例如:

u := 1cm;
beginfig(1);
z1 = (0,0);
z2 = (3,2)*u;
z3 = (6,0)*u;
for i=1,2,3: draw z[i] withpen pencircle scaled 3bp; endfor;
draw z1{up} .. z2{dir -45} .. z3{dir 45};
setbounds currentpicture to boundingbox currentpicture enlarged 10bp;
endfig;
end.

在此处输入图片描述

一旦它满足您的需要,您就可以将其放入宏中。

更新metaobj 包可能正是您所需要的。请参阅其文档

input metaobj;
u := 1cm;
beginfig(1);
z1 = (0,0);
z2 = (3,2)*u;
z3 = (6,0)*u;
nczigzag(z1)(z2), "angleA(90)", "angleB(-45)", "arrows(-)", "coilwidth(0.2u)",
    "coilarmA(0)", "coilarmB(0)";
nczigzag(z2)(z3), "angleA(-45)", "angleB(45)", "arrows(-)", "coilwidth(0.2u)",
    "coilarmA(0)", "coilarmB(0)";
endfig;
end.

在此处输入图片描述

答案2

如果你不想学习全部元对象只是为了做之字形或摆动,那么你可能会喜欢这两个基于arctime命令的功能。它们可以沿着几乎任何路径绘制“摆动”或“之字形”。我试图让它们保持简单,但它们远非完美,所以任何改进建议都会受到欢迎。

prologues := 3;
outputtemplate := "%j%c.eps";

% Be reasonable with these lengths.  If you use `drawarrow` then 
% you should ensure that s_lambda > ahlength

s_lambda = 6; % the length of the waves
s_angle = 45; % the angle of attack 

vardef sinuous expr p = 
   save ut; hide( ut := arctime s_lambda of p;) 
   point 0 of p {curl 0} ..
      for i=1 step 2 until floor(length p / ut)-2:
            point     i*ut of p { (direction     i*ut of p) rotated +s_angle } ..
            point (i+1)*ut of p { (direction (i+1)*ut of p) rotated -s_angle } ..
          endfor
          { direction length p of p } point length p of p
   enddef;

z_lambda = 6; % the length of the zig-zags
z_amp   = 2.4; % the amplitude of each zig and zag

vardef ziggy expr p = 
   save ut; hide( ut := arctime z_lambda of p;) 
   point 0 of p { direction 0 of p } ..
      for i=2 step 2 until floor(length p / ut)-3:
            point     i*ut of p + z_amp*up   rotated angle direction     i*ut of p --
            point (i+1)*ut of p + z_amp*down rotated angle direction (i+1)*ut of p --
          endfor
          point length p - ut of p -- point length p of p
   enddef;

% example of usage
beginfig(1);
  path p ;
  p := origin {dir 60} .. {down} (210,20);
  drawarrow sinuous p shifted 20up withcolor red;
  drawarrow ziggy   p shifted 10up withcolor blue;
  drawarrow p; 
endfig;
end.

在此处输入图片描述

OP 图可以像这样完成:

beginfig(2);
  z1 = (0,0);
  z2 = (3cm,2cm);
  z3 = (6cm,0);
  z_amp := 1mm;
  z_lambda := .8mm;
  draw ziggy z1 .. z2 .. z3;
  fill fullcircle scaled 4 shifted z1;
  fill fullcircle scaled 4 shifted z3;
endfig;

在此处输入图片描述

相关内容