Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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}
share|improve this answer
    
No warning when using latex-dvips-ps2pdf and compilation is much much much much faster. –  cyanide-based food Apr 21 '13 at 19:26

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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