TikZ:根据路径长度计算“预长度”

TikZ:根据路径长度计算“预长度”

我想自动计算pre length线条装饰“居中”的值。让我通过这个例子来解释一下:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.shapes}

\begin{document}
\begin{tikzpicture}[foo/.style={decorate,decoration={
    shape backgrounds,shape=circle,shape sep=2.5cm,pre=moveto,pre length=#1}}]
  \node at (0cm,0cm)  [label=below:0]  {};
  \node at (5cm,0cm)  [label=below:5]  {};
  \node at (9cm,0cm)  [label=below:9]  {};
  \node at (10cm,0cm) [label=below:10] {};
  \draw[foo=0cm]    (0cm,0cm) -- (10cm,0cm); % line 1
  \draw[foo=0cm]    (0cm,5mm) -- ( 9cm,5mm); % line 2
  \draw[foo=0.75cm] (0cm,1cm) -- ( 9cm,1cm); % line 3
\end{tikzpicture}
\end{document}

(我如何添加代码的编译版本?我是否必须手动编译并将其作为图像上传?)

编译版本

  • line 1(底部)是我想要的:从线的起点(0,0)到线的终点(10,0)的圆圈。
  • line 2(中间)显示问题:如果路径长度不是的倍数shape sep,则第一个圆圈位于路径的起点,而最后一个圆圈和路径终点之间没有任何内容。
  • line 3(顶部)显示了如何处理该问题:通过设置pre length正确的值(或者如果您有更好的想法,可以使用其他方法),路径起点和第一个圆之间的距离与最后一个圆和路径终点之间的距离相同。

现在我的问题是:我该如何pre length设置(path length - shape sep)/2(path length - (number of circles - 1) * shape sep) / 2(感谢 Altermundus 的修正)?或者有更好的解决方案吗?

答案1

我不太确定我的公式,但(path length - shape sep)/2不太好。您可以使用一些值,例如 \pgfmetadecoratedpathlength\pgf@lib@shapedecoration@sep

\documentclass{standalone}    
\usepackage{tikz}
\usetikzlibrary{decorations.shapes}

\begin{document} 
  \makeatletter
\begin{tikzpicture}%
   [foo/.style={decorate,
                decoration={shape backgrounds,
                            shape=circle,
                            shape sep=2.5cm,
                            pre=moveto,
   pre length=(\pgfmetadecoratedpathlength-%
              floor(\pgfmetadecoratedpathlength/\pgf@lib@shapedecoration@sep)*%
                     \pgf@lib@shapedecoration@sep)/2}}]
  \node at (0cm,0cm)  [label=below:0]  {};
  \node at (5cm,0cm)  [label=below:5]  {};
  \node at (9cm,0cm)  [label=below:9]  {};
  \node at (10cm,0cm) [label=below:10] {};
  \draw[foo] (0cm,1cm) -- ( 9cm,1cm); 
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

前段时间,我制作了下面的插图,将脂质分子沿着路径均匀分布。

中心部分是访问 \pgfdecoratedpathlength,从而计算相邻分子之间的距离。

根据给定路径调整的自定义路径装饰

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.pathreplacing}

\begin{document}

% Define decoration
\pgfdeclaredecoration{lipidleaflet}{initial}
{
  % Place as many segments as possible along the path to decorate
  % the minimum distance between two segments is set to 7 pt.
  \state{initial}[width=\pgfdecoratedpathlength/floor(\pgfdecoratedpathlength/7pt)]
  {
    % Draw the two acyl chains
    \pgfpathmoveto{\pgfpoint{-1pt}{0pt}}
    \pgfpathlineto{\pgfpoint{-1pt}{-10pt}}
    \pgfpathmoveto{\pgfpoint{1pt}{0pt}}
    \pgfpathlineto{\pgfpoint{1pt}{-10pt}}
    % Draw the head group
    \pgfpathmoveto{\pgfpoint{1pt}{0pt}}
    \pgfpathcircle{\pgfpoint{0pt}{2pt}}{2.5pt}
  }
  \state{final}
  {
    \pgfpathmoveto{\pgfpointdecoratedpathlast}
  }
}

% Draw a vesicle composed of two lipid layers
\begin{tikzpicture}
% Micelle
\draw[decorate, decoration={lipidleaflet, mirror}] (0, 3) circle (0.6cm);
\draw (0, 2) node {Micelle};

% Inverted micelle
\draw[decorate, decoration={lipidleaflet}] (0, 0) circle (0.45cm);
\draw (0, -1) node {Inverted micelle};

% Lipid bilayer
\draw[decorate, decoration={lipidleaflet, mirror}]
  (-1, -2.8) -- (2, -2.8);
\draw[decorate, decoration={lipidleaflet}]
  (-1, -2) -- (2, -2);
\draw (0, -3.5) node {Lipid bilayer};

% Vesicle
\draw[decorate, decoration={lipidleaflet}] (5, 0.5) circle (2.5cm);
\draw[decorate, decoration={lipidleaflet, mirror}] (5, 0.5) circle (3.3cm);
\draw (5, -3.5) node {Vesicle};

\end{tikzpicture}

\end{document} 

相关内容