如何使以下递归宏起作用?

如何使以下递归宏起作用?

对于小于 3 的正值,以下 MWE 有效。

\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}

如何使以下递归宏对大于 2 的正值起作用?

答案1

如果我在调用之前展开算术,我会得到该图像,但会出现一些警告:

在此处输入图片描述

** 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)

(赛莱特)

\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}

答案2

这里没有必要使用递归。迭代速度更快:

\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}

在此处输入图片描述

与递归相同:

\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}

在此处输入图片描述

相关内容