14

I would like to draw the following deformed grid in Tikz.

enter image description here

I have written something that achieves visualisation of homogeneous deformations of a normal grid as in the following MWE, but it cannot handle drawing the deformed grid shown in the link.

Any approach would be appreciated but I would favour updating the \tikzset to achieve this.

MWE

\documentclass[tikz]{standalone}
\usepackage{pgfplots} 
\usetikzlibrary{arrows,shapes,backgrounds,fit,decorations.pathreplacing,chains,snakes,positioning,angles,quotes}  
\usepackage{amsmath,amssymb,bm}

\pgfplotsset{compat=1.10}

\tikzset{
pics/grid/.style n args={9}{
code = {%       
    \pgfmathsetmacro \nxx{#6 - 1}
    \pgfmathsetmacro \nyy{#7 - 1}       

    %CENTRAL DOT
    \filldraw[black] circle (2pt) coordinate (-c); 

    % DASHED GRID VERTICAL  
    \foreach \i in {1,...,#6}{
        \foreach \j in {1,...,\nyy}{
            \pgfmathsetmacro \x {(\i - (#6 + 1)/2)*#8*#2 + (\j - (#7 + 1)/2)*#9*#3} 
            \pgfmathsetmacro \y {(\i - (#6 + 1)/2)*#8*#4 + (\j - (#7 + 1)/2)*#9*#5}

            \pgfmathsetmacro \xt {(\i - (#6 + 1)/2)*#8*#2 + (\j+1 - (#7 + 1)/2)*#9*#3} 
            \pgfmathsetmacro \yt {(\i - (#6 + 1)/2)*#8*#4 + (\j+1 - (#7 + 1)/2)*#9*#5}

            \draw [#1, dashed, line width = 0.2mm] (\x,\y) -- (\xt,\yt);
            }
    }

    % DASHED GRID HORIZONTAL
    \foreach \i in {1,...,\nxx}{
        \foreach \j in {1,...,#7}{
            \pgfmathsetmacro \x {(\i - (#6 + 1)/2)*#8*#2 + (\j - (#7 + 1)/2)*#9*#3} 
            \pgfmathsetmacro \y {(\i - (#6 + 1)/2)*#8*#4 + (\j - (#7 + 1)/2)*#9*#5}

            \pgfmathsetmacro \xt {(\i+1 - (#6 + 1)/2)*#8*#2 + (\j - (#7 + 1)/2)*#9*#3} % Use the macros to calculate positions
            \pgfmathsetmacro \yt {(\i+1 - (#6 + 1)/2)*#8*#4 + (\j - (#7 + 1)/2)*#9*#5}

            \draw [#1, dashed, line width = 0.2mm] (\x,\y) -- (\xt,\yt);
            }
    }   

    % SOURCES
    \foreach \i in {1,...,#6}{
        \foreach \j in {1,...,#7}{
            \pgfmathsetmacro \x {(\i - (#6 + 1)/2)*#8*#2 + (\j - (#7 + 1)/2)*#9*#3} 
            \pgfmathsetmacro \y {(\i - (#6 + 1)/2)*#8*#4 + (\j - (#7 + 1)/2)*#9*#5}

            \filldraw[fill=#1, draw=black, line width = 0.2mm] (\x,\y) circle (2.5mm) coordinate (-\i-\j);
        }
    }   
}}}

\begin{document}

%% PROPERTIES OF THE GRID
\def \nx{4}
\def \ny{3}
\def \dx{3}
\def \dy{2.2}

%% IDENTITY. UNDEFORMED ARRAY
\def \ai{1}     
\def \bi{0}
\def \ci{0}  
\def \di{1}

%% STRETCHING DEFORMATION MATRIX 
\def \af{0.8} 
\def \bf{0}
\def \cf{0}  
\def \df{1.2}

\begin{tikzpicture}
\pic (a) {grid={blue!15!white}{\ai}{\bi}{\ci}{\di}{\nx}{\ny}{\dx}{\dy}};
\pic (b) {grid={orange!75!red}{\af}{\bf}{\cf}{\df}{\nx}{\ny}{\dx}{\dy}};
\end{tikzpicture}

\end{document}
percusse
  • 157,807
Sid
  • 1,806
  • 2
    By the way, instead of doing the same calculations multiple times, I would remove the first two \foreach loops, use a \node instead of \filldraw to make the circles, and then add two (shorter) loops afterwards to draw the connections between the nodes. Code is too long for comment, and doesn't answer the question, but see this Gist: https://gist.github.com/TorbjornT/5e76cefb7e401fea657291efc160182f – Torbjørn T. Nov 02 '17 at 11:04
  • 1
    We've done this before... (or at least something very similar). – Thruston Nov 02 '17 at 16:26

1 Answers1

28

You can use nonlinear transformations in TikZ.

\documentclass[tikz]{standalone}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\mytransformation{\pgfmathparse{abs(exp(\pgf@y/100))}\pgf@x=\pgfmathresult\pgf@x}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{scope}%Inside the scope transformation is active
\pgftransformnonlinear{\mytransformation}
\draw (-3,0mm) grid [step=1] (3,5);
\draw[red,ultra thick] (0,0) -- (3,5);
\end{scope}
% Here back to normal
\draw[blue,ultra thick] (0,0) -- (3,5);
\end{tikzpicture}
\end{document}

enter image description here

Here I basically scaled the x coordinate with the logarithm of the current y coordinate divided 100 just for demonstration.

percusse
  • 157,807
  • 2
    please be so kind and add to your nice answer information, where to find description about \usepgfmodule{nonlineartransformations}. – Zarko Nov 02 '17 at 11:00
  • 1
    @Zarko In the manual section 103.4 – percusse Nov 02 '17 at 11:02
  • thank you very much. i search for \usepgfmodule and receife zero findings :-(. now i found description :-). – Zarko Nov 02 '17 at 11:06
  • 1
    @Zarko Search for {nonlinear instead. (Searching for {<name of library>} is usually a good way of finding the description of libraries that aren't in the Libraries part.) – Torbjørn T. Nov 02 '17 at 15:09