
Estoy intentando dibujar dos triángulos de Sierpinski que se tocan en un vértice. Mi código es el siguiente:
\documentclass[../main.tex]{subfiles}
\begin{document}
\begin{tikzpicture}[
main tri/.style={isosceles triangle, isosceles triangle apex angle=60, draw, anchor=apex, inner sep=0},
node distance=0mm,
minimum height=6cm
]
% Define recursive macro to create the Sierpinski triangle pattern
\newcommand\createsierpinski[3]{
\ifnum#3>0
% Calculate size for the smaller triangles
\pgfmathsetmacro{\newsize}{#1/2}
% Calculate the position for the next set of triangles
\path #2;
\pgfmathsetmacro{\newya}{\y-0.25*#1}
\pgfmathsetmacro{\newyb}{\y-0.75*#1}
% Draw the three smaller triangles
\createsierpinski{\newsize}{([yshift=-0.25*#1]#2)}{#3-1}
\createsierpinski{\newsize}{([yshift=-0.25*#1]#2.east)}{#3-1}
\createsierpinski{\newsize}{([yshift=-0.75*#1]#2.east)}{#3-1}
\fi
}
% First Sierpinski triangle
\node[main tri] (A) {};
\createsierpinski{6cm}{A}{4}
% Second Sierpinski triangle (rotated to touch at the tip)
\node[main tri, rotate=180] (B) at (A.apex) {};
\createsierpinski{6cm}{B}{4}
\end{tikzpicture}
\end{document}
Respuesta1
Puede que no sea la solución más elegante, pero debería funcionar:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
main tri/.style={
isosceles triangle,
isosceles triangle apex angle=60,
draw,
anchor=apex,
inner sep=0pt
},
node distance=0mm,
minimum height=6cm,
]
% Define recursive macro to create the Sierpinski triangle pattern
\newcommand\createsierpinski[4][]{
\ifnum#4>0
% Calculate size for the smaller triangles
\pgfmathsetmacro{\newsize}{#2/2}
\node[main tri, #1, anchor=apex, minimum height={\newsize}]
(#3-1) at (#3.apex) {};
\node[main tri, #1, anchor=right corner, minimum height={\newsize}]
(#3-2) at (#3.right corner) {};
\node[main tri, #1, anchor=left corner, minimum height={\newsize}]
(#3-3) at (#3.left corner) {};
% Draw the three smaller triangles
\pgfmathtruncatemacro{\newi}{#4-1}
\begin{scope}
\createsierpinski[#1]{\newsize}{#3-1}{\newi}
\end{scope}
\begin{scope}
\createsierpinski[#1]{\newsize}{#3-2}{\newi}
\end{scope}
\begin{scope}
\createsierpinski[#1]{\newsize}{#3-3}{\newi}
\end{scope}
\fi
}
\pgfmathsetmacro{\y}{1}
% First Sierpinski triangle
\node[main tri] (A) {};
\createsierpinski{6cm}{A}{4}
% Second Sierpinski triangle (rotated to touch at the tip)
\node[main tri, rotate=180] (B) at (A.apex) {};
\createsierpinski[rotate=180]{6cm}{B}{4}
\end{tikzpicture}
\end{document}
Su código tiene algunos problemas y otras deficiencias.
Con
\path #2;
yA
dado el segundo argumento, obtendrá\path A;
cuál no es válido Tiksintaxis Z y es el motivo del error "falta punto y coma". Debe utilizar paréntesis aquí y eliminarlos en los\createsierpinski
comandos anidados.Si proporciona
[yshift=-0.25*#1]#2
un segundo argumento al comando, también obtendrá, en algún momento, una sintaxis no válida. Pero en realidad no es necesario,yshift
ya que simplemente puede utilizar los anclajes de la forma del nodo para el posicionamiento.Su definición de comando actualmente no incluye
\draw
ni un\node
comando, por lo tanto, no hay salida.No puedes decirlo
\ifnum3-1>0
, pero debes hacer el cálculo de antemano porque\ifnum
solo puedes comparar números enteros. Puedes utilizar\pgfmathtruncatemacro
para este fin.Necesita algo de alcance, porque de lo contrario el primer anidado
\createsierpinski
ya finalizará el ciclo de recursividad.Si desea rotar cosas, debe pasar esta transformación como opción a cada
\createsierpinski
comando.
Respuesta2
Aquí hay una versión que usa lindenmayersystems
(capítulo 57 en elmanual de instrucciones).
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{tikzpicture}[l-system={step=.5pt, order=8, angle=60}, rotate=-30]
\pgfdeclarelindenmayersystem{Sierpinski triangle}{
\symbol{X}{\pgflsystemdrawforward}
\symbol{Y}{\pgflsystemdrawforward}
\rule{X -> Y-X-Y}
\rule{Y -> X+Y+X}
}
\draw[red] (0,0) l-system[l-system={Sierpinski triangle, axiom=+++X, anchor=north east}];
\draw[blue] (0,0) l-system[l-system={Sierpinski triangle, axiom=X, anchor=south west}];
\end{tikzpicture}
\end{document}