6

For positive values less than 3 the following MWE works.

\documentclass[pstricks,border=12pt]{standalone}
\SpecialCoor
\psset{showpoints=true}

\def\Triangle#1{%
    \ifnum#1=1\relax
        \pspolygon(0,0)(!.5 3 sqrt 2 div neg)(1,0)
    \else
        \rput(0,0){\Triangle{\numexpr#1-1\relax}}
        \rput{-60}(!#1 1 sub 0){\Triangle{\numexpr#1-1\relax}}
        \rput(!#1 1 sub 0){\Triangle{\numexpr#1-1\relax}}
        \rput(!#1 1 sub 2 div dup 3 sqrt mul neg){\Triangle{\numexpr#1-1\relax}}
    \fi
}
\begin{document}
\begin{pspicture}(5,-5)
    \Triangle{3}
\end{pspicture}
\end{document}

How to make the following recursive macro work for positive values greater than 2?

9

If I expand the arithmetic before the call I get that image but a few warnings:

enter image description here

** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)
** WARNING ** << /Rotate 270 >> found. (Not supported yet)

(xelatex)

\documentclass[pstricks,border=12pt]{standalone}
\SpecialCoor
\psset{showpoints=true}

\def\Triangle#1{%
    \ifnum#1=1\relax
        \pspolygon(0,0)(!.5 3 sqrt 2 div neg)(1,0)
    \else
        \rput(0,0){\expandafter\Triangle\expandafter{\the\numexpr#1-1\relax}}
        \rput{-60}(!#1 1 sub 0){\expandafter\Triangle\expandafter{\the\numexpr#1-1\relax}}
        \rput(!#1 1 sub 0){\expandafter\Triangle\expandafter{\the\numexpr#1-1\relax}}
        \rput(!#1 1 sub 2 div dup 3 sqrt mul neg){\expandafter\Triangle\expandafter{\the\numexpr#1-1\relax}}
    \fi
}
\begin{document}
\begin{pspicture}(5,-5)
    \Triangle{3}
\end{pspicture}
\end{document}
  • No warning when using latex-dvips-ps2pdf and compilation is much much much much faster. – kiss my armpit Apr 21 '13 at 19:26
8

it makes no sense to use a recursion here. An iteration is faster:

\documentclass{article}
\usepackage{pstricks,multido}
\SpecialCoor

\def\Triangle#1{%
 \edef\Cnt{\the\numexpr#1-1}
 \psset{yunit=0.866cm}
 \begin{pspicture}(\Cnt,-\Cnt)
  \multido{\rA=0.0+0.5,\iA=#1+-1}{#1}{%
    \multido{\iB=0+1}{\iA}{\psdot(!\rA\space dup \iB\space add exch neg 2 mul)}
    \def\Line{\psline(!\rA\space dup neg 2 mul)(!#1 \rA\space sub 1 sub \rA\space neg 2 mul)}
    \Line
    \rput[lb]{240}(\Cnt,0){\Line}
    \rput[lb]{120}(!\Cnt\space dup 2 div exch neg){\Line}
}
 \end{pspicture}}
\begin{document}    
\Triangle{3} \Triangle{7}    
\end{document}

enter image description here

and the same as a recursion:

\documentclass{article}
\usepackage{pstricks,multido}
\SpecialCoor
\def\Atom(#1,#2)#3{%
  \multido{\iA=0+1}{\numexpr#3-1}{%
    \rput(!#1 \iA\space add #2){\pspolygon[showpoints](0,0)(1,0)(.5,-1)}}%
  \ifnum#3>1 \edef\No{\the\numexpr#3-1}\Atom(#1 0.5 add ,#2 1 sub ){\No}\fi}

\def\Triangle#1{%
  \psset{yunit=0.8660254cm,dotscale=1.5}
  \begin{pspicture}(\numexpr#1-1,\numexpr-#1+1)
    \Atom(0,0){#1}%}
  \end{pspicture}}
\begin{document}    
\Triangle{3} \hspace{-4cm}\Triangle{12}    
\end{document}

enter image description here

  • But I am interested to learn how to implement it in recursive form rather than the usual form with iteration. – kiss my armpit Apr 22 '13 at 11:15
  • see edited answer – user2478 Apr 23 '13 at 18:15
  • I cannot work for \Triangle{128}. :-) – kiss my armpit Apr 23 '13 at 18:18
  • sure, it will if you extend the main memory size ... – user2478 Apr 23 '13 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.