4

I would like to represent the construction of a house of cards, probably in tikz. Before creating my own, has anyone done anything similar?

A 3 storey house of cards represented by lines

  • Related: Second pentagonal number (OEIS #A005449: Number of cards to build an n-tier house of cards) – Werner Nov 14 at 23:32
  • This doesn't seem like it would take all that much to make a first attempt (probably not as elegant or fun as the given answer, but it would be a first attempt). Is the counting question about 20 stories intended to be a part of the question? @Werner I'm not seeing how the OEIS reference matches with this picture. If you add the bottom three cards, it would seem like this is 3 times a triangular number: 3n(n+1)/2. – Teepeemm Nov 15 at 3:08
  • @Teepeemm: For n = 3, 3n(n+1)/2 = 18, while OEIS #A005449's formula of n(3n+1)/2 = 15 in the post. – Werner Nov 15 at 5:31
21
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
\draw (-0.45,0.1) -- (0,0.9) -- (0.45,0.1) (-0.45,0) -- (0.45,0);}}]
 \path foreach \Y in {1,2,3}
  {foreach \X in {1,...,\Y} {(\X-\Y/2,1-\Y) pic{Lam}}};
\end{tikzpicture}
\end{document}

enter image description here

Here is a version (more faithful to the original post) without lines at the bottom and much more. You only need to say

\path pic{card house=3};

See the examples in

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
    \def\pkv##1{\pgfkeysvalueof{/tikz/Lam/##1}}
    \draw (-0.5*\pkv{shrink}*\pkv{width},0.1) -- 
    (0,\pkv{shrink}*\pkv{height}) -- (0.5*\pkv{shrink}*\pkv{width},0.1) 
     \ifnum#1<\pkv{cutoff} (-0.5*\pkv{shrink}*\pkv{width},0) -- (0.5*\pkv{shrink}*\pkv{width},0)
     \fi;}},
    pics/card house/.style={code={
     \tikzset{Lam/cutoff=#1}
     \path foreach \Y in {1,...,#1}
      {foreach \X in {1,...,\Y} 
       {(\X*\pgfkeysvalueof{/tikz/Lam/width}-\Y*\pgfkeysvalueof{/tikz/Lam/width}/2,
       -\Y*\pgfkeysvalueof{/tikz/Lam/height}) pic{Lam=\Y}}};
    }},  
    /tikz/Lam/.cd,cutoff/.initial=3,width/.initial=1,height/.initial=1,shrink/.initial=0.9,
    ]
 \path pic{card house=3} (4,0) pic[red]{card house=4} (9,0) pic[blue,thick]{card house=5};  
\end{tikzpicture}
\end{document}

enter image description here

What could this be good for? Oh, of course!

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikzlings}
\newif\iftikzling
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
    \def\pkv##1{\pgfkeysvalueof{/tikz/Lam/##1}}
    \draw (-0.5*\pkv{shrink}*\pkv{width},0.1) -- 
    (0,\pkv{shrink}*\pkv{height}) -- (0.5*\pkv{shrink}*\pkv{width},0.1) 
     \ifnum#1<\pkv{cutoff} (-0.5*\pkv{shrink}*\pkv{width},0) -- (0.5*\pkv{shrink}*\pkv{width},0)
     \fi;
     \iftikzling
     \tikzling[scale=0.25*\pkv{height}*\pkv{shrink}]
     \fi}},
    pics/card house/.style={code={
     \tikzset{Lam/cutoff=#1}
     \path foreach \Y in {1,...,#1}
      {foreach \X in {1,...,\Y} 
       {(\X*\pgfkeysvalueof{/tikz/Lam/width}-\Y*\pgfkeysvalueof{/tikz/Lam/width}/2,
       -\Y*\pgfkeysvalueof{/tikz/Lam/height}) pic{Lam=\Y}}};
    }},  
    /tikz/Lam/.cd,cutoff/.initial=3,width/.initial=1,height/.initial=1,shrink/.initial=0.9,
    tikzling/.is if=tikzling
    ]
 \path[/tikz/Lam/.cd,height=3,width=3,tikzling=true]  pic[blue,thick]{card house=5};  
\end{tikzpicture}
\end{document}

enter image description here

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.