我正在嘗試製作一張無限根三元樹的圖片,下面的程式碼可以很好地完成這項工作。
\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 tree
一個R
、一個M
和一個L
部分,每個部分只繪製三個「部分」之一,並使用\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}
編輯:我發現了一個技巧(bug?功能?)只聲明樹一次:輸出與上面完全相同。技巧是您可以使用該\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}