我想在 Tikz 中绘制一个类似下图的图形
我曾尝试使用来自的代码这个问题所以我有以下 MWE
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,5}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,9}{
\coordinate [shift={(\j,\i)}] (n-\i-\j) at (rand*180:1/4+rnd/8);
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\fi
}}
\end{tikzpicture}
\end{document}
代码给出以下输出
这里的问题是我没有那些黑色节点,我不知道如何将它们放置在每个交叉点上。另外,我不知道如何放置小方块和文本定位节点。任何帮助都将不胜感激
答案1
代码中给出了所有节点,并且已经命名,因此我们只需从坐标切换到我们填充的某个圆形节点即可。甚至还提供了重心所需的计数器。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,5}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,9}{
\node [shift={(\j,\i)},circle,fill,inner sep=1.5pt] (n-\i-\j) at
(rand*180:1/4+rnd/8){};
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\ifnum\j>0
\node[draw,inner sep=1.5pt] at
(barycentric cs:n-\i-\j=1,n-\ii-\j=1,n-\i-\jj=1,n-\ii-\jj=1){};
\fi
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\fi
}}
\end{tikzpicture}
\end{document}
为了遮蔽一个单元格,可以使用backgrounds
库。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[>=latex,font=\sffamily]
\pgfmathsetseed{42}
\foreach \i [evaluate={\ii=int(\i-1);}] in {0,...,5}{
\foreach \j [evaluate={\jj=int(\j-1);}] in {0,...,9}{
\node [shift={(\j,\i)},circle,fill,inner sep=1.5pt] (n-\i-\j) at
(rand*180:1/4+rnd/8){};
\ifnum\i>0
\draw [help lines] (n-\i-\j) -- (n-\ii-\j);
\ifnum\j>0
\node[draw,inner sep=1.5pt] (b-\i-\j) at
(barycentric cs:n-\i-\j=1,n-\ii-\j=1,n-\i-\jj=1,n-\ii-\jj=1){};
\fi
\fi
\ifnum\j>0
\draw [help lines] (n-\i-\j) -- (n-\i-\jj);
\fi
}}
\begin{scope}[on background layer]
\fill[gray!20] (n-3-5.center) -- (n-2-5.center) -- (n-2-4.center) --
(n-3-4.center) -- cycle coordinate[pos=0.7] (aux);
\end{scope}
\draw[<-] (b-3-5) --++ (1.5em,-0.3em) node[right]{cell center};
\draw[<-] (aux) --++ (1em,2em) node[above right]{cell face};
\draw[<-] (n-2-4) --++ (-1em,-1em) node[below left]{node};
\end{tikzpicture}
\end{document}
可以使用以下方法获得上面屏幕截图中网格线
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,calc}
\begin{document}
\begin{tikzpicture}[>=latex,font=\sffamily,
bullet/.style={circle,fill,inner sep=1.5pt},
declare function={a=0.5;},scale=2]
\pgfmathsetseed{123}
\draw (0,0) node[bullet] (n-0-0) {} rectangle (5,4) node[bullet] (n-5-4) {}
(0,4) node[bullet] (n-0-4) {} (5,0) node[bullet] (n-5-0) {}
foreach \X in {1,...,4}
{({\X+a*(rnd-0.5)},0) node[bullet] (n-\X-0) {}
({\X+a*(rnd-0.5)},4) node[bullet] (n-\X-4) {}
edge (n-\X-0)}
foreach \Y in {1,...,3}
{(0,{\Y+a*(rnd-0.5)}) node[bullet] (n-0-\Y) {}
(5,{\Y+a*(rnd-0.5)}) node[bullet] (n-5-\Y) {}
edge (n-0-\Y)}
foreach \X [evaluate={\XX=int(\X-1)}] in {1,...,5} {
foreach \Y [evaluate={\YY=int(\Y-1)}] in {1,...,4} {
\ifnum\X<5
\ifnum\Y<4
(intersection of n-\X-0--n-\X-4 and n-0-\Y--n-5-\Y)
node[bullet] (n-\X-\Y){}
\fi
\fi
(barycentric cs:n-\X-\Y=1,n-\XX-\Y=1,n-\X-\YY=1,n-\XX-\YY=1)
node[draw,inner sep=1.5pt] (b-\X-\Y){}
}};
\begin{scope}[on background layer]
\fill[gray!20] (n-3-2.center) -- coordinate[pos=0.2] (aux) (n-2-2.center) --
(n-2-1.center) -- (n-3-1.center) -- cycle ;
\end{scope}
\draw[<-] (b-3-2) --++ (1.6em,-0.3em) node[right]{cell center};
\draw[<-] (aux) --++ (0.3em,0.6em) node[above right]{cell face};
\draw[<-] (n-2-1) --++ (-0.3em,-0.3em) node[below left]{node};
\end{tikzpicture}
\end{document}