
Estou tentando desenhar dois triângulos de Sierpinski que se tocam em um vértice. Meu código é o seguinte:
\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}
Responder1
Pode não ser a solução mais elegante, mas deve 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}
Seu código tem alguns problemas e outras deficiências.
Com
\path #2;
eA
dado ao segundo argumento, você obterá\path A;
o que não é válido TikSintaxe Z e é o motivo do erro de "ponto e vírgula ausente". Você precisa usar parênteses aqui e removê-los nos\createsierpinski
comandos aninhados.Se você fornecer
[yshift=-0.25*#1]#2
como segundo argumento ao comando, também obterá, em algum momento, uma sintaxe inválida. Mas na verdade não há necessidade,yshift
pois você pode simplesmente usar as âncoras do formato do nó para posicionamento.Sua definição de comando atualmente não inclui
\draw
nem um\node
comando, portanto, não há saída.Você não pode dizer
\ifnum3-1>0
, mas precisa fazer o cálculo antecipadamente porque\ifnum
só pode comparar números inteiros. Você pode usar\pgfmathtruncatemacro
para esse fim.Você precisa de algum escopo, caso contrário o primeiro aninhado
\createsierpinski
já encerrará o loop de recursão.Se você quiser girar as coisas, você precisa passar essa transformação como opção para cada
\createsierpinski
comando.
Responder2
Aqui está uma versão usando lindenmayersystems
(capítulo 57 doManual PGF).
\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}