我试图在 tikz 中绘制一条路径,使用 Paul Gaborit 的解决方案TikZ:如何在线中间画箭头?我设法得到了这个,其中路径是环的一半。
但我希望左下角的箭头和内侧半圆箭头向右移动(因此,在遍历路径时所有箭头都具有相同的方向,并且环的内部位于左侧)。
我在绘画中糟糕的编辑
我正在使用的代码
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows, decorations.markings, decorations.pathreplacing}
\tikzset{
% style to apply some styles to each segment of a path
on each segment/.style={
decorate,
decoration={
show path construction,
moveto code={},
lineto code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
curveto code={
\path [#1] (\tikzinputsegmentfirst)
.. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
..
(\tikzinputsegmentlast);
},
closepath code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
},
% style to add an arrow in the middle of a path
mid arrow/.style={postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow[#1]{stealth}}
}}},
}
\begin{center}
\begin{tikzpicture}[domain=0:4]
\draw [<->, very thick] (0,4) node (yaxis) [above] {$y$}
|- (-4,0) node (zaxis) [left] {}
|- (4,0) node (xaxis) [right] {$x$}
;
\path [draw=black, ultra thick, postaction={on each segment={mid arrow=black}}] (3,0) arc (0:180:3cm)
(1,0) -> (3,0)
(1,0) arc (0:180:1cm)
(-1,0) -> (-3,0) ;
\end{tikzpicture}
\end{center}
任何帮助,将不胜感激 :)
答案1
将来,请考虑发布完整的最小工作示例(MWE)。这让其他人更容易开始帮助你。:-)
Paul Gaborit 的代码将箭头应用于路径遍历的方向。因此在这种情况下,只需更改有问题的路径段的方向即可。
我只需要更改环境中的两行代码tikzpicture
;这两行更改都在下面代码的注释行中详细说明。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows, decorations.markings, decorations.pathreplacing}
\tikzset{
% style to apply some styles to each segment of a path
on each segment/.style={
decorate,
decoration={
show path construction,
moveto code={},
lineto code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
curveto code={
\path [#1] (\tikzinputsegmentfirst)
.. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
..
(\tikzinputsegmentlast);
},
closepath code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
},
% style to add an arrow in the middle of a path
mid arrow/.style={postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow[#1]{stealth}}
}}},
}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw [<->, very thick] (0,4) node (yaxis) [above] {$y$}
|- (-4,0) node (zaxis) [left] {}
|- (4,0) node (xaxis) [right] {$x$}
;
\path [draw=black, ultra thick, postaction={on each segment={mid arrow=black}}] (3,0) arc (0:180:3cm)
(1,0) -> (3,0)
(-1,0) arc (180:0:1cm) % changed starting point and swapped arc bounding angles
(-3,0) -> (-1,0) ; % swapped coordinates here
\end{tikzpicture}
\end{document}