1

This question is vagely related to https://math.stackexchange.com/questions/3449433/selecting-cards-to-form-a-fair-game?noredirect=1#comment7090962_3449433. I am trying to visually illustrate the ways one can draw 3 balls from a pile of N+M balls, where N is the number of blue and M is the number of green.

My attempt at doing this in tikz can be seen below. Here I first arrange the balls in order, and then show how they appear when one ball is removed. A line between two balls indicates that these two balls are choosen PLUS the dotted ball.

enter image description here

There are some changes I would like to do however.

  1. So we pick 3 balls, the two connected by the line PLUS the dotted one. If these three balls have the same color, I want the line connecting them to be in style X (dashed, or red or etc)
  2. If the three balls have different colors, I want the line connecting them to be in style Y (solid line, orange, or etc)
  3. Why does my code fail for the particular case 2?
  4. Is there a way to make the code work for different number of blue and green balls?
  5. How can I pick the rotation of the center polygon so that it matches with the corners of the outer one? Now I changed the inner one to manually fit.
  6. Is there a particular reason the code below does not work?

    \foreach \x in {1,...,\n}{
      \begin{scope}[shift={(b.corner \x)}]
        \missingPentagonTemp{2}{c};
      \end{scope}
    } 
    

Here is what I want it to look like (courtesy of paint).

enter image description here

MWE

\documentclass[a4paper,11pt,margin=5pt]{standalone}

\usepackage{tikz,xcolor}

\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{decorations}

\usepackage{ifthenx}

\newcommand{\missingPentagon}[2]{
    % draw edges
    \node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (#2) {};

    \def\n{6}

    \pgfmathsetmacro{\start}{int(min(#1+1, \n))};
    \pgfmathsetmacro{\stop}{int(max(#1-1, 2))};

    \ifthenelse{\equal{#1}{1}}{
         \foreach\x in{2,...,\n}{
            \foreach\y in{\x,...,\n}{
                    \draw[color = black, dashed](#2.corner \x)--(#2.corner \y);
            }
        }

        \foreach \x in {1}{
          \draw[fill=green!20,dashed](#2.corner \x) circle[radius=1em] node {\x};
         }

        \foreach \x in {2,...,\n}{
          \draw[fill=blue!20](#2.corner \x) circle[radius=1em] node {\x};
        }

    }{

        \foreach\x in {1,...,\stop}{
            \foreach\y in {\x,...,\n}{
                \ifthenelse{\equal{#1}{\y}}{}{
                    \draw[color = black, dashed](#2.corner \x) -- (#2.corner \y)};
            }
        }
        \foreach\x in {\start,...,\n}{
            \foreach\y in {\x,...,\n}{
                \ifthenelse{\equal{#1}{\y}}{}{
                    \draw[color = black, dashed](#2.corner \x) -- (#2.corner \y)};
            }
        }

        \foreach \x in {1}{
          \draw[fill=green!20](#2.corner \x) circle[radius=1em] node {\x};
         }

        \foreach \x in {2,...,\n}{
            \ifthenelse{\equal{#1}{\x}}{
                \draw[fill=blue!20,dashed](#2.corner \x) circle[radius=1em] node {\x};
            }{
                \draw[fill=blue!20](#2.corner \x) circle[radius=1em] node {\x};
          };
        }

}
}


\begin{document}

\begin{tikzpicture}

\def\n{6}

\node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (a) {};

\node[draw=none, minimum size=10cm, regular polygon, regular polygon sides = \n] (b) {};

\foreach \x in {4}{
  \draw[fill=green!20](a.corner \x) circle[radius=1em] node {1};
 }

\foreach \x in {1,...,3}{
    \pgfmathparse{int(\x+3)}
  \draw[fill=blue!20](a.corner \x) circle[radius=1em] node {\pgfmathresult};
}

\foreach \x in {5,...,6}{
    \pgfmathparse{int(\x-3)}
  \draw[fill=blue!20](a.corner \x) circle[radius=1em] node {\pgfmathresult};
}

%\foreach \x in {1,...,\n}{
%  \begin{scope}[shift={(b.corner \x)}]
%    \missingPentagonTemp{2}{c};
%  \end{scope}
%}

\begin{scope}[shift={(b.corner 1)}]
    \missingPentagon{4}{c};
\end{scope}
\begin{scope}[shift={(b.corner 2)}]
    \missingPentagon{5}{c};
\end{scope}
\begin{scope}[shift={(b.corner 3)}]
    \missingPentagon{6}{c};
\end{scope}
\begin{scope}[shift={(b.corner 4)}]
    \missingPentagon{1}{c};
\end{scope}
\begin{scope}[shift={(b.corner 5)}]
    \missingPentagon{2}{c};
\end{scope}
\begin{scope}[shift={(b.corner 6)}]
    \missingPentagon{3}{c};
\end{scope}

\end{tikzpicture}

\end{document}
4

This is an attempt to answer the question with a pic. This code checks whether or not a vertex is in the list of missing or highlighted vertices (yes, you can use lists now) and draws the edges accordingly.

As for your points:

  1. Done.
  2. Done. At this point the style is is solid. One can change it.
  3. Honestly, I did not try to think through the code. Sorry.
  4. Yes. Now these are lists.
  5. Changed. One may also just rotate the polygon, but I like mod conditions. ;-)
  6. Again, I did not try. Sorry.

Full code:

\documentclass[a4paper,11pt,margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\makeatletter
\pgfmathdeclarefunction{memberQ}{2}{%
  \begingroup%
    \edef\pgfutil@tmpb{0}%memberQ({\lstPast},\inow)
    \edef\pgfutil@tmpa{#2}%
    \expandafter\pgfmath@member@i#1\pgfmath@token@stop
    \edef\pgfmathresult{\pgfutil@tmpb}%
    \pgfmath@smuggleone\pgfmathresult%
  \endgroup}
\def\pgfmath@member@i#1{%
    \ifx\pgfmath@token@stop#1%
    \else
      \edef\pgfutil@tmpc{#1}%
      \ifx\pgfutil@tmpc\pgfutil@tmpa\relax%
      \gdef\pgfutil@tmpb{1}%
      \fi%
      \expandafter\pgfmath@member@i
    \fi}        
\makeatother
\tikzset{circ/.style={circle,minimum size=2em,draw},
pics/missing polygon/.style={code={
   \tikzset{missing polygon/.cd,#1}
   \def\pv##1{\pgfkeysvalueof{/tikz/missing polygon/##1}}
    \node[draw=none, minimum size=\pv{size}, 
        regular polygon, regular polygon sides =\pv{n}] (-poly) {};
    % test if the highlighted node is in the missin nodes
    \edef\lsthigh{\pv{highlight}}
    \foreach \XX in \lsthigh
    {\pgfmathtruncatemacro{\ktest}{memberQ(\pv{miss},\XX)}
    \xdef\pgfmathresult{\ktest}}
    \edef\ktest{\pgfmathresult}
    \foreach \XX in {1,...,\the\numexpr\pv{n}-1}
    {
     \foreach \YY in {2,...,\pv{n}}
     {
      \pgfmathtruncatemacro{\itest}{memberQ(\pv{miss},\XX)+memberQ(\pv{miss},\YY)}
      \ifnum\itest=0
       \pgfmathtruncatemacro{\jtest}{memberQ(\pv{highlight},\XX)+memberQ(\pv{highlight},\YY)}
       \draw \ifnum\jtest=0 [dashed]\fi \ifnum\ktest=1 [solid]\fi
       (-poly.corner \XX) -- (-poly.corner \YY);
      \fi
     }
    }   
    \foreach \XX in {1,...,\pv{n}}  
    {\pgfmathtruncatemacro{\itest}{memberQ(\pv{highlight},\XX)}
    \ifnum\itest=1
     \node[missing polygon/highlighted] (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
    \else
     \pgfmathtruncatemacro{\jtest}{memberQ(\pv{miss},\XX)}
     \ifnum\jtest=1
      \node[missing polygon/missing]  (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
     \else
      \node[missing polygon/regular] (-poly-vertex-\XX) at (-poly.corner \XX){\XX};
     \fi
    \fi
    }
}},missing polygon/.cd,size/.initial=3cm,n/.initial=6,
miss/.initial={2},highlight/.initial={1},
highlighted/.style={circ,fill=green!20},
missing/.style={circ,fill=blue!20,dashed},
regular/.style={circ,fill=blue!20}
}

\begin{document}

\begin{tikzpicture}

\def\n{6}

\node[draw=none, minimum size=3cm, regular polygon, regular polygon sides = \n] (a) {};

\node[draw=none, minimum size=10cm, regular polygon, regular polygon sides = \n] (b) {};


\foreach \X in {1,...,\n}{
  \pgfmathparse{int(1+Mod(\X-4,6))}
  \draw \ifnum\X=4 [fill=green!20] \else  [fill=blue!20]\fi
   (a.corner \X) circle[radius=1em] node {\pgfmathresult};
}

\path foreach \X in {1,...,\n}
{ [/utils/exec=\pgfmathtruncatemacro{\mymiss}{int(1+Mod(\X-4,6))}] 
    (b.corner \X) pic(m\X){missing polygon={miss={\mymiss}}}};

\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |
  • Wow, this looks fantastic. Never knew about lists! Regards to the lower left one it would be great if all the lines were solid, and the circle around the 1 should be dashed (minor detail). Btw I tried to keep the dashed circles towards the center, any particular reason you did not? (I am still digesting your great code) – N3buchadnezzar Nov 27 '19 at 20:58
  • @N3buchadnezzar OK, I updated accordingly. – user194703 Nov 27 '19 at 21:44
  • I updated your code slightly, so that it better fits my aim. Rollback if you are not happy. I only changed the mod part to change where the dashed balls appear =) – N3buchadnezzar Nov 27 '19 at 22:31
  • @N3buchadnezzar Thanks! – user194703 Nov 27 '19 at 22:44

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.