8

I'm trying to replicate this picture (from wikipedia) with TikZ: enter image description here

The part on the left is straightforward but I'm struggling to come up with ideas for the part on the right.

3
  • This can be achieved rather easily, either with projections from 3D, nonlinear transformations or just by hand.
    – user194703
    Jan 24 '20 at 3:08
  • @Schrödinger'scat what do you mean “projections from 3D” ?
    – Black Mild
    Jan 24 '20 at 6:12
  • 1
    @BlackMild You can draw something in 3D and project it on the screen. The pic on the right looks a bit like a surface in 3D.
    – user194703
    Jan 24 '20 at 6:22
19

This employs a nonlinear transformation to produce a similar output.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\definecolor{myblue}{RGB}{191,202,236}
\definecolor{myred}{RGB}{185,134,146}
\usepgfmodule{nonlineartransformations}
\makeatletter 
\def\curvytransformation{%
\pgfmathsetmacro{\myx}{\pgf@x+0.3*\pgf@y-3*sin(\pgf@y*2)}%
\pgfmathsetmacro{\myy}{\pgf@y-10*sin(\pgf@x)}%
\pgf@x=\myx pt%
\pgf@y=\myy pt%
}
\makeatother
\begin{document}
\begin{tikzpicture}[>=stealth]
 \begin{scope}[xshift=-10cm,local bounding box=L]
  \draw[fill=myblue] (0,0) rectangle (8,8);
  \draw[help lines] (0,0) grid (8,8);
  \draw[thick,fill=myred] (1,1) rectangle (7,7);
  \draw[help lines] (1,1) grid (7,7);
  \draw[very thick,<->] (1,7) |- (7,1);
 \end{scope}
 %
 \begin{scope}[local bounding box=R]
  \pgftransformnonlinear{\curvytransformation}%
  \draw[fill=myblue] (0,0) rectangle (8,8);
  \draw[help lines] (0,0) grid (8,8);
  \draw[thick,fill=myred] (1,1) rectangle (7,7);
  \draw[help lines] (1,1) grid (7,7);
  \path (1,1) coordinate (o) (1,1.1) coordinate (y) (1.1,1) coordinate (x);
 \end{scope}
 \draw[fill=white,fill opacity=0.2] let \p1=($(y)-(o)$),\p2=($(x)-(o)$),
    \n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)} in
  [fill=white,fill opacity=0.2] (o) -- ++(\n1:6.25) coordinate (y')  
  -- ++(\n2:6.25) -- ++(\n1+180:6.25) coordinate (x')
  -- cycle;
 \draw[very thick,<->] (y') -- (o) -- (x');
 %
 \draw[-stealth,thick] (-1.5,4) to[bend left=10] node[above]{$f$} (0.5,4);
\end{tikzpicture}
\end{document}

enter image description here

3
  • 1
    Good answer!! Can you write the expression of the nonlinear transformation, please? Thank you, as always.
    – manooooh
    Jan 24 '20 at 5:02
  • 5
    @manooooh It is all in the code. (\pgf@x,\pgf@y) gets mapped to (\pgf@x+0.3*\pgf@y-3*sin(\pgf@y*2), \pgf@y-10*sin(\pgf@x)).
    – user194703
    Jan 24 '20 at 5:04
  • 1
    As usual excellent answer ! Jan 24 '20 at 13:32

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.