Tikz 中的聚合状态:绘制随机圆圈并随机连接它们

Tikz 中的聚合状态:绘制随机圆圈并随机连接它们

嘿 TeX Stackexchange!

我在关于核聚变的演示\tikzpicture中遇到了一个问题。我想展示一个包含所有四种聚集状态的图形,包括作为第四种状态的等离子体。beamer

这大致就是我想要实现的目标: 四种聚集状态

我已经用带环的固体分子结构对第一个状态进行了编码foreach,但我不知道如何在其他三个状态下绘制随机圆圈。我可以——而且我确实知道如何——手工绘制所有单个圆圈并将它们连接在一起,这将花费很多精力和时间。

但我想要的是自动化在正方形内的随机坐标处绘制圆圈并且——在第二种状态下——随机连接它们。在等离子状态下,我也不想手工绘制每个粒子(定义额外的节点样式)。所以我也想在这里实现自动化。

在这个论坛上——我发现它真的很有帮助!——我找不到任何合适的问题。我确实查找了类似“均匀随机地分布圆圈“ 和 ”在TikZ中用随机点填充指定区域“但这些答案并不符合我的目的(它们甚至不符合第三种状态的要求,因为圆圈几乎遍布我的幻灯片)。

所以也许你们中有人可以帮助我解决这个问题,提前谢谢大家!

这是我目前拥有的代码:

\documentclass[12pt, aspectratio=169, xcolor=dvipsnames]{beamer}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{cmbright} 
\pdfmapfile{=hfbright.map}
\renewcommand*\sfdefault{lmss}
\renewcommand*\ttdefault{lmtt}
\usepackage{tikz}
\usetikzlibrary{calc, shapes.arrows, decorations.pathreplacing}


\usetheme{Luebeck}

\definecolor{bBlue}{rgb}{0.2,0.2,0.7}
\colorlet{bgColor}{blue!12!black}
\colorlet{lightyel}{Goldenrod!50}

\setbeamercolor*{normal text}{fg=white, bg=bgColor}

\setbeamercolor{background canvas}{bg=bgColor}
\setbeamercolor{background canvas}{bg=black!90!blue}
\setbeamercolor{block body}{fg=white, bg=bBlue!30!black}
\setbeamercolor{alerted text}{fg=orange}

\setbeamercolor{item projected}{fg=white}
\setbeamercolor{section in toc}{fg=white, bg=bBlue}
\setbeamercolor{section in toc shaded}
    {fg=bBlue!90, bg=bgColor}


%-----presentation-----%
\begin{document}

% many other slides...

\begin{frame}[t]{Plasma} \vspace{0.5cm}
\begin{itemize}[<+->] \pause
\item Atome überwinden magnetische Abstoßung $\Rightarrow$ viel Energie
\item \alert{Plasma} = 4. Aggregatzustand der Materie
\end{itemize}

\vfill

\pgfmathsetmacro{\length}{2.6}
\pgfmathsetmacro{\rad}{0.14}
\pgfmathsetseed{127}

\begin{center}
\begin{tikzpicture}
\begin{scope}[gray]
    \fill[white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Festkörper};
    
    \foreach \x in {1,...,6} {
        \foreach \y in {1,...,6} {
            \fill[] (\x * 0.37, \y * 0.37) circle (\rad);
        }
    }
    \draw[thick, step=0.37] (0.37, 0.37) grid (.37*6,.37*6);
\end{scope}

\begin{scope}[xshift=3.4cm, gray]
    \fill[white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Flüssigkeit};
    
%   \fillrandomly{\length - 0.4}{\length - 0.4}{\rad}{30}{0.37}{0.37}
    %@param {width}{height}{rad}{n}{start_x}{start_y}
\end{scope}

\begin{scope}[xshift=6.8cm, gray]
    \fill[white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Gas};
    
\end{scope}

\begin{scope}[xshift=10.2cm, gray]
    \fill[white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Plasma};
    
\end{scope}
\end{tikzpicture}
\end{center}

\vfill

\end{frame} 

\end{document}

来自代码的临时聚合状态


编辑:非常感谢 @Schrödinger 的猫快速回答。我对他的答案进行了一些小调整,更新了上述代码(现在应该可以正常工作,没有任何错误):

\documentclass[12pt, aspectratio=169, xcolor=dvipsnames]{beamer}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{cmbright} 
\pdfmapfile{=hfbright.map}
\renewcommand*\sfdefault{lmss}
\renewcommand*\ttdefault{lmtt}
\usepackage{tikz}
\usetikzlibrary{calc, shapes.arrows, decorations.pathreplacing}


\usetheme{Luebeck}

\definecolor{bBlue}{rgb}{0.2,0.2,0.7}
\colorlet{bgColor}{blue!12!black}
\colorlet{lightyel}{Goldenrod!50}

\setbeamercolor*{normal text}{fg=white, bg=bgColor}

\setbeamercolor{background canvas}{bg=bgColor}
\setbeamercolor{background canvas}{bg=black!90!blue}
\setbeamercolor{block body}{fg=white, bg=bBlue!30!black}
\setbeamercolor{alerted text}{fg=orange}

\setbeamercolor{item projected}{fg=white}
\setbeamercolor{section in toc}{fg=white, bg=bBlue}
\setbeamercolor{section in toc shaded}
    {fg=bBlue!90, bg=bgColor}


%---- code from Schrödinger's cat -----%

\tikzset{pics/random chains/.style={code={
\tikzset{random chains/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/random chains/##1}}%
\pgfmathsetmacro\xlist{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\ylist{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})} 
\draw [pic actions] (\xlist,\ylist) circle [radius=\pv{r}];
\foreach \XX in {1,...,\pv{n}}
 {\pgfmathsetmacro\x{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
  \pgfmathsetmacro\y{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
  \xdef\collision{0}
  \foreach \element [count=\YY starting from 0] in \xlist{
      \pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
      \ifdim\checkdistance pt<\pv{R} pt
          \xdef\collision{1}
          \breakforeach
      \fi
       } 
   \ifnum\collision=0
      \foreach \element [count=\YY starting from 0] in \xlist{
          \pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
          \ifdim\checkdistance pt<\pv{Rcrit} pt
            \draw[shorten >=\pv{r}*1cm,shorten <=\pv{r}*1cm] (\x,\y) --({{\xlist}[\YY]},{{\ylist}[\YY]});
          \fi
           }      
      \xdef\xlist{\xlist,\x}
      \xdef\ylist{\ylist,\y}
      \draw[pic actions]  (\x,\y) circle [radius=\pv{r}];
   \fi 
    }   
 }
},
random chains/.cd,n/.initial=50,% tries
xmax/.initial=2.6,% width
ymax/.initial=2.6,% height
r/.initial=0.14,%radius
R/.initial=0.45,% grace distance, should be greater than 2*r
Rcrit/.initial=0.6,% critical distance, closer objects will be connected
/tikz/molecules/.cd,
1/.style={draw=gray},
2/.style={}
}


%----- presentation -----%
\begin{document}

% many other slides...

\begin{frame}[t]{Plasma}

\vfill

\pgfmathsetmacro{\length}{2.6}
\pgfmathsetmacro{\rad}{0.14}

\begin{center}
\begin{tikzpicture}
\begin{scope}[gray]
    \fill[white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Festkörper};
    
    \foreach \x in {1,...,6} {
        \foreach \y in {1,...,6} {
            \fill[] (\x * 0.37, \y * 0.37) circle (\rad);
        }
    }
    \draw[thick, step=0.37] (0.37, 0.37) grid (.37*6,.37*6);
\end{scope}

 \path (0,3.5); % simple way to add some distance
 \pgfmathsetseed{496}
 
\begin{scope}[xshift=3.4cm]
    \draw[fill=white] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Flüssigkeit};
    \draw[] (0,0) pic[fill=gray,draw=gray]{random chains} rectangle ++(\length,\length);
\end{scope}
 
\begin{scope}[xshift=6.8cm]
     \draw[fill=white] (0,0) rectangle (\length,\length);   
     \node[white, above] at (\length / 2, \length) {\small Gas};
     \draw (0,0) pic[fill=gray]{random chains={Rcrit=0.3}} rectangle ++(\length,\length);
\end{scope} 
 
\begin{scope}[xshift=10.2cm]
    \draw[fill=white,] (0,0) rectangle (\length,\length);
    \node[white, above] at (\length / 2, \length) {\small Plasma};
    \draw (0,0) pic[fill=gray]{random chains={n=80,Rcrit=0.3}} rectangle ++(\length,\length);
    
    \foreach \X [count=\Z starting from 0] in \xlist {
        \pgfmathsetmacro{\Y}{{\ylist}[\Z]}
        \pgfmathrandominteger{\rin}{0}{4}
        \ifcase\rin
        \or
            \draw (\X,\Y)[fill=cyan] circle[radius=\rad] node[scale=0.8]{$+$};
        \or
            \draw (\X,\Y)[fill=green!70!black] circle[radius=\rad] node[scale=0.8]{$-$};
        \or
            \draw (\X,\Y)[fill=yellow] circle[radius=\rad] node[scale=0.8]{$*$};
        \or
            \path (\X,\Y)[fill=white] circle[radius=\rad] 
                node[fill=red,draw,inner sep=0.3pt,circle,scale=0.4]{$e^-$};
        \fi
    }
\end{scope}
\end{tikzpicture}
\end{center}

\vfill

\end{frame}

\end{document}

更新的聚合状态

答案1

这是基于杰克的回答但有一个问题:如果圆之间的距离小于临界值,Rcrit它们就会连接起来。它还以图片的形式实现了代码。参数由 pgf 键给出。它们是圆的半径r、最小距离R(显然需要至少是半径的两倍)、、Rcrit放置圆的尝试次数、n(控制圆的数量)以及框尺寸xmaxymax

\documentclass{beamer}
\usepackage{tikz}
\tikzset{pics/random chains/.style={code={
\tikzset{random chains/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/random chains/##1}}%
\pgfmathsetmacro\xlist{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
\pgfmathsetmacro\ylist{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})} 
\draw [pic actions] (\xlist,\ylist) circle [radius=\pv{r}];
\foreach \XX in {1,...,\pv{n}}
 {\pgfmathsetmacro\x{\pv{r}+rnd*(\pv{xmax}-2*\pv{r})}
  \pgfmathsetmacro\y{\pv{r}+rnd*(\pv{ymax}-2*\pv{r})}
  \xdef\collision{0}
  \foreach \element [count=\YY starting from 0] in \xlist{
      \pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
      \ifdim\checkdistance pt<\pv{R} pt
          \xdef\collision{1}
          \breakforeach
      \fi
       } 
   \ifnum\collision=0
      \foreach \element [count=\YY starting from 0] in \xlist{
          \pgfmathsetmacro\checkdistance{sqrt(({\xlist}[\YY]-(\x))^2+({\ylist}[\YY]-(\y))^2)}
          \ifdim\checkdistance pt<\pv{Rcrit} pt
            \draw[shorten >=\pv{r}*1cm,shorten <=\pv{r}*1cm] (\x,\y) --({{\xlist}[\YY]},{{\ylist}[\YY]});
          \fi
           }      
      \xdef\xlist{\xlist,\x}
      \xdef\ylist{\ylist,\y}
      \draw[pic actions]  (\x,\y) circle [radius=\pv{r}];
   \fi 
    }   
 }
},
random chains/.cd,n/.initial=50,% tries
xmax/.initial=3,% width
ymax/.initial=3,% height
r/.initial=0.15,%radius
R/.initial=0.45,% grace distance, should be greater than 2*r
Rcrit/.initial=0.6,% critical distance, closer objects will be connected
/tikz/molecules/.cd,
1/.style={draw},
2/.style={}
}

\begin{document}

\begin{frame}[t]
\frametitle{Random chains}
\centering
\begin{tikzpicture}
 \path (0,3.5); % simple way to add some distance
 \pgfmathsetseed{496}
 \draw (0,0) pic[fill=gray!60]{random chains} rectangle ++(3,3)
  (3.5,0) pic[fill=gray!60]{random chains={Rcrit=0.3}} rectangle ++(3,3);
 \begin{scope}[xshift=7cm]
 \draw  pic[fill=gray!60]{random chains={n=80,Rcrit=0.3}} rectangle ++(3,3);
 \foreach \X [count=\Z starting from 0] in \xlist
  {\pgfmathsetmacro{\Y}{{\ylist}[\Z]}
  \pgfmathrandominteger{\rin}{0}{4}
  \ifcase\rin
  \or
   \draw (\X,\Y)[fill=cyan] circle[radius=0.15cm] node[scale=0.8]{$+$};
  \or
   \draw (\X,\Y)[fill=green!70!black] circle[radius=0.15cm] node[scale=0.8]{$-$};
  \or
   \draw (\X,\Y)[fill=yellow] circle[radius=0.15cm] node[scale=0.8]{$*$};
  \or
   \path (\X,\Y)[fill=white] circle[radius=0.18cm] 
    node[fill=red,draw,inner sep=0.3pt,circle,scale=0.4]{$e^-$};
  \fi}
 \end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

如果想要更多圆圈,就减少R,如果想要更少圆圈,就增加 。如果想要更多连接,就增加Rcrit,如果将其设置为小于R,则不会有连接,如您在中间和右侧嵌件中看到的那样。

同样的序言

\begin{frame}[t]
\frametitle{Random chains}
\centering
\begin{tikzpicture}
 \pgfmathsetseed{496}
 \draw foreach \X in {0,3.5,7}
  {foreach \Y in {0,-3.5} 
  { (\X,\Y) pic[fill=gray!60]{random chains} rectangle ++(3,3)}};
\end{tikzpicture}
\end{frame}

生产

在此处输入图片描述

我并没有热情将它嵌入到您的文档中(也因为它既不简单也不实用,至少在我的计算机上是这样)。但是您应该能够将它添加到任何可以beamer加载的工作文档中tikz

相关内容