使用 TikZ 绘制渗透配置

使用 TikZ 绘制渗透配置

我正在尝试绘制一个位点/键渗透配置。对于位点渗透,这似乎更容易绘制,我尝试了这个可能太幼稚的代码:

\documentclass{standalone}    
\usepackage{tikz}    
\usepackage{pgfplots}    
\usepackage{xifthen}    
\begin{document}    
\begin{tikzpicture}    
\pgfmathsetseed{1}    
  \foreach \x/\y in {1/1,2/2,3/3,4/4,5/5}{
    \ifthenelse {rand >50} 
      {\fill[gray!50] (\x,\y) rectangle (\x+1,\y+1);}
      {\fill[gray!10] (\x,\y) rectangle (\x+1,\y+1);}
  }
\end{tikzpicture}
\end{document}

似乎行不通。我想要一个可以轻松适应键配置的解决方案,即不是(白色/灰色)正方形有白色/灰色边段。最终能够绘制六边形站点渗透,例如

六方位渗透

答案1

好的,这是您的起点。实际上绘制六边形并不复杂,您只需使用shapes.geometrictikz 库即可。\ifthenelse我发现使用语句来实现条件处理更容易\ifnum。您必须对使用的节点数量稍加注意,不要超过 Tex 的容量。也许调整节点的大小或切换到 LuaLatex 可能会有所帮助。

在此处输入图片描述

代码:

\documentclass[border=1cm]{standalone}    
\usepackage{tikz}    
\usetikzlibrary{shapes.geometric}       
\begin{document}    
\begin{tikzpicture}[every node/.style={regular polygon, regular polygon sides=6, minimum size=1cm}]      
    \foreach \y [count=\yi] in {1,2,...,90}{
        \foreach \x in {0,1.5,...,45}{
            \pgfmathrandominteger{\a}{1}{100}
            \ifnum\a>50 
                \node[draw=white, fill=gray] at ({\x + mod(\yi,2)*0.75},{\y*sqrt(3)/4}) {};
            \else
                \node[draw=white, fill=gray!25] at ({\x + mod(\yi,2)*0.75},{\y*sqrt(3)/4}) {};
            \fi
        }
    }
\end{tikzpicture}
\end{document}

编辑:

使用 LuaLaTex 渲染图像有助于克服内存限制。下图是用 ymax=360 和 xmax=180 绘制的:

在此处输入图片描述

编辑2

好的,这里有一些针对您的键渗透问题的代码。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
%
\begin{document}
\begin{tikzpicture}[every path/.style={ultra thick, black}]
    %
    \coordinate (xa) at ({-1cm-0.8pt},0); % correcting the vectors for half the line width (0.8pt) of ultra thick
    \coordinate (ya) at (0,{-1cm-0.8pt});
    \coordinate (xb) at (0.8pt,0);
    \coordinate (yb) at (0,0.8pt);
    %
    \foreach \y in {1,2,...,50}{
        \foreach \x in {1,2,...,50}{
            \ifnum\x>1
                \pgfmathrandominteger{\a}{1}{100}
                \ifnum\a>50 
                    \draw ($(\x,\y)+(xa)$) -- ($(\x,\y)+(xb)$);
                \fi
            \fi
            %
            \ifnum\y>1
                \pgfmathrandominteger{\a}{1}{100}
                \ifnum\a>50 
                    \draw ($(\x,\y)+(ya)$) -- ($(\x,\y)+(yb)$);
                \fi
            \fi
        }
    }
\end{tikzpicture}%
%
\end{document}

在此处输入图片描述

答案2

这是渗透的不同方法元帖子使用方格和递归例程来查找连接到随机节点的边。几乎肯定有更有效的方法来编码,但我试图让它尽可能简单。您可以使用类似的技术来探索六边形网格。

在此处输入图片描述

n>20当和时,搜索节点列表会有点慢p>0.5。如果你有足够的耐心,它会起作用,但对于n>32你应该使用-numbersystem=double作为命令行选项的mpost

prologues := 3;
outputtemplate := "%j%c.eps";

% recursive function to mark the connections from a given node
% 
% global variables: 
% - edges[] - list of edges as node pairs
% - e       - the number of edges (counting from 1)
% - wet[]   - boolean to show if we have marked an edge with blue already
% 
% note that we only mark an edge if the current node is the head
%
vardef mark_connections_from(expr node) = 
   save head, tail; numeric head, tail;
   for i=1 upto e:
      if not wet[i]:
          head := xpart edges[i];
          tail := ypart edges[i];
          if node=head:
             draw put(head)--put(tail) withcolor blue;
             wet[i] := true;
             mark_connections_from(tail);
          elseif node=tail:
             mark_connections_from(head);
          fi
      fi
   endfor
enddef;

% convert a node number to an appropriately scaled (x,y) pair
% global variable: u - unit distance
vardef put(expr i) = (i mod n, floor (i/n)) scaled u enddef; 

beginfig(1);
% set parameters: 
% n = make an n*n lattice
% p = probability of adjacent nodes being connected
numeric n, p;
n = 16;
p = .50;

% Create a list of edges using parameters n and p
% being careful to confine the edges to the square.
% Each edge is stored as a pair of node numbers.
pair edges[]; numeric e; e = 0;
for i=0 upto n*n-1:
    if (i mod n < n-1) and (uniformdeviate 1 < p):
        edges[incr e] = (i,i+1);
    fi
    if (i < n*(n-1)) and (uniformdeviate 1 < p):
        edges[incr e] = (i,i+n);
    fi
endfor

% mark all the edges with the "dry" color to start with
numeric u; u = 5mm; % unit distance between nodes
boolean wet[];
numeric head,tail;
for i=1 upto e:
   head := xpart edges[i];
   tail := ypart edges[i];
   draw put(head) -- put(tail) withcolor .6[red,white];
   wet[i] := false;
endfor

% pick a node at random from the list of edges
% and mark it with a blue circle
numeric start;
start = xpart edges[floor uniformdeviate e];
draw fullcircle scaled 6 shifted put(start) withcolor blue;

% recursively mark the connection from start as "wet"
mark_connections_from(start);

% add a neat dot on each node in the lattice
for i=0 upto n-1:
  for j=0 upto n-1:
     drawdot (i,j) scaled u withpen pencircle scaled 2;
  endfor
endfor

% add a margin all round
setbounds currentpicture to unitsquare scaled (u*n+u) shifted -(u,u);

endfig;
end.

相关内容