我需要编辑此代码,以便红色虚线可以结束于 R4 而不是 R3,如下图所示。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,calc,arrows.meta}
\begin{document}
\begin{tikzpicture}[bullet/.style={draw,circle,minimum width=3mm,inner
sep=0pt,
fill=cyan!50}]
\node[bullet,label={[yshift=-2mm]below:$R_1$}] (R1){};
\node[right=2cm of R1,bullet,label={[yshift=-2mm]below:$R_2$}] (R2){};
\node[right=2cm of R2,yshift=6mm,bullet,label={[yshift=2mm]above:$R_3$}] (R3)
{};
\node[right=2cm of R3,yshift=-6mm,bullet,label={[yshift=-2mm]below:$R_4$}]
(R4){};
\node[right=2cm of R4,bullet,label={[yshift=-2mm]below:$R_5$}] (R5){};
\foreach \X [evaluate=\X as \Y using {int(\X+1)}] in {1,...,4}
{\draw[thick,-latex,cyan!50] (R\X) -- (R\Y);}
\draw[thick,red,{Circle}-latex,dashed] let \p1=($(R2)-(R1)$), \p2=($(R3)-
(R2)$),
\n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)} in
([yshift=1mm]R1.north) to[out=\n1,in=180,looseness=0.5]
([yshift=1mm]R2.north) to[out=0,in=\n2-180,looseness=0.5]
([yshift=1mm,xshift=-2mm]R3.north);
\end{tikzpicture}
\end{document}
答案1
需要为红线添加一些代码:
\draw[thick,red,{Circle}-latex,dashed] let \p1=($(R2)-(R1)$), \p2=($(R3)-(R2)$), \p3=($(R4)-(R3)$),
\n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)},\n3={atan2(\y3,\x3)} in
([yshift=1mm]R1.north) to[out=\n1,in=180,looseness=0.5]
([yshift=1mm]R2.north) to[out=0,in=\n2-180,looseness=0.5]
([yshift=1mm]R3.north) to[out=0,in=\n3-180,looseness=0.5]
([yshift=1mm,xshift=-2mm]R4.north);
拥有完整的 MWE
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,calc,arrows.meta}
\begin{document}
\begin{tikzpicture}[bullet/.style={draw,circle,minimum width=3mm,inner sep=0pt,fill=cyan!50}]
\node[bullet,label={[yshift=-2mm]below:$R_1$}] (R1){};
\node[right=2cm of R1,bullet,label={[yshift=-2mm]below:$R_2$}] (R2){};
\node[right=2cm of R2,yshift=6mm,bullet,label={[yshift=2mm]above:$R_3$}] (R3)
{};
\node[right=2cm of R3,yshift=-6mm,bullet,label={[yshift=-2mm]below:$R_4$}]
(R4){};
\node[right=2cm of R4,bullet,label={[yshift=-2mm]below:$R_5$}] (R5){};
\foreach \X [evaluate=\X as \Y using {int(\X+1)}] in {1,...,4}
{\draw[thick,-latex,cyan!50] (R\X) -- (R\Y);}
%\draw[thick,red,{Circle}-latex,dashed] let \p1=($(R2)-(R1)$), \p2=($(R3)-(R2)$),
\draw[thick,red,{Circle}-latex,dashed] let \p1=($(R2)-(R1)$), \p2=($(R3)-(R2)$), \p3=($(R4)-(R3)$),
\n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)},\n3={atan2(\y3,\x3)} in
([yshift=1mm]R1.north) to[out=\n1,in=180,looseness=0.5]
([yshift=1mm]R2.north) to[out=0,in=\n2-180,looseness=0.5]
([yshift=1mm]R3.north) to[out=0,in=\n3-180,looseness=0.5]
([yshift=1mm,xshift=-2mm]R4.north);
\end{tikzpicture}
\end{document}
你得到:
答案2
您无需通过巧妙的计算来重建红色虚线箭头的路径,而是可以使用preaction
允许您将 放置path
在临时位置scope
并垂直移动 的键transform canvas
。
为了让它看起来更漂亮,我用圆角键将角度弄圆rounded corners=5pt
。
\path[rounded corners=5pt,preaction={draw,red,thick,{Circle}-latex,
,transform canvas={yshift=3mm}}
] (R1.center)to(R2.center)to(R3.center)to(R4.center);
没有圆角的结果:
带有圆角和虚线的结果:
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,calc,arrows.meta}
\begin{document}
\begin{tikzpicture}[bullet/.style={draw,circle,minimum width=3mm,inner
sep=0pt,
fill=cyan!50}]
\node[bullet,label={[yshift=-2mm]below:$R_1$}] (R1){};
\node[right=2cm of R1,bullet,label={[yshift=-2mm]below:$R_2$}] (R2){};
\node[right=2cm of R2,yshift=6mm,bullet,label={[yshift=2mm]above:$R_3$}] (R3)
{};
\node[right=2cm of R3,yshift=-6mm,bullet,label={[yshift=-2mm]below:$R_4$}]
(R4){};
\node[right=2cm of R4,bullet,label={[yshift=-2mm]below:$R_5$}] (R5){};
\foreach \X [evaluate=\X as \Y using {int(\X+1)}] in {1,...,4}
{\draw[thick,-latex,cyan!50] (R\X) -- (R\Y);}
\path[rounded corners=5pt,preaction={draw,red,dashed,thick,{Circle}-latex,
,transform canvas={yshift=3mm}}
] (R1.center)to(R2.center)to(R3.center)to(R4.center);
\end{tikzpicture}
\end{document}
答案3
不错的变体安德烈·C回答:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,
chains,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 6mm and 20mm,
start chain = going right,
arr/.style = {thick, -Stealth, color=#1},
dot/.style = {circle, draw, fill=cyan!50,
minimum size=3mm, inner sep=0pt,
on chain, join=by {arr=cyan!50},
node contents={}},
every label/.style = {label distance=2mm, inner sep=0pt}
]
\node (R1) [dot,label=below:$R_1$];
\node (R2) [dot, right=of R1,label=below:$R_2$];
\node (R3) [dot, above right=of R2,label=above:$R_3$];
\node (R4) [dot, below right=of R3,label=below:$R_4$];
\node (R5) [dot, right=of R4,label=below:$R_5$];
\draw[arr=red, densely dashed, rounded corners=4pt,
{Circle[length=1.6mm]}-Latex, shorten <=-0.8mm,
transform canvas={yshift=1.5mm}]
(R1.north) -- (R2.north) -- (R3.north) -- (R4.north);
\end{tikzpicture}
\end{document}