3

I am trying to recreate the following image in latex. enter image description here

Where I want to vary the number of white and black balls. As indicated in the title, I am also trying to draw a line between balls of same colour and a dashed line of balls with opposite color. I have tried to do this in the MWE below. However I am unsure how to get latex to know whether to draw a dashed or single line between two balls. Any ideas?

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}
% create the node

\pgfmathsetmacro\white{3}
\pgfmathsetmacro\black{2}
\pgfmathsetmacro\n{\white+\black}

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

% draw a black dot in each vertex
\foreach \x in {1,2,...,\n}
  \fill (a.corner \x) circle[radius=2.2pt];

% draw a black dot in each vertex
\foreach \x in {1,2,...,\white}
  \fill[color=white] (a.corner \x) circle[radius=2pt];

\end{tikzpicture}
\end{document}
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114
  • Do you want LaTeX automatically decide which line to use? In any case, between which balls do you want lines? – Ignasi Jan 27 '16 at 15:18
  • All the balls. Dashed between different colored balls, and a single line between same colored balls. I can add a second image for clearification =) – N3buchadnezzar Jan 27 '16 at 15:51
  • If you create separate lists of black and while balls then one can easily process black to black, white to white, and black to white. – John Kormylo Jan 27 '16 at 16:05

1 Answers1

2

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}
% create the node

\pgfmathsetmacro\white{9}
\pgfmathsetmacro\black{4}
\pgfmathsetmacro\whiteplusone{int(\white+1)}
\pgfmathsetmacro\n{\white+\black}

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

% draw edges
\foreach\x in{1,...,\n}{
    \foreach\y in{\x,...,\n}{
        \pgfmathsetmacro\tempdimen{(\x-\white-.5)*(\y-\white-.5)}
        \ifdim\tempdimen pt>0pt
            \draw(a.corner \x)--(a.corner \y);
        \else
            \draw[dashed](a.corner \x)--(a.corner \y);
        \fi
    }
}

% draw a white dot in each vertex
\foreach \x in {1,...,\white}
  \fill (a.corner \x) circle[radius=2pt];

% draw a black dot in each vertex
\foreach \x in {\whiteplusone,...,\n}
  \draw[fill=white](a.corner \x) circle[radius=2pt];

\end{tikzpicture}
\end{document}

Just saying... probably you need this next.

Symbol 1
  • 36,855
  • Could you add an explanation on how your code works? I mean the % draw edges section. – Alenanno Jan 28 '16 at 12:46
  • @Alenanno If you want to check if \x and \y are of different color, it is equivalent to check if exactly one lies in interval [0,\white] and the other one lies in interval [\white+1,\n]. The later is further equivalent to check if (\x-\white-.5)(\y-white-.5) is negative, hence the code. – Symbol 1 Jan 28 '16 at 12:55