这个问题和这个问题类似tikz:均匀随机分布圆圈(这也是我获得这张 TiKz 图片的部分代码的地方)但有所不同。我希望绘制多个不重叠的圆圈,但其中一些圆圈不同。目前这是我的代码:
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\newcommand{\fillrandomly}[5]{
\xdef\xlist{4}
\xdef\ylist{4}
\pgfmathsetmacro\diametersqr{(#3*2)^2}
\foreach \i in {1,...,#4}{
\foreach \k in {1,...,20}{
\pgfmathsetmacro\x{rnd*#1}
\pgfmathsetmacro\y{rnd*#2}
\xdef\collision{0}
\foreach \element [count=\i] in \xlist{
\pgfmathtruncatemacro\j{\i-1}
\pgfmathsetmacro\checkdistancesqr{ ( ({\xlist}[\j]-(\x))^2 + ({\ylist}[\j]-(\y))^2 ) }
\ifdim\checkdistancesqr pt<\diametersqr pt
\xdef\collision{1}
\breakforeach
\fi
}
\ifnum\collision=0
\xdef\xlist{\xlist,\x}
\xdef\ylist{\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}
生成如下图像: 我不确定如何停止三种不同类型的圆圈之间的重叠,尤其是从左边数的第一个和第三个烧杯。请 XeLaTeX 回答
答案1
发生这种情况的原因很明显:你抽取两个或更多个独立样本,因此如果你说
\fillrandomly{3.6}{3.8}{0.15}{12}{$-$}
\fillrandomly{3.6}{3.8}{0.15}{12}{$+$}
+ 离子不知道 - 离子。但修复它非常简单。只需将碰撞列表的重置设为不属于 的一部分\fillrandomly
,但每当您开始新组时都执行此操作。也就是说,我重新定义了它,使其不会重置碰撞列表,并添加了发出重置的\fillrandomly
命令。我还定义了一种在过程中会发生变化的样式,以便可以添加已填充的离子。\resetlists
ion
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\newcommand{\resetlists}{\xdef\xlist{4}\xdef\ylist{4}}
\newcommand{\fillrandomly}[5]{
\pgfmathsetmacro\diametersqr{(#3*2)^2}
\foreach \i in {1,...,#4}{
\foreach \k in {1,...,20}{
\pgfmathsetmacro\x{rnd*#1}
\pgfmathsetmacro\y{rnd*#2}
\xdef\collision{0}
\foreach \element [count=\i] in \xlist{
\pgfmathtruncatemacro\j{\i-1}
\pgfmathsetmacro\checkdistancesqr{ ( ({\xlist}[\j]-(\x))^2 + ({\ylist}[\j]-(\y))^2 ) }
\ifdim\checkdistancesqr pt<\diametersqr pt
\xdef\collision{1}
\breakforeach
\fi
}
\ifnum\collision=0
\xdef\xlist{\xlist,\x}
\xdef\ylist{\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}