环绕两个线段的虚线轨迹

环绕两个线段的虚线轨迹

在 TikZ 中,我想绘制两个线段(黑色)和一条围绕这些线段的虚线轨迹(红色),如图所示。有没有办法仅根据两个线段的坐标轻松绘制虚线轨迹?

在此处输入图片描述

这是我的尝试:

\begin{tikzpicture}
\draw [thick] (1,1) -- (2,2);
\draw [thick] (2,2) -- (2,3.5);
\draw [dashed] (2.2,2) -- (2.2,3.7) -- (1.8,3.7) -- (1.8,2) -- (0.8,1) -- (1.1,0.8) -- (2.2,2);
\end{tikzpicture}

它看上去不太好看,而且我无法做出曲线。

在此处输入图片描述

答案1

如果你可以接受不被破折号,那么round cap可以提供帮助:

\documentclass[tikz, border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw [thick] (1,1) -- (2,2);
\draw [thick] (2,2) -- (2,3.5);
\draw [dashed] (2.2,2) -- (2.2,3.7) -- (1.8,3.7) -- (1.8,2) -- (0.8,1) -- (1.1,0.8) -- (2.2,2);

\begin{scope}[xshift=2cm]
\draw[line cap=round, red!30, line width=2.5mm] (1,1)--(2,2)--(2,3.5);
\draw (1,1)--(2,2)--(2,3.5);
\end{scope}

\begin{scope}[xshift=4cm]
\draw[line cap=round, line width=3mm] (1,1)--(2,2)--(2,3.5);
\draw[line cap=round, white, line width=2.5mm] (1,1)--(2,2)--(2,3.5);
\draw (1,1)--(2,2)--(2,3.5);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:只要有耐心和hobby资源,你就可以做类似的事情:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{hobby}

\begin{document}
\begin{tikzpicture}
\draw (1,1) node (a) {}  --(2,2) node (b) {} --(2,3.5)  node[rotate=45] (c) {};
\draw[dashed, use Hobby shortcut] (a.south west) to[closed, curve through={(a.north west) .. (b.north west) .. (c.north west) .. (c.north east) .. (c.south east) .. (b.south east) .. (a.south east)}] cycle; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

下面在简单路径周围绘制光晕,就像上面的一样。您只需记录路径

\draw[thick,postaction={record path={step=5pt}}] (1,1) -- (2,2) -- (2,3.5);

然后可以通过绘制光环

\draw[dashed,halo];

在此处输入图片描述

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\newcounter{halo}
\tikzset{record path/.style={/utils/exec=\tikzset{halo pars/.cd,#1},
    decorate,decoration={markings,mark=at position 0 with
    {\setcounter{halo}{1}%\typeout{\pgfdecoratedpathlength}
    \path (0pt,{\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-L-\number\value{halo})
    (0pt,{-1*\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-R-\number\value{halo})
    ({-\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)},{-\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)}) coordinate (halo-A-1)
    ({-\pgfkeysvalueof{/tikz/halo pars/dist}},{0pt}) coordinate (halo-A-2)
    ({-\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)},{\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)}) coordinate (halo-A-3);
     \pgfmathsetmacro{\mystep}{(\pgfdecoratedpathlength-2*\pgfkeysvalueof{/tikz/halo pars/step})/int(1+(\pgfdecoratedpathlength-2*\pgfkeysvalueof{/tikz/halo pars/step})/\pgfkeysvalueof{/tikz/halo pars/step})}
     \xdef\mystep{\mystep}},
    mark=between positions \pgfkeysvalueof{/tikz/halo pars/step} and {\pgfdecoratedpathlength-\pgfkeysvalueof{/tikz/halo pars/step}} step \mystep pt with {\stepcounter{halo}
    \path (0pt,{\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-L-\number\value{halo})
    (0pt,{-1*\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-R-\number\value{halo});},
    mark=at position 1 with {\stepcounter{halo}
    \path (0pt,{\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-L-\number\value{halo})
    (0pt,{-1*\pgfkeysvalueof{/tikz/halo pars/dist}}) coordinate (halo-R-\number\value{halo})
    ({\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)},{\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)}) coordinate (halo-B-1)
    ({\pgfkeysvalueof{/tikz/halo pars/dist}},{0pt}) coordinate (halo-B-2)
    ({\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)},{-\pgfkeysvalueof{/tikz/halo pars/dist}/sqrt(2)}) coordinate (halo-B-3);
    \xdef\LstHaloCoords{(halo-A-1) (halo-A-2) (halo-A-3)}
    \foreach \XX in {1,...,\number\value{halo}}
    {\xdef\LstHaloCoords{\LstHaloCoords\space (halo-L-\XX)}}
    \xdef\LstHaloCoords{\LstHaloCoords\space (halo-B-1) (halo-B-2) (halo-B-3)}
    \foreach \XX in {\number\value{halo},\the\numexpr\number\value{halo}-1,...,1}
    {\xdef\LstHaloCoords{\LstHaloCoords\space (halo-R-\XX)}}
    }}},
    halo/.style={insert path={plot[smooth,samples at={1,...,\number\value{bracep}},variable=\x]
    (bracep-\x)}},halo/.style={insert path={plot[smooth cycle] coordinates {\LstHaloCoords} }},
    halo pars/.cd,dist/.initial=5pt,step/.initial=2pt}
\begin{document}
\begin{tikzpicture}
    \draw[thick,postaction={record path={step=5pt}}] (1,1) -- (2,2) -- (2,3.5);
    \draw[dashed,halo];
\end{tikzpicture}
\end{document}

haloTi 知道该路径Z,因此它可以用于各种各样的事情,比如交叉点或用图案填充(需要库intersectionspatterns),例如

\draw[thick,postaction={record path={step=5pt}}] (1,1) -- (2,2) -- (2,3.5);
\draw[red,pattern=north west lines,pattern color=blue,halo];

在此处输入图片描述

如您所见,结果并不完美,但可以通过调整步长参数来改进(我故意选择这个参数,以免欺骗用户)。很难获得适用于更复杂路径(尤其是自相交路径)的自动良好解决方案。但是,上述方法也适用于曲线,即不仅仅是直线。


还要注意,如果我只取虚线轮廓,添加rounded corners并替换最后一个坐标cycle(以便最后一个角也是圆的),我得到

在此处输入图片描述

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [thick] (1,1) -- (2,2);
\draw [thick] (2,2) -- (2,3.5);
\draw [dashed,rounded corners] (2.2,2) -- (2.2,3.7) -- (1.8,3.7) -- (1.8,2) --
(0.8,1) -- (1.1,0.8) -- cycle;
\end{tikzpicture}
\end{document}

如果一次性画出第一条线,并在绘制轮廓时考虑到小腿的倾斜度,那么事情看起来可能会更好。

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [thick] (1,1) -- (2,2) -- (2,3.5);
\draw [dashed,rounded corners] (2.2,1.9) -- (2.2,3.7) -- (1.8,3.7) -- (1.8,2.1) --
(0.8,1) -- (1.1,0.8) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果希望虚线图案闭合,请使用

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\draw [thick] (1,1) -- (2,2) -- (2,3.5);
\path [rounded corners,preaction={decorate,decoration={markings,mark=at position 0 with {%
\pgfmathsetmacro{\myon}{\pgfdecoratedpathlength/50}
\xdef\myon{\myon}}}},postaction={draw,dash pattern=on \myon pt off \myon pt}] (2.2,1.9) -- (2.2,3.7) -- (1.8,3.7) -- (1.8,2.1) --
(0.8,1) -- (1.1,0.8) -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容