# How generate random ball in a specific space with tikz?

I created a tikz picture as following :

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1.5]
\draw[blue,thin,->,dashed] (0,-1.5) -- (0,1.5)node[blue,right]{\scriptsize $y_{RP}$};
\draw[blue,thin,->,dashed] (-2.5,0) -- (2.5,0)node[blue,below]{\scriptsize $x_{RP}$};
\draw[blue,dashed] (-0.6,0) circle (1);
\draw[blue,dashed] (0.6,0) circle (1);
\fill[inner color=black,outer color=green,dashed] (0.1,0.1) circle (0.05);
\draw[red,rotate=10] (0,0) ellipse (0.3cm and 0.6cm);
\draw[red,->,rotate=10] (0,0) -- (2.5,0)node[above,rotate=10,red]{\scriptsize $x_{PP}$};
\draw[red,->,rotate=10] (0,0) -- (0,1.5)node[left,rotate=10,red]{\scriptsize $y_{PP}$};
\end{tikzpicture}
\end{document}


Schematicely reproduce by :

I want put a number (I don't know the exact number but maybe 10-15) balls in a random place to fill the red ellipse. I know that I can do a ball with \usetikzlibrary{shapes} and \shade [ball color=green] (0.1,0.1) circle [radius=0.05cm]; for example but I put just here a circle for example.

Can we do that with tikz ? How we can do that ? Thanks,

This is really just a question of what you need to do to place a point randomly inside the ellipse. If you use a parametric equation then you should choose a random angle and random lengths along the two axes and then you have a random point.

Edit

In fact, as there are only two coordinates it is better to choose polar coordinates randomly and then rescale to the ellipse.

Here is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math, shapes}

\newcommand\RandomPointInEllipse[3]{% \RandomPointInEllipse[a][b][rotation]
% parametrically, the ellipse is (a\cos u, b\sin u), so randomly
% choose polar coordinates (r,u) and then rescale to ellipse
\pgfmathsetmacro\uRnd{random(0,360)}
\pgfmathsetmacro\rRnd{random}
\pgfmathsetmacro\aRnd{#1*\rRnd*cos(\uRnd)}
\pgfmathsetmacro\bRnd{#2*\rRnd*sin(\uRnd)}
}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\draw[blue,thin,->,dashed] (0,-1.5) -- (0,1.5)node[blue,right]{\scriptsize $y_{RP}$};
\draw[blue,thin,->,dashed] (-2.5,0) -- (2.5,0)node[blue,below]{\scriptsize $x_{RP}$};
\draw[blue,dashed] (-0.6,0) circle (1);
\draw[blue,dashed] (0.6,0) circle (1);
\fill[inner color=black,outer color=green,dashed] (0.1,0.1) circle (0.05);
\draw[red,rotate=10] (0,0) ellipse (0.3cm and 0.6cm);
\draw[red,->,rotate=10] (0,0) -- (2.5,0)node[above,rotate=10,red]{\scriptsize $x_{PP}$};
\draw[red,->,rotate=10] (0,0) -- (0,1.5)node[left,rotate=10,red]{\scriptsize $y_{PP}$};
\foreach \ball in {1,...,10} { \RandomPointInEllipse{0.3}{0.6}{10}}
\end{tikzpicture}
\end{document}

• This is a very proper solution, I like it, because I want ball inside and not on the ellipse, I just changed \pgfmathsetmacro\aRnd{0.3*rand*cos(\uRnd)} and \pgfmathsetmacro\bRnd{0.6*rand*sin(\uRnd)} by \pgfmathsetmacro\aRnd{0.25*rand*cos(\uRnd)} and \pgfmathsetmacro\bRnd{0.55*rand*sin(\uRnd)} Commented Nov 1, 2017 at 16:48
• @G.Sophys Actually, I realised that I was over doing it as there are only two degrees of freedom. It is better to randomly choose polar coordinates inside the unit circle and then rescale to the ellipse.
– user30471
Commented Nov 1, 2017 at 16:58

Something like that could help...

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\begin{document}
\begin{tikzpicture}[
]
\begin{scope}[rotate=10]
\draw[red] (0,0) ellipse (0.3 and 0.6);
\foreach \x in {1,...,10} {
}
\end{scope}
\end{tikzpicture}
\end{document}


...the tricky part is the mix of coordinates in absolute values and the x and y of tikz.

• I did know the rand variables but it works well, when I try with more balls (20), I saw one ball outside the ellispe, I changed 0.25*rand, 0.55*rand by 0.2*rand, 0.5*rand. Thanks for the answer Commented Nov 1, 2017 at 16:45

with use of clip:

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
\begin{tikzpicture}[scale=1.5,
N/.style   = {rotate=#1, font=\scriptsize},
N/.default = 0,
L/.style   = {blue, dashed, ->}
]
\draw[L,thin]   ( 0.0,-1.5) -- (0,1.5) node[N,right] {$y_{RP}$};
\draw[L,thin]   (-2.5, 0.0) -- (2.5,0) node[N,below] {$x_{RP}$};
\draw[blue]     (-0.6, 0.0) circle (1);
\draw[blue]     ( 0.6, 0.0) circle (1);
\draw[red,->,rotate=10] (0,0) -- (2.5,0) node[N=10,above] {$x_{PP}$};
\draw[red,->,rotate=10] (0,0) -- (0,1.5) node[N=10,left]  {$y_{PP}$};
%
\begin{scope}[red,rotate=10]
\draw[clip] (0,0) ellipse (0.3cm and 0.6cm);
\foreach \i in {1,...,30}
\fill[ball color=green] (rand*360:rand/2) circle (0.25mm);
\end{scope}
\end{tikzpicture}
\end{document}


In Metapost using a transformation to keep the ellipse within the overlap of the two circles, and to place the balls neatly inside it.

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

save c; path c, c';
c = fullcircle scaled 2r;
c' = fullcircle scaled 1/32 r shifted 1/2(r,0) rotated light_angle;
image(
for s=0 step 1/64 until 1:
endfor
)
enddef;

beginfig(1);

numeric u; u = 42;

path xx, xx', yy, yy', C, D, E;

xx = (left -- right) scaled 4u;
yy = (down -- up) scaled 3u;
xx' = subpath (1/2, 1) of xx rotated 12;
yy' = subpath (1/2, 1) of yy rotated 12;

numeric d; d = .8u;
C = fullcircle scaled 4u shifted (d,0);
D = fullcircle scaled 4u shifted (-d,0);

z0 = xx' intersectionpoint D;
z1 = yy' intersectionpoint C;

transform T;
origin transformed T = origin;
right transformed T = 3/4 z0;
up transformed T = 3/4 z1;
E = fullcircle scaled 2 transformed T;

drawoptions(dashed withdots scaled 1/2 withcolor 2/3 blue);
drawarrow xx; drawarrow yy; draw C; draw D;
label.rt("$x_{RP}$", point 1 of xx);
label.urt("$y_{RP}$", point 1 of yy);

drawoptions(withcolor 3/4 red);
drawarrow xx'; drawarrow yy'; draw E;
label.urt("$x_{PP}$", point 1 of xx');
label.ulft("$y_{PP}$", point 1 of yy');

drawoptions();

picture b; b = ball(2.4, 135, 3/4 green, red+green, 3/4 green);

for i=1 upto 20:
draw b shifted (uniformdeviate 7/8 * dir uniformdeviate 360 transformed T);
endfor

endfig;
\end{mplibcode}
\end{document}


This is wrapped up in luamplib so compile with lualatex, or adapt for plain MP or for gmp.