10

This question is similar to How to improve my PSTricks code to construct a triangle number diagram?.

Consider the following example.

Code

% pdflatex -shell-escape test.tex

\documentclass{article}

\usepackage{
  auto-pst-pdf,
  pstricks-add
}

\begin{document}

\begin{figure}
\def\laengde{6}
\def\startX{3}
\def\startY{0}
\pstFPdiv\konstA{\laengde}{2}
\pstFPsub\Bmin{\startX}{\konstA}
\pstFPadd\Bmaks{\startX}{\konstA}
\pstFPadd\Hmin{\startY}{0}
\pstFPdiv\konstB{3 sqrt}{2}
\pstFPmul\Hmaks{\konstB}{\laengde}
 \centering
 \psset{
   radius=0.2
 }
  \begin{pspicture}(\Bmin,\Hmin)(\Bmaks,\Hmaks)
   \Cnode*(\startX,\startY){A}
   \Cnode*(!\startX\space \laengde\space   8 div     sub \startY\space   3 sqrt     8 div \laengde\space mul add){B}
   \Cnode*(!\startX\space \laengde\space   8 div     add \startY\space   3 sqrt     8 div \laengde\space mul add){C}
   \Cnode*(!\startX\space \laengde\space   4 div     sub \startY\space   3 sqrt     4 div \laengde\space mul add){D}
   \Cnode*(!\startX\space                                \startY\space   3 sqrt     4 div \laengde\space mul add){E}
   \Cnode*(!\startX\space \laengde\space   4 div     add \startY\space   3 sqrt     4 div \laengde\space mul add){F}
   \Cnode*(!\startX\space \laengde\space 3 8 div mul sub \startY\space 3 3 sqrt mul 8 div \laengde\space mul add){G}
   \Cnode*(!\startX\space \laengde\space   8 div     sub \startY\space 3 3 sqrt mul 8 div \laengde\space mul add){H}
   \Cnode*(!\startX\space \laengde\space   8 div     add \startY\space 3 3 sqrt mul 8 div \laengde\space mul add){I}
   \Cnode*(!\startX\space \laengde\space 3 8 div mul add \startY\space 3 3 sqrt mul 8 div \laengde\space mul add){J}
   \Cnode*(!\startX\space \laengde\space   2 div     sub \startY\space   3 sqrt     2 div \laengde\space mul add){K}
   \Cnode*(!\startX\space \laengde\space   4 div     sub \startY\space   3 sqrt     2 div \laengde\space mul add){L}
   \Cnode*(!\startX\space                                \startY\space   3 sqrt     2 div \laengde\space mul add){M}
   \Cnode*(!\startX\space \laengde\space   4 div     add \startY\space   3 sqrt     2 div \laengde\space mul add){N}
   \Cnode*(!\startX\space \laengde\space   2 div     add \startY\space   3 sqrt     2 div \laengde\space mul add){O}
   \pspolygon(A)(K)(O)
   \ncline{B}{N}
   \ncline{D}{M}
   \ncline{G}{L}
   \ncline{B}{C}
   \ncline{D}{F}
   \ncline{G}{J}
   \ncline{C}{L}
   \ncline{F}{M}
   \ncline{J}{N}
   \pnode(!\startX\space \laengde\space 3 8 div mul add \startY\space 3 sqrt 8 div \laengde\space mul add){S}
   \rput(S){\footnotesize{$15$~prikker}}
   \end{pspicture}
\end{figure}

\end{document}

Output

enter image description here

Question

I have the desired output but the code is rather messy; can anyone help me simplify if? (The code can probably be made even 'simpler' than in the other post.)

Update

Using the second method of @Herbert in the other post, I now have the following:

\documentclass{article}

\usepackage[pdf]{pstricks}
\usepackage{pst-eucl}

\newcounter{No}
\setcounter{No}{0}
\def\trekanttal#1#2{%
\psset{
  radius=#2,
  yunit=0.8660254
}

\begin{pspicture}(#1,#1)
\pspolygon[
  linecolor=black,
  linewidth=1.5pt
](0,#1)(#1,#1)(!#1 2 div 0)
\multido{\iRow=0+2,\iStep=1+1}{\numexpr#1/2+1}{%
  \multido{\iCol=0+2}{\iStep}{%
    \stepcounter{No}%
    \pnode(!#1 2 div \iStep\space sub 1 add \iCol\space add \iRow){N\theNo}
    \rput(N\theNo){
      \pscircle[
        fillstyle=solid,
        fillcolor=black
      ]{#2mm}
    }
  }
}
\end{pspicture}
}

\begin{document}

\begin{figure}[htbp]
\trekanttal{10}{3}
\end{figure}

\end{document}

but the lines between all the dots are missing.

4 Answers 4

5
+250

If you do not need the points as nodes for further modifications, then it is easy:

\documentclass{article}

%\usepackage[pdf]{pstricks}% for pdflatex --shell-escape
\usepackage{pst-eucl}

\def\trekanttal#1#2{%
  \psset{yunit=0.8660254cm}
  \begin{pspicture}(\numexpr2*#1-2,\numexpr2*#1-2)
  \pstVerb{ /MaxIndex #1 2 mul 2 sub def }
  \multido{\iRow=0+2,\iStep=1+1}{#1}{%
    \psline(!\iStep\space 1 sub MaxIndex \iRow\space sub)(!MaxIndex \iStep\space sub 1 add MaxIndex \iRow\space sub) 
    \psline(!\iRow\space MaxIndex)(!#1 \iStep\space add 2 sub \iRow\space) 
    \psline(!MaxIndex \iRow\space sub MaxIndex)(!#1 \iStep\space sub \iRow\space) 
    \multido{\iCol=0+2}{\iStep}{%
      \pscircle*(!#1 1 sub \iStep\space sub 1 add \iCol\space add \iRow){#2mm}%
  }}
  \end{pspicture}}

\begin{document}

\trekanttal{3}{3}
\trekanttal{6}{3}

\end{document}

enter image description here

I changed the meaning of the parameter: The first one is the number of dots in the outer row.

And the same with a recursive call:

\documentclass{article}
%\usepackage[pdf]{pstricks}% for pdflatex --shell-escape
\usepackage{pst-eucl}
\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\trekanttal#1{%
  \psset{yunit=0.8660254cm,dotscale=2.5}
  \begin{pspicture}(\numexpr#1-1,\numexpr-#1+1)
    \Atom(0,0){#1}
  \end{pspicture}}
\begin{document}

\trekanttal{3} \trekanttal{6}

\end{document}
1
  • Very nice, as always, Herbert! Commented Apr 20, 2013 at 4:21
6

UPDATE: Demonstrating the flexibility of my code.

(Thanks to @Herbert for mentioning \pspolygon[showpoints].)

Construction:

  1. In \tile we write the pstricks code how to create a tile (e.g., a triangle). For the length of a side of a triangle we use 2 for simplicity. Hence, half of the length of a side is 1.
  2. To create the entire figure we print \tile repeatedly by using two \multido loops. That is, for the bottom row, we print just one tile, for the row above, we print two tiles, and so on. How many rows are printed can be controlled by \br. -- The number in the name is calculated automatically.

Here is a picture to visualize how this works. Only nodes overlap in this case (when using triangles).

explanation

The following code is flexibel in the sense that it is simple to create similar pictures just by changing \tile and adjusting the horizontal and vertical offset in \rput. Here we show four such pictures including the "15 prikker".

examples

\documentclass{article}
\usepackage[pdf]{pstricks}
\usepackage{pst-eucl}

\begin{document}
\psset{radius=0.2, unit=8mm, dotsize=4pt 6}
\newcommand\br{4}%how many tiles in the top row, 4 -> "15 prikker"

\newcommand\tiletriangle{%------  this prints a triangle  ------
  \pspolygon[showpoints](!0 3 sqrt)(!1 0)(!2 3 sqrt)
}
\begin{pspicture}(\numexpr2*\br,\numexpr2*\br)
\multido{\iRow=0+1}{\br}{%
  \multido{\iCol=1+1}{\numexpr\iRow+1}{%
    \rput(!\iCol\space 2 mul \iRow\space sub
           \iRow\space 3 sqrt mul){\tiletriangle}
  }
}
\rput[l](!4 \br\space 3 div add 1.6){$\number\numexpr(\br+1)*(\br+2)/2$~prikker}
\end{pspicture}
%
%
\hfill
\newcommand\tilesquare{%------  this prints a square  ------
  \pspolygon[showpoints](2,1)(1,2)(0,1)(1,0)
}
\begin{pspicture}(\numexpr2*\br,\numexpr2*\br)
\multido{\iRow=0+1}{\br}{%
  \multido{\iCol=1+1}{\numexpr\iRow+1}{%
    \rput(!\iCol\space 2 mul \iRow\space sub
           \iRow\space){\tilesquare}
  }
}
\rput[l](!4 \br\space 3 div add  1){$\number\numexpr\br*(\br+1)/2$~squares}
\end{pspicture}


\bigskip
\newcommand\tilehexagon{%------  this prints a hexagon  ------
  \pspolygon[showpoints](!3 sqrt  1.5)(!3 sqrt 2 div  2)(!0       1.5)%
                        (!0       0.5)(!3 sqrt 2 div  0)(!3 sqrt  0.5)
}
\begin{pspicture}(\numexpr2*\br,\numexpr2*\br)
\multido{\iRow=0+1}{\br}{%
  \multido{\iCol=1+1}{\numexpr\iRow+1}{%
    \rput(!\iCol\space 2 mul \iRow\space sub  3 sqrt mul 2 div
           \iRow\space 1.5 mul){\tilehexagon}
  }
}
\rput[l](!3.4 \br\space 3 div add  0.9){$\number\numexpr\br*(\br+1)/2$~hexagons}
\end{pspicture}
%
%
\hfill
\newcommand\tilecircle{%------  this prints a circle  ------
  \pscircle(0,0){1}
}
\begin{pspicture}(\numexpr2*\br,\numexpr2*\br)
\multido{\iRow=0+1}{\br}{%
  \multido{\iCol=1+1}{\br}{%
    \rput(!\iCol\space 2 mul \iRow\space 2 mod sub
           \iRow\space 3 sqrt mul){\tilecircle}
  }
}
\rput[l](!\br\space 2 mul 0.5 add  1.5){$\number\numexpr\br*\br$~circles}
\end{pspicture}
\end{document}
8
  • Very nice idea! Commented Apr 20, 2013 at 4:21
  • It seems like not all lines are of equal thickness though. Maybe because the top and bottom lines of inverted, resp. non-inverted, triangles are overlapping in some points and not in others.
    – Mythio
    Commented Apr 22, 2013 at 12:34
  • @Mythio Possibly lines might look not equal thick but that is due to the screenshot. Look at the pstricks code and my explanation in the update: 1) all lines have same thickness; 2) And no, there is no overlap of lines (neither top nor bottom nor other lines) of the triangles. For the newly added figures some lines overlap but still there is no problem...
    – e-birk
    Commented Apr 23, 2013 at 16:07
  • @e-birk: there is no need for defining nodes. A \pspolygon[showpoints](...) does the same.
    – user2478
    Commented Apr 23, 2013 at 16:46
  • Points shown by showpoints option are a bit strange because they have a pipe when we highlight them inside a PDF viewer. See this. Commented Apr 23, 2013 at 19:18
4

For once, here is a TikZ solution to a PSTricks question:

enter image description here

\documentclass{article}
\usepackage{tikz}
\newcommand{\triangdiag}[1]{%
% the argument corresponds to the number of vertices on the outer row
    \begin{tikzpicture}[thick]
        \def\a{1}           % length of a triangle edge
        \def\r{.15}         % radius of the dots
        \foreach \j in {1,...,#1}{%
            % Draw dots
            \def\M{\the\numexpr#1-\j+1}
            \foreach \i in {1,...,\M}{%     
                \fill ({(.5*(\j-1)+(\i-1))*\a},{(1-\j)*.5*sqrt(3)*\a}) circle (\r);
            }
            % Draw lines
            \draw   ({(.5*(\j-1)},{(1-\j)*.5*sqrt(3)*\a}) --
                    ++({(\M-1)*\a},0);
            \draw   ({((\j-1))*\a},0) --
                    ({.5*(#1+\j-2)*\a},{(\j-#1)*.5*sqrt(3)*\a});
            \draw   ({(#1-\j)*\a},0) -- 
                    ({.5*(#1-\j)*\a},{(\j-#1)*.5*sqrt(3)*\a});
        }
    \end{tikzpicture}
}

\begin{document}
\triangdiag{5} \qquad \triangdiag{8}
\end{document}
3
  • why do you use the \pgfmath... which are not needed?? Simple macros and \ numexpr is faster,
    – user2478
    Commented Apr 22, 2013 at 6:01
  • Out of habit, I suppose. I'm not that used to \numexpr yet.
    – jub0bs
    Commented Apr 22, 2013 at 10:14
  • I edited your answer to see what I mean. Please delete it later.
    – user2478
    Commented Apr 22, 2013 at 10:17
4

With a recursive method but it works only for positive numbers less than 8. TeX main memory capacity is not enough to generate more than 7. Please let me know how to fix it!

Single:

enter image description here

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

\usepackage{multido}
\usepackage{fp}

\def\Atom#1{%
    \ifnum#1=1\relax
        \pspolygon(0,0)(!.5 dup 3 sqrt mul neg)(1,0)
    \else
        \sbox0{\expandafter\Atom\expandafter{\the\numexpr#1-1\relax}}%
        \rput(0,0){\usebox0}
        \rput{-60}(!2 #1 2 sub exp 0){\usebox0}
        \rput(!2 #1 2 sub exp 0){\usebox0}
        \rput(!2 #1 3 sub exp dup 3 sqrt mul neg){\usebox0}
    \fi
}

\def\Molecule#1{%
    \FPeval\Wd{pow(#1-1,2)}
    \FPeval\Ht{Wd/2*root(2,3)}
    \begin{pspicture}(\Wd,-\Ht)
    \Atom{#1}
    \end{pspicture}}

\begin{document}
\Molecule{5}
\end{document}

Multiple:

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{pstricks}
\SpecialCoor
\psset{showpoints=true,unit=2.5mm}

\usepackage{multido}
\usepackage{fp}

\def\Atom#1{%
    \ifnum#1=1\relax
        \pspolygon(0,0)(!.5 dup 3 sqrt mul neg)(1,0)
    \else
        \sbox0{\expandafter\Atom\expandafter{\the\numexpr#1-1\relax}}%
        \rput(0,0){\usebox0}
        \rput{-60}(!2 #1 2 sub exp 0){\usebox0}
        \rput(!2 #1 2 sub exp 0){\usebox0}
        \rput(!2 #1 3 sub exp dup 3 sqrt mul neg){\usebox0}
    \fi
}

\def\Molecule#1{%
    \FPeval\Wd{pow(#1-1,2)}
    \FPeval\Ht{Wd/2*root(2,3)}
    \begin{pspicture}(\Wd,-\Ht)
    \Atom{#1}
    \end{pspicture}}

\begin{document}
\multido{\i=1+1}{6}{\Molecule{\i}\quad}
\end{document}
1
  • 3
    Pardon the pun, but this seems like a typical case of... stack overflow!
    – jub0bs
    Commented Apr 27, 2013 at 3:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .