Metapost 围绕中心而不是原点旋转标签

Metapost 围绕中心而不是原点旋转标签

目前,我正在尝试旋转一些标签,以便它们与图表中线条的方向相匹配。

例如这样: 在此处输入图片描述

该示例的问题在于标签是围绕原点旋转的,而不是围绕其自身的轴旋转的,这使其偏离了应有的位置。这是未旋转标签的相同图表:

在此处输入图片描述

如您所见,标签的位置实际上位于段的中间。通常,要按我想要的方式旋转,您需要先旋转然后平移,但我无法在移动标签之前将旋转应用于标签。

这是我的代码:

\documentclass[border=6cm]{standalone}

\usepackage{luamplib}
\mplibnumbersystem{double}
\mplibtextextlabel{enable}
\usepackage[margin=0.5cm]{geometry}

\begin{document}
{\centering
\begin{mplibcode}
u:=2cm;

vardef line(expr pfirst, psecond)=
    m := 0.07;
    pair tangent, normal, off, ofs, osf, oss, pointOne, pointTwo;
    tangent := psecond - pfirst;
    tx := xpart tangent;
    ty := ypart tangent;
    numeric len;
    len = sqrt(tx*tx + ty*ty);
    tangent := tangent * 1/len;
    normal := tangent rotated 90;

    pointOne := pfirst + tangent * 0.2;
    pointTwo := psecond - tangent * 0.2;

    off := pointOne - normal * m;
    ofs := pointTwo - normal * m;
    draw u*off--u*ofs withpen pencircle scaled 2 withcolor black;
    draw u*off--u*(off + (normal * 0.15 rotated 240)) withpen pencircle scaled 2 withcolor black;   

    osf := pointOne + normal * m;
    oss := pointTwo + normal * m;
    draw u*(osf)--u*(oss) withpen pencircle scaled 2 withcolor black;   
    draw u*oss--u*(oss + normal * 0.15 rotated 60) withpen pencircle scaled 2 withcolor black;  
enddef;

input latexmp;
% Start figure
beginfig(0);


pair vzero, vone, vtwo, vthree;  
vzero := (0, 1);
vone := (0, -1);
vtwo := (-2, 0);
vthree := (2, 0);

line(vzero, vone);
line(vzero, vtwo);
line(vtwo, vone);

line(vone, vthree);
line(vthree, vzero);
line(vone, vthree);


vzero := u*vzero;
vone := u*vone;
vtwo := u*vtwo;
vthree := u*vthree;
fill fullcircle scaled 8bp shifted vzero;
fill fullcircle scaled 8bp shifted vone;
fill fullcircle scaled 8bp shifted vtwo;
fill fullcircle scaled 8bp shifted vthree;

label.top("\huge$v_0$", vzero + (0, 0.2) * u);
label.bot("\huge$v_1$", vone + (0,-0.2) * u);
label("\huge$v_2$", vtwo + (-0.4, 0) * u);
label("\huge$v_3$", vthree + (0.4, 0) * u);

label("\huge$p_2f_1$", (vzero + vtwo) * 0.5);

endfig;
\end{mplibcode}
\par}
\end{document}

答案1

代替

label("\huge$p_2f_1$", (vzero + vtwo) * 0.5);

你可以使用这个:

labeloffset := 7bp; % necessary here to avoid overlapping the arrows
draw thelabel.top("\huge$p_2f_1$", .5[vzero,vtwo]) rotatedaround (.5[vzero,vtwo], angle(vzero-vtwo));

这使:

在此处输入图片描述

答案2

如果你经常这样做,你可能需要制作一个宏:

vardef label_along(expr your_label, target_path, time_on_path, offset) = 
    draw thelabel(your_label, origin) rotated angle direction time_on_path of target_path
        shifted (up scaled offset rotated angle direction time_on_path of target_path)
        shifted point time_on_path of target_path 
enddef;

这是使用它的图表的版本。

在此处输入图片描述

其来源如下:

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
vardef label_along(expr your_label, target_path, time_on_path, offset) = 
    draw thelabel(your_label, origin) rotated angle direction time_on_path of target_path
        shifted (up scaled offset rotated angle direction time_on_path of target_path)
        shifted point time_on_path of target_path 
enddef;

vardef connect(expr a, b) = 
    save arc, barb, alpha;
    path arc, barb; 
    arc = a -- b cutbefore fullcircle scaled 8 shifted a
                  cutafter fullcircle scaled 8 shifted b;
    numeric alpha;
    alpha = angle (b-a);
    barb = (arc -- (right scaled ahlength 
                          rotated (alpha + 180 - 1/2 ahangle) 
                          shifted point 1 of arc))
           shifted (up scaled 1 rotated alpha);
    draw barb; draw barb rotatedabout(1/2[a,b], 180);
enddef;

beginfig(1);

    z0 = -z1 = 42 up;
    z3 = -z2 = 68 right;

    dotlabel.top("$v_0$", z0);
    dotlabel.bot("$v_1$", z1);
    dotlabel.lft("$v_2$", z2);
    dotlabel.rt("$v_3$", z3);

    connect(z0, z1);
    connect(z0, z2);
    connect(z0, z3);
    connect(z1, z2);
    connect(z1, z3);

    label_along("$p_2 f_1$", z2--z0, 0.4, 8);

endfig;
\end{mplibcode}
\end{document}

用 编译lualatex

  • 这类工作中旋转的关键在于rotate你面前的物体shift。或者使用rotatedabout...

  • ahangleahlength是用于常规箭头的普通 MP 参数。

相关内容