0

This questtion is extending the already answered question: Randomly arrange images in beamer. Since the person posting the correct answer seems no longer active I am creating a new question.

The question is: Is there a way to make nodes not to overlap one another, yet still randomly distributed on the slide?

I have tried to find a structure to store the nodes to and then check for the overlaps and try to move them or shift them, but I did not find the right data structures. Starting to think this might not be possible.

0
1

Modifying https://tex.stackexchange.com/a/87518/190633 :

\documentclass{beamer}
\usepackage{tikz}

\def\xlist{4}
\def\ylist{4}

\newcommand{\fillrandomly}[4]{
    \pgfmathsetmacro\diameter{#3*2}
%    \draw (0,0) rectangle (#1,#2);
    \foreach \i in {1,...,#4}{
        \pgfmathsetmacro\x{rnd*#1}
        \pgfmathsetmacro\y{rnd*#2}
        \xdef\collision{0}
        \foreach \element [count=\i] in \xlist{
            \pgfmathtruncatemacro\j{\i-1}
            \pgfmathsetmacro\checkdistance{ sqrt( ({\xlist}[\j]-(\x))^2 + ({\ylist}[\j]-(\y))^2 ) }
            \ifdim\checkdistance pt<\diameter pt
                \xdef\collision{1}
                \breakforeach
            \fi
        }
        \ifnum\collision=0
            \xdef\xlist{\xlist,\x}
            \xdef\ylist{\ylist,\y}
            \node at (\x,\y) {\includegraphics[width=1cm]{example-image}};
%            \draw [red, thick] (\x,\y) circle [radius=#3];
        \fi 

    }
}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\pgfmathsetseed{2}
\fillrandomly{10}{5}{0.65}{100}

\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

1
  • I would accept it as an answer, but it does need fixed size to be passed to the macro and does not take the array of images as an input. The latter can be solved quite easily, but I did not manage to solve the first one. – Croolman Jun 12 '19 at 17:12

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.