Estoy intentando hacer una imagen de un árbol ternario con raíces infinitas; el siguiente código hará el trabajo bastante bien.
\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}
Sin embargo, me gustaría que los bordes correspondientes a cada letra se colorearan con diferentes colores, digamos L rojo, M verde y G azul. Lo intenté
\symbol{L}{\pgflsystemdrawforward}[red]
\symbol{M}{\pgflsystemdrawforward}[green]
\symbol{R}{\pgflsystemdrawforward}[blue]
pero no parece hacer nada. ¿Alguna sugerencia?
Respuesta1
Después de pensarlo un poco, creo que la forma más sencilla es dibujar las partes R,M,L por separado en diferentes colores. Así que me dividí 3-ary rooted tree
en a R
, a M
y a L
part, cada uno dibujando solo una de las tres "partes" y usando \pgflsystemmoveforward
en lugar de \pgflsystemdrawforward
las otras dos. Luego puedes usar 3 \draw
comandos diferentes con 3 colores diferentes :)
.
\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}
Editar: Encontré un truco (¿error? ¿característica?) para declarar el árbol solo una vez: el resultado es exactamente el mismo que el anterior. El truco es que puedes usar el \symbol
comando para moverte en las 3 direcciones sin dibujar, y decirle que dibuje en una de las tres, y anulará el comando anterior "solo mover" para esa dirección. ¡Dale un parámetro y ya tendrás que dibujar dinámicamente la rama que quieras!
\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}