5

I want to draw a figure in Tikz similar to the following picture

enter image description here

I have tried using the code from this question so I have the following 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}

The code gives the following ouput

enter image description here

The problem here is that I don't have those black nodes of which I don't know how to place them on every intersection. Also, I don't know how I can place the tiny squares and the text locating nodes. Any help would be appreciated

7

The nodes are all given in the code, and already named, so we only have to switch from coordinate to some circle shaped node that we fill. Even the counters needed for the barycenters are provided.

\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}

enter image description here

In order to shade one cell, one can use the backgrounds library.

\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}

enter image description here

Something along the lines of the grid of the upper screen shot can be obtained with

\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}

enter image description here

| improve this answer | |
  • Thanks a lot. All the solutions work so well. – Mafeni Alpha May 13 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.