Metapost,旋转功能不起作用

Metapost,旋转功能不起作用

我正在尝试按如下角度旋转矢量:

u:=2cm;

vardef rotate(expr p, t)=
    x := xpart(p);
    y := ypart(p);

    (cosd(t)*x - sind(t)*y, sind(t)*x + cosd(t)*y)
enddef;

vardef line(expr pfirst, psecond)=
    m := 0.2;
    pair tangent, rv;
    tangent := psecond - pfirst;
    tx := xpart tangent;
    ty := ypart tangent;
    tangent := (-ty, tx);
    numeric len;
    len = sqrt(tx*tx + ty*ty);
    tangent := tangent * 1/len;

    rv := rotate(tangent, 0.01);

    draw u*pfirst--u*psecond withpen pencircle scaled 1 withcolor black;
    draw u*pfirst--u*(pfirst + rv * 0.15) withpen pencircle scaled 1 withcolor black;   

    draw u*(pfirst + tangent * m)--u*(psecond + tangent * m) withpen pencircle scaled 1 withcolor black;    
enddef;

然而输出是: 在此处输入图片描述

这是一个 90 度角,这根本不是我想要计算的,我传递给宏的任何其他值看起来都与此完全一样。结果应如下图所示: 在此处输入图片描述

答案1

这是我的尝试。我充分利用了rotatedabout这两种方法,因为效果很好,而且旋转也引出了你的问题。除了以下方法,我想不出更好的方法来获得箭头:

  • 取连接节点中心的线,并切掉节点周围(未绘制的)圆圈内的部分,以便在节点和箭头之间留出一些分离。
  • 采取结果线并将其垂直于其方向移动一定距离,以使前向鱼叉和后向鱼叉分离。
  • 将此线绕其终点旋转一定角度并向后移动一段距离以获得鱼叉尖。
  • 围绕连接节点中心的线的中点旋转结果,以获得另一个方向的鱼叉

在此处输入图片描述

\documentclass[border=5mm]{standalone}

\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{mplibcode}
u:=2cm;

vardef harpoons(expr p,q,s,t)=
    save a,k; path a[]; clearxy;
    ang:=25; % ang = harpoon angle
    k:=.07;  % k= length of harpoon
    a0=fullcircle scaled s; % s = separation btw harpoon tip and node
    a1=(p--q) cutbefore (a0 shifted p) cutafter (a0 shifted q); % chop path btw nodes at circles
    z0=dir(angle(q-p)+90); % z0 = unitvector perpendicular to line through p and q
    a2=a1 shifted (t*z0); % shift p--q perp dist along z0
    % a3 = harpoon tip: rotate a2 about endpoint through ang, then move some dist along this line
    a3=a2--k[point 1 of a2,point 0 of a2 rotatedabout(point 1 of a2,-ang)];
    draw a3 withpen pencircle scaled 1bp;
    % once we have one harpoon, the other is just a rotation of it
    draw a3 rotatedabout(point .5 of a1,180) withpen pencircle scaled 1bp;
enddef;

% nsep=sep between node and harpoon, nsize= node size, psep=separation between harpoons
vardef dmnd(expr p,q,nsep,nsize,psep)=
  clearxy;
  z0=p; z1=q;
  z2=p reflectedabout (q,q+up); % reflect p about vertical line through q
  z3=q reflectedabout (p,p+right); % reflect q about horizontal line through p
  for i=0 upto 3:
    harpoons(z[i],z[(i+1) mod 4],nsep,psep);
    draw z[i] withpen pencircle scaled nsize;
  endfor;
harpoons(z1,z3,nsep,psep);
enddef;

beginfig(0);
    dmnd(origin,(2u,u),.25u,.1u,.04u);
endfig;
\end{mplibcode}
\end{document}

相关内容