1

Is there a way to draw the following figure using tikz enter image description here

This post is similar but the for loops are complicated for me to understand.

The rectangle domain is 2.0 x 1.0 (width x height) and there are 20 subdivisions along horizontal direction and 10 subdivisions along vertical directions.

The blue dots are centroids of the triangle

Any help/clue will be really appreciated.

Thank you

10

One needs to find an algorithm to draw it. The following is one of such algorithms.

\documentclass[tikz,margin=3]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw[gray] (0,0) grid (20,10);
\foreach \i in {0,...,9} {
    % Draw the grid
    \draw[gray] (0,\i) -- (10-\i,10);
    \draw[gray] (10+\i,0) -- (20,10-\i);
    \ifnum\i=0\relax\else
        \draw[gray] (\i,0) -- (10+\i,10);\fi
    % Draw the dots
    \foreach \j in {0,...,19} {
        % The center of each square is \j+.5 and \i+.5
        \coordinate (center) at (\j+.5,\i+.5);
        \fill[blue] ([shift={(-1/6,1/6)}]center) circle (3pt);
        \fill[blue] ([shift={(1/6,-1/6)}]center) circle (3pt);
    }
}
\end{tikzpicture}
\end{document}

enter image description here

8

An alternative construction with a matrix and nodes in empty cells.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[%
    mymatrix/.style={
        matrix of nodes,
        nodes in empty cells,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={draw=gray,
            minimum size=1cm,
            outer sep=0pt,
            inner sep=0pt,
            anchor=center,
            path picture={%
                \draw (path picture bounding box.north east)--(path picture bounding box.south west);
                \fill[blue] ([shift={(-1/6,1/6)}]path picture bounding box.center) circle(3pt);
                \fill[blue] ([shift={(1/6,-1/6)}]path picture bounding box.center) circle(3pt);
            }
        }
    }
]
\matrix[mymatrix]{%
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
&&&&&&&&&&&&&&&&&&&\\
};
\end{tikzpicture}
\end{document}

enter image description here

1

Yet another alternative: pics. And store the variables in pgf keys. Like Joule V's nice solution, this grid can be transformed (e.g. rotated) without resorting to transform canvas, which tends to mess up things.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/cell/.style={code={\draw (-#1,-#1) rectangle (#1,#1)
(-#1,-#1) -- (#1,#1);
\fill[\pgfkeysvalueof{/tikz/cell/color}]
(135:{#1*\pgfkeysvalueof{/tikz/cell/circle pos}}) 
circle[radius=#1*\pgfkeysvalueof{/tikz/cell/radius}] 
(-45:{#1*\pgfkeysvalueof{/tikz/cell/circle pos}})
circle[radius=#1*\pgfkeysvalueof{/tikz/cell/radius}];}},
cell/.cd,color/.initial=blue,radius/.initial=1/6,circle pos/.initial={sqrt(0.5)}]
\path foreach \X in {1,...,20}
{foreach \Y in {1,...,10} {(\X,\Y) pic[cell/radius=1/5,cell/circle pos=1/2]{cell=0.5}}};
\end{tikzpicture}
\end{document}

enter image description here

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.