可重复随机的 TikZ 形状 - 从指定每个坐标开始生成最少的代码

可重复随机的 TikZ 形状 - 从指定每个坐标开始生成最少的代码

我创建了一张自己满意的图像,但想扩展我的tikz知识 - 我不知道如何在图片中创建随机但一致的形状tikz。最终图像如下所示:

在此处输入图片描述

我必须指定图表中的所有内容,包括“酰胺水解酶”的线条与球体的连接位置。有什么更好的方法来绘制此图表而不必指定每个坐标?

\RequirePackage[x11names]{xcolor}
\documentclass[border=0pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}

\usepackage{fontspec}
\usepackage{microtype}
\setmainfont{Arial}[ItalicFont=ariali.ttf,BoldFont=arialbd.ttf,BoldItalicFont=arialbi.ttf]
\newcommand\Denovo{\textit{De novo}}

\begin{document}
%
\footnotesize%
%
\tikzset{%
    dotted_ellipse/.style={draw=black!80!white, line width=0.5pt, dash pattern=on 1.5pt off 1.5pt,%
            inner ysep=0pt,inner xsep=-2pt, ellipse}%
}%
%
\definecolor{myblack}{rgb}{.3,.3,.3}%
%
\newcommand\circleColour{Bisque2}%
%
\newcommand\medEL{5}%
\newcommand\medEW{3}%
\newcommand\smallEL{2.5}%
\newcommand\biggerEL{5.5}%
\newcommand\smallEW{1.35}%
\newcommand\biggerEW{3.5}%
%
\begin{tikzpicture}[%
    background/.style   = {ultra thin,color=DodgerBlue3},%
    small ellipse/.style   ={color=\circleColour},%
    med ellipse/.style    ={color=\circleColour},%
    bigger ellipse/.style ={color=\circleColour},%
    max ellipse/.style    ={color=\circleColour},%
    ]%
%
\filldraw[background] (0,0) -- (0,3) -- (16,3) -- (16,0) -- (0,0);%
%
\node [anchor=west,text width=1.11cm,inner ysep=0.1pt] (native) at (16.3,2.66) {\textcolor{myblack}{Native \\ enzymes}};%
\node [anchor=west,text width=1.11cm,inner ysep=0.6pt] (ratdes) at (16.3,1.05) {\textcolor{myblack}{Rational \\ design}};%
\node [anchor=west,text width=1.11cm,inner ysep=0.1pt] (direvo) at (16.3,1.87) {\textcolor{myblack}{Directed \\ evolution}};%
\node [anchor=west,text width=1.1cm,inner ysep=0pt] (denovo) at (16.3,0.29) {%
    \textcolor{myblack}{\Denovo\\ design}%
};%
%
\node [anchor=west] (dehy) at (1, 2.75) {\textcolor{white}{\scriptsize\textit{Dehydrogenases}}};%
\node [anchor=west] (urea) at (3.2, 0.27) {\textcolor{white}{\scriptsize\textit{Urease}}};%
\node [anchor=west] (hydro) at (2.1, 0.70) {\textcolor{white}{\scriptsize\textit{Amidohydrolases}}};%
%
\filldraw[small ellipse] (2.1,1.5)         ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (3.1,1.67)       ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (4,2.625)         ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (4.8,0.45)        ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (5,2.325)         ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (6.8,1.35)        ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (7.4,0.6)          ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (7.6,0.625)      ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (8.2,1.72)        ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (9.5,2.25)           ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (9.6,2.025)     ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (9.75,0.825)     ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (10.3,0.9)         ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (10.5,0.72)      ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (10.7,0.8625)   ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (10.75,1.525)   ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (11,1.8)          ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (11.3,1.65)       ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (11.7,1.6125)   ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (12.55,2.6625) ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (14,2.175)        ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (14.4,2.175)     ellipse (\smallEL pt and \smallEW pt);%
\filldraw[small ellipse] (14.7,2.5875)   ellipse (\smallEL pt and \smallEW pt);%
%
\filldraw[med ellipse] (1.75,1.525) ellipse (\medEL pt and \medEW pt);%
\filldraw[med ellipse] (2.1,2.025)   ellipse (\medEL pt and \medEW pt);%
\filldraw[med ellipse] (5,0.55)        ellipse (\medEL pt and \medEW pt);%
\filldraw[med ellipse] (3.7,2.55)     ellipse (\medEL pt and \medEW pt);%
\filldraw[med ellipse] (8,1.35) ellipse (\medEL pt and \medEW pt);%
\draw[dotted_ellipse, name=ratdot] (8,1.35) ellipse (10 pt and 7pt);%
\filldraw[med ellipse] (14.2,2.325) ellipse (\medEL pt and \medEW pt);%
%
\filldraw[bigger ellipse] (2.9,1.5)         ellipse (\biggerEL pt and \biggerEW pt);%
\filldraw[bigger ellipse] (4.7,2.4)         ellipse (\biggerEL pt and \biggerEW pt);%
\filldraw[bigger ellipse] (11.1,0.7125) ellipse (\biggerEL pt and \biggerEW pt);%
%
\filldraw[max ellipse] (13.5,1.65) ellipse (7pt and 5pt);   % Main one
%
\draw[thin] (denovo) -- (13.5,0.45)  node [draw=black, circle, fill=black, inner sep=1.4pt, pos=1]  {};%
\draw[thin] (native)   -- (14.2,2.325) node [draw=black, circle, fill=black, inner sep=1.4pt, pos=1]  {};%
\draw[thin] (direvo)   -- (13.83,1.65) node [draw=black, circle, fill=black, inner sep=1.4pt, pos=1]  {};%
\draw[thin] (ratdes)   -- (8.44,1.350) node [draw=black, circle, fill=black, inner sep=1.4pt, pos=1]  {};%
%
\draw[->,thin,-latex,color=white] (dehy.east) -- (3.5,2.6);% 
\draw[->,thin,-latex,color=white] (hydro.east) -- (4.8,0.6);% 
\draw[->,thin,-latex,color=white] (urea.east) -- (4.7,0.43);%
%
\end{tikzpicture}
\end{document}

我希望它每次都一样,使用最少的代码(以我的水平可以理解),标签没有重叠,并且看起来向左和向右分布得很均匀。我的代码使用了大约 100 行代码,远非最佳。有人有更好的解决方案吗?我对我的图像很满意,但对我的代码却不满意,它太丑了。我正在编译,lualatex因为我的报告需要 Arial 字体。

答案1

只是一个想法,我不知道这是否有用:您可以使用两个\foreach循环在某个区域内水平和垂直逐步移动。步骤很大,以至于您想要放置的椭圆永远不会重叠。在每个步骤中,您可以使用随机函数来决定是放置小椭圆还是大椭圆,或者根本不放置椭圆。您还可以使用随机函数将椭圆在其“单元格”内稍微移动一点,这样它看起来真的很随机​​。

\documentclass[border=10pt]{standalone}
\usepackage[x11names]{xcolor}
\usepackage{tikz}

\tikzset{
    medium ellipse/.pic={
        \fill[Bisque2] (0,0) circle[x radius=5pt, y radius=3pt];
    },
    small ellipse/.pic={
        \fill[Bisque2] (0,0) circle[x radius=2.5pt, y radius=1.35pt];
    }
}

\begin{document}
  \begin{tikzpicture}
    \fill[DodgerBlue3] (0,0) rectangle (16,3);

    \foreach \x in {0.5,1,...,15.5} {
        \foreach \y in {0.5,1,...,2.5} {
            \pgfmathsetmacro{\temprand}{rand}
            \pgfmathparse{ifthenelse(\temprand > 0.5 && \temprand < 0.75, 1, 0)}
            \ifnum\pgfmathresult=1\relax
                \pic at ([shift={({rand*5pt},{rand*5pt})}]\x,\y) {medium ellipse};
            \fi
            \pgfmathparse{ifthenelse(\temprand > 0.75, 1, 0)}
            \ifnum\pgfmathresult=1\relax
                \pic at ([shift={({rand*5pt},{rand*5pt})}]\x,\y) {small ellipse};
            \fi
        }
    }
  \end{tikzpicture}
\end{document}

在此处输入图片描述

通过命名,pics您可以为它们附加标签:

\documentclass[border=10pt]{standalone}
\usepackage[x11names]{xcolor}
\usepackage{tikz}

\tikzset{
    medium ellipse/.pic={
        \fill[Bisque2] (0,0) circle[x radius=5pt, y radius=3pt];
        \coordinate (-center) at (0,0);
    },
    small ellipse/.pic={
        \fill[Bisque2] (0,0) circle[x radius=2.5pt, y radius=1.35pt];
        \coordinate (-center) at (0,0);
    }
}

\begin{document}
  \begin{tikzpicture}
    \fill[DodgerBlue3] (0,0) rectangle (16,3);

    \newcounter{pics}
    \foreach \x in {0.5,1,...,15.5} {
        \foreach \y in {0.5,1,...,2.5} {
            \pgfmathsetmacro{\temprand}{rand}
            \pgfmathparse{ifthenelse(\temprand > 0.5 && \temprand < 0.75, 1, 0)}
            \ifnum\pgfmathresult=1\relax
                \stepcounter{pics}
                \pic (pic \thepics) at ([shift={({rand*5pt},{rand*5pt})}]\x,\y) {medium ellipse};
            \fi
            \pgfmathparse{ifthenelse(\temprand > 0.75, 1, 0)}
            \ifnum\pgfmathresult=1\relax
                \stepcounter{pics}
                \pic (pic \thepics) at ([shift={({rand*5pt},{rand*5pt})}]\x,\y) {small ellipse};
            \fi
        }
    }

    \draw (pic 7-center) -- (pic 7-center -| -1,0) node[left] {A};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

据我所知,您可以使用 来修复某个(伪)随机生成的模式\pgfmathsetseed{<integer>}。也就是说,指定 后,<integer>您的输出将始终相同。如果您希望椭圆随机分布,但仍能获得固定的可重现输出,这可能会有所帮助。

相关内容