How to stop overlap between different circles randomly generated











up vote
5
down vote

favorite
1












This question is similar to this question tikz: Distribute evenly and randomly circles (which is also where I got some of the code for this TiKz picture) but is different. I wish to draw multiple circles that do not overlap but some of these circles are different. Currently this is my code:



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{fillrandomly}[5]{
xdefxlist{4}
xdefylist{4}
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
end{scope}
foreach X in {1,...,8}
{
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}
begin{scope}[xshift=15cm]
foreach X in {1,...,2}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


Which produces this image:
enter image description here
I am unsure how to stop overlapping between the three different types of circles especially in the first and third beaker counting from the left. XeLaTeX answer please










share|improve this question
























  • Lualatex ok?...
    – JPi
    2 hours ago










  • Sorry I prefer XeLaTeX, forgot to mention
    – sab hoque
    2 hours ago















up vote
5
down vote

favorite
1












This question is similar to this question tikz: Distribute evenly and randomly circles (which is also where I got some of the code for this TiKz picture) but is different. I wish to draw multiple circles that do not overlap but some of these circles are different. Currently this is my code:



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{fillrandomly}[5]{
xdefxlist{4}
xdefylist{4}
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
end{scope}
foreach X in {1,...,8}
{
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}
begin{scope}[xshift=15cm]
foreach X in {1,...,2}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


Which produces this image:
enter image description here
I am unsure how to stop overlapping between the three different types of circles especially in the first and third beaker counting from the left. XeLaTeX answer please










share|improve this question
























  • Lualatex ok?...
    – JPi
    2 hours ago










  • Sorry I prefer XeLaTeX, forgot to mention
    – sab hoque
    2 hours ago













up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





This question is similar to this question tikz: Distribute evenly and randomly circles (which is also where I got some of the code for this TiKz picture) but is different. I wish to draw multiple circles that do not overlap but some of these circles are different. Currently this is my code:



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{fillrandomly}[5]{
xdefxlist{4}
xdefylist{4}
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
end{scope}
foreach X in {1,...,8}
{
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}
begin{scope}[xshift=15cm]
foreach X in {1,...,2}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


Which produces this image:
enter image description here
I am unsure how to stop overlapping between the three different types of circles especially in the first and third beaker counting from the left. XeLaTeX answer please










share|improve this question















This question is similar to this question tikz: Distribute evenly and randomly circles (which is also where I got some of the code for this TiKz picture) but is different. I wish to draw multiple circles that do not overlap but some of these circles are different. Currently this is my code:



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{fillrandomly}[5]{
xdefxlist{4}
xdefylist{4}
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
end{scope}
foreach X in {1,...,8}
{
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}
begin{scope}[xshift=15cm]
foreach X in {1,...,2}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw[fill=black!10] (2+1.8*rand,2+1.9*rand) circle (0.15);
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


Which produces this image:
enter image description here
I am unsure how to stop overlapping between the three different types of circles especially in the first and third beaker counting from the left. XeLaTeX answer please







tikz-pgf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 2 hours ago









sab hoque

1,250318




1,250318












  • Lualatex ok?...
    – JPi
    2 hours ago










  • Sorry I prefer XeLaTeX, forgot to mention
    – sab hoque
    2 hours ago


















  • Lualatex ok?...
    – JPi
    2 hours ago










  • Sorry I prefer XeLaTeX, forgot to mention
    – sab hoque
    2 hours ago
















Lualatex ok?...
– JPi
2 hours ago




Lualatex ok?...
– JPi
2 hours ago












Sorry I prefer XeLaTeX, forgot to mention
– sab hoque
2 hours ago




Sorry I prefer XeLaTeX, forgot to mention
– sab hoque
2 hours ago










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










It is clear why this happens: you draw two or more independent samples, so if you say e.g.



fillrandomly{3.6}{3.8}{0.15}{12}{$-$} 
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}


the + ions do not know about the - ones. But it is very simple to fix it. Just make the reset of the collision list not part of the fillrandomly, but do it whenever you start a new group. That is, I redefined fillrandomly such that it does not reset the collision list, and added a command resetlists that issues the reset. I also defined an ion style that gets changed along the way such that one can add the filled ions.



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{resetlists}{xdefxlist{4}xdefylist{4}}
newcommand{fillrandomly}[5]{
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw[ion] (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

% 2
begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}

% 3
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{8}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}

%4
begin{scope}[xshift=15cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{2}{$-$}
fillrandomly{3.6}{3.8}{0.15}{2}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{1}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


enter image description here






share|improve this answer





















  • I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
    – sab hoque
    56 mins ago






  • 2




    @sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
    – marmot
    51 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466069%2fhow-to-stop-overlap-between-different-circles-randomly-generated%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










It is clear why this happens: you draw two or more independent samples, so if you say e.g.



fillrandomly{3.6}{3.8}{0.15}{12}{$-$} 
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}


the + ions do not know about the - ones. But it is very simple to fix it. Just make the reset of the collision list not part of the fillrandomly, but do it whenever you start a new group. That is, I redefined fillrandomly such that it does not reset the collision list, and added a command resetlists that issues the reset. I also defined an ion style that gets changed along the way such that one can add the filled ions.



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{resetlists}{xdefxlist{4}xdefylist{4}}
newcommand{fillrandomly}[5]{
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw[ion] (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

% 2
begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}

% 3
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{8}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}

%4
begin{scope}[xshift=15cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{2}{$-$}
fillrandomly{3.6}{3.8}{0.15}{2}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{1}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


enter image description here






share|improve this answer





















  • I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
    – sab hoque
    56 mins ago






  • 2




    @sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
    – marmot
    51 mins ago















up vote
4
down vote



accepted










It is clear why this happens: you draw two or more independent samples, so if you say e.g.



fillrandomly{3.6}{3.8}{0.15}{12}{$-$} 
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}


the + ions do not know about the - ones. But it is very simple to fix it. Just make the reset of the collision list not part of the fillrandomly, but do it whenever you start a new group. That is, I redefined fillrandomly such that it does not reset the collision list, and added a command resetlists that issues the reset. I also defined an ion style that gets changed along the way such that one can add the filled ions.



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{resetlists}{xdefxlist{4}xdefylist{4}}
newcommand{fillrandomly}[5]{
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw[ion] (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

% 2
begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}

% 3
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{8}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}

%4
begin{scope}[xshift=15cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{2}{$-$}
fillrandomly{3.6}{3.8}{0.15}{2}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{1}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


enter image description here






share|improve this answer





















  • I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
    – sab hoque
    56 mins ago






  • 2




    @sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
    – marmot
    51 mins ago













up vote
4
down vote



accepted







up vote
4
down vote



accepted






It is clear why this happens: you draw two or more independent samples, so if you say e.g.



fillrandomly{3.6}{3.8}{0.15}{12}{$-$} 
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}


the + ions do not know about the - ones. But it is very simple to fix it. Just make the reset of the collision list not part of the fillrandomly, but do it whenever you start a new group. That is, I redefined fillrandomly such that it does not reset the collision list, and added a command resetlists that issues the reset. I also defined an ion style that gets changed along the way such that one can add the filled ions.



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{resetlists}{xdefxlist{4}xdefylist{4}}
newcommand{fillrandomly}[5]{
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw[ion] (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

% 2
begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}

% 3
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{8}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}

%4
begin{scope}[xshift=15cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{2}{$-$}
fillrandomly{3.6}{3.8}{0.15}{2}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{1}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


enter image description here






share|improve this answer












It is clear why this happens: you draw two or more independent samples, so if you say e.g.



fillrandomly{3.6}{3.8}{0.15}{12}{$-$} 
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}


the + ions do not know about the - ones. But it is very simple to fix it. Just make the reset of the collision list not part of the fillrandomly, but do it whenever you start a new group. That is, I redefined fillrandomly such that it does not reset the collision list, and added a command resetlists that issues the reset. I also defined an ion style that gets changed along the way such that one can add the filled ions.



documentclass{article}
usepackage{tikz}
usepackage[margin=0.5in]{geometry}
newcommand{resetlists}{xdefxlist{4}xdefylist{4}}
newcommand{fillrandomly}[5]{
pgfmathsetmacrodiametersqr{(#3*2)^2}
foreach i in {1,...,#4}{
foreach k in {1,...,20}{
pgfmathsetmacrox{rnd*#1}
pgfmathsetmacroy{rnd*#2}
xdefcollision{0}
foreach element [count=i] in xlist{
pgfmathtruncatemacroj{i-1}
pgfmathsetmacrocheckdistancesqr{ ( ({xlist}[j]-(x))^2 + ({ylist}[j]-(y))^2 ) }
ifdimcheckdistancesqr pt<diametersqr pt
xdefcollision{1}
breakforeach
fi
}
ifnumcollision=0
xdefxlist{xlist,x}
xdefylist{ylist,y}
draw[ion] (x,y) circle [radius=#3] node[anchor=center] {#5};
breakforeach
fi
}
}
}
begin{document}
begin{figure}
begin{tikzpicture}
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated strong acid};

% 2
begin{scope}[xshift=5cm]
foreach X in {1,...,3}
{draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$+$};
draw (2+1.8*rand,2+1.9*rand) circle (0.15) node[anchor=center] {$-$};}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute strong acid};
end{scope}

% 3
begin{scope}[xshift=10cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{8}{$-$}
fillrandomly{3.6}{3.8}{0.15}{8}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{8}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {concentrated weak acid};
end{scope}

%4
begin{scope}[xshift=15cm]
begin{scope}[xshift=0.2cm,yshift=0.2cm]
resetlists
tikzset{ion/.style={}}
fillrandomly{3.6}{3.8}{0.15}{2}{$-$}
fillrandomly{3.6}{3.8}{0.15}{2}{$+$}
tikzset{ion/.style={fill=black!10}}
fillrandomly{3.6}{3.8}{0.15}{1}{}
end{scope}
draw (0,5) -- (0,1);
draw (0,1) .. controls (0,0) and (0,0) .. (1,0);
draw (1,0) -- (3,0);
draw (3,0) .. controls (4,0) and (4,0) .. (4,1);
draw (4,1) -- (4,5);
draw[densely dashed] (0,5) .. controls (0,4.5) and (0,4.5) .. (0.5,4.5);
draw[densely dashed] (0.5,4.5) -- (3.5,4.5);
draw[densely dashed] (3.5,4.5) .. controls (4,4.5) and (4,4.5) .. (4,5);
node[anchor=center] at (2,-0.5) {dilute weak acid};
end{scope}
draw[fill=black!10] (0,-1) circle (0.15) node[right=0.1cm] {Unionised};
draw (0,-1.5) circle (0.15) node[anchor=center] {$+$} node[right=0.1cm] {Positive Ion};
draw (0,-2) circle (0.15) node[anchor=center] {$-$} node[right=0.1cm] {Negative Ion};
end{tikzpicture}
vspace{-2cm}
caption{Concentrated and dilute solutions of weak and strong acids}
end{figure}
end{document}


enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









marmot

83.5k493178




83.5k493178












  • I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
    – sab hoque
    56 mins ago






  • 2




    @sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
    – marmot
    51 mins ago


















  • I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
    – sab hoque
    56 mins ago






  • 2




    @sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
    – marmot
    51 mins ago
















I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
– sab hoque
56 mins ago




I may have misunderstood something, but is the only thing that was changed was the addition of an ion style. I can't see where the cahnge was made to prevent the resetting of the list? I like to understand the code before I use it
– sab hoque
56 mins ago




2




2




@sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
– marmot
51 mins ago




@sabhoque No, the original code has xdefxlist{4}xdefylist{4} part of fillrandomly. This resets the collision list, i.e. the list of previous positions of ions. Whenever a new ion is placed, fillrandomly checks if the would-be ion overlaps with one of the previous ones. If not, the ion is drawn. All I did was not to reset the list automatically. So in the first example the + ions also know where the - ions were. I only reset the list when I switch to the next container. The ion style allows you to draw the shaded circles with fillrandomly, too.
– marmot
51 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466069%2fhow-to-stop-overlap-between-different-circles-randomly-generated%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei