如何使用 tikz l-systems 绘制不同颜色的不同线条

如何使用 tikz l-systems 绘制不同颜色的不同线条

我正在尝试绘制一个无限根三元树的图像,下面的代码可以很好地完成这项工作。

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}

\begin{figure}
    \begin{tikzpicture}
        \pgfdeclarelindenmayersystem{3-ary rooted tree}{
            \rule{L -> L [s+ [-L] [M] [+R]]}
            \rule{M -> M [s  [-L] [M] [+R]]}
            \rule{R -> R [s- [-L] [M] [+R]]}

            \symbol{L}{\pgflsystemdrawforward}
            \symbol{M}{\pgflsystemdrawforward}
            \symbol{R}{\pgflsystemdrawforward}
            \symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
        }
        \draw[rotate = -90] l-system [l-system={3-ary rooted tree,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
    \end{tikzpicture}
\end{figure}
\end{document}

但是,我希望每个字母对应的边缘都涂上不同的颜色,比如 L 为红色、M 为绿色、G 为蓝色。我试过了

  \symbol{L}{\pgflsystemdrawforward}[red]
  \symbol{M}{\pgflsystemdrawforward}[green]
  \symbol{R}{\pgflsystemdrawforward}[blue]

但似乎什么也没做。有什么建议吗?

答案1

经过一番思考,我认为最简单的方法是用不同的颜色分别绘制 R、M、L 部分。因此,我将其分成3-ary rooted treea R、aM和 aL部分,每个部分只绘制三个“部分”中的一个,并使用\pgflsystemmoveforward代替\pgflsystemdrawforward绘制其他两个部分。然后,您可以使用 3 个不同的\draw命令和 3 种不同的颜色:)在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}

    \begin{figure}
        \begin{tikzpicture}
        \pgfdeclarelindenmayersystem{3-ary rooted tree-L}{  %define the left branch
            \rule{L -> L [s+ [-L] [M] [+R]]}
            \rule{M -> M [s  [-L] [M] [+R]]}
            \rule{R -> R [s- [-L] [M] [+R]]}

            \symbol{L}{\pgflsystemdrawforward}  % draw the L branch
            \symbol{M}{\pgflsystemmoveforward}  % only move
            \symbol{R}{\pgflsystemmoveforward}  % only move
            \symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
        }
        \pgfdeclarelindenmayersystem{3-ary rooted tree-M}{ % define the middle branch
            \rule{L -> L [s+ [-L] [M] [+R]]}
            \rule{M -> M [s  [-L] [M] [+R]]}
            \rule{R -> R [s- [-L] [M] [+R]]}

            \symbol{L}{\pgflsystemmoveforward}  % only move
            \symbol{M}{\pgflsystemdrawforward}  % draw the M branch
            \symbol{R}{\pgflsystemmoveforward}  % only move
            \symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
        }
        \pgfdeclarelindenmayersystem{3-ary rooted tree-R}{ % define the right branch
            \rule{L -> L [s+ [-L] [M] [+R]]}
            \rule{M -> M [s  [-L] [M] [+R]]}
            \rule{R -> R [s- [-L] [M] [+R]]}

            \symbol{L}{\pgflsystemmoveforward}  % only move
            \symbol{M}{\pgflsystemmoveforward}  % only move
            \symbol{R}{\pgflsystemdrawforward}  % draw the R branch
            \symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
        }
        \draw[rotate = -90,color=blue] l-system [l-system={3-ary rooted tree-L,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
        \draw[rotate = -90,color=red] l-system [l-system={3-ary rooted tree-M,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
        \draw[rotate = -90,color=green] l-system [l-system={3-ary rooted tree-R,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];

        \end{tikzpicture}
    \end{figure}
\end{document}

编辑:我发现了一个技巧(错误?功能?)来仅声明一次树:输出与上面完全相同。这个技巧是,您可以使用命令\symbol在 3 个方向上移动而不绘制,并告诉它在三个方向之一上绘制,它将覆盖该方向的先前“仅移动”命令。给它一个参数,您就可以让它动态绘制您想要的分支!

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\newcommand{\defbranch}[1]{
    \pgfdeclarelindenmayersystem{3-ary rooted tree#1}{  % dynamic definition of the branches
        \rule{L -> L [s+ [-L] [M] [+R]]}
        \rule{M -> M [s  [-L] [M] [+R]]}
        \rule{R -> R [s- [-L] [M] [+R]]}

        \symbol{L}{\pgflsystemmoveforward} % only move
        \symbol{M}{\pgflsystemmoveforward} % only move
        \symbol{R}{\pgflsystemmoveforward} % only move
        \symbol{#1}{\pgflsystemdrawforward}  % draw the branch (overrides previous command)
        \symbol{s}{\pgflsystemstep =0.4 \pgflsystemstep}
    }
}
\defbranch{L} % define the branches (needed only once per documents)
\defbranch{M}
\defbranch{R}

\newcommand{\branch}[2]{  %define command to draw branches.
    %You can add more parameters for step, angle, order,... if needed
    \draw[rotate = -90,color=#2] l-system [l-system={3-ary rooted tree#1,axiom = [-L] [M] [+R], angle=45, step=6cm, order=5}];
}

\begin{document}
\begin{figure}
    \begin{tikzpicture}
        \branch{L}{blue}
        \branch{M}{red}
        \branch{R}{green}
    \end{tikzpicture}
\end{figure}
\end{document}

相关内容