decorations.markings:隐藏路径

decorations.markings:隐藏路径

我正在使用 decorations.markings 库绘制链条(金属类型),但遇到了一些问题:

  1. 我想删除链条后面的路径。我无法设置路径的颜色来隐藏它,因为链条将位于阴影区域前面,颜色会发生变化。
  2. 由于路径上的曲线紧密,相邻的链接对表现不佳。我意识到这是由于我的双链接形状的长度相对于曲线的紧密度。是否可以将双链接形状拆分为单独的(即较短的)形状并分别绘制它们,以便它们更紧密地贴合路径?我该怎么做?

TIA,戴夫

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, decorations.markings}

\begin{document}
\begin{tikzpicture}[line cap=round,decoration={
    markings,% switch on markings
    mark=% actually add a mark
    between positions 0 and 1 step 12pt
    with
    {       
        \draw[red, very thick] (0pt,-2pt) -- ++(4pt,0) arc(-90:90:2pt) -- ++(-4pt,0pt) arc(90:270:2pt) -- cycle;
        \draw[red,very thick] (-8pt,0) -- (0pt,0);      
    }
    }]
    \draw[ultra thin, postaction={decorate}] (0,0) -- +(0,7.5) arc(180:90:1) -- +(2,0);
\end{tikzpicture}
\end{document}

答案1

这是“合理”完整的。链接数与连续链接的重叠一起指定。装饰“尽力”提供沿路径的完整链接数,但(与装饰一样),TeX 数学有点不准确,因此某些参数设置可能会产生意外的输出或错误。

其他定制(未在此处实现)包括初始链接是“侧”链接(即直线)还是“前”链接(即拉长的“O”),以及第一个/最后一个链链接是否应该是居中在路径的起点/终点或者左/右对齐(就像这里一样)。

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{calc, decorations.markings}
\pgfdeclaredecoration{chain links}{prepare}{
\state{prepare}[width=0,next state=shift, 
  persistent precomputation={
    \def\chainstate{0}
    \ifpgflinkcentered
        \def\pgflinkshift{0pt}
          \pgfmathsetlengthmacro\pgflinklength{\pgfdecoratedpathlength/
            \pgflinkcount+\pgflinkoverlap}
      \else
        \def\pgflinkshift{\pgflinklength/2}
          \pgfmathsetlengthmacro\pgflinklength{(\pgfdecoratedpathlength
        -\pgflinkoverlap)/\pgflinkcount+\pgflinkoverlap}
      \fi
      \pgfmathsetlengthmacro\pgflinkheight{\pgfdecorationsegmentamplitude}
  }]{}
\state{shift}[width=\pgflinkshift, next state=link]{}
\state{link}[width=\pgflinklength-\pgflinkoverlap,
  persistent precomputation={\pgfmathsetmacro\chainstate{int(1-\chainstate}}]    
    {\pgfchainlinkdrawlink}
\state{final}{
  \pgfmathsetmacro\chainstate{int(1-\chainstate)}
  \pgfchainlinkdrawlink
 }
}
\def\pgfchainlinkdrawlink{%
  \ifcase\chainstate
    \path [every chain link/.try]
      (-\pgflinklength/2, 0) -- (\pgflinklength/2, 0);
  \else
    \path [every chain link/.try]
      (0, \pgflinkheight/2) --
      (\pgflinklength/2-\pgflinkheight/2, \pgflinkheight/2)
          arc (90:-90:\pgflinkheight/2) --
          (-\pgflinklength/2+\pgflinkheight/2, -\pgflinkheight/2)
          arc (270:90:\pgflinkheight/2) -- cycle;
    \fi
}
\newif\ifpgflinkcentered
\pgfkeys{/pgf/decoration/.cd,
  link count/.store in=\pgflinkcount,
  link overlap/.store in=\pgflinkoverlap,
  center links/.code=\csname pgflinkcentered#1\endcsname,
  center links/.default=true,
  link count=20,
  link overlap=2pt,
  center links=false,
}
\tikzset{%
  every chain link/.style={
    line cap=round,
    very thick,
    draw=red,
  }
}
\begin{document}
\begin{tikzpicture}[decoration={chain links, amplitude=4pt}]
\path [postaction={decorate}] 
  (0,0) -- ++(0,2) arc(180:90:1) -- ++(2,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容