4

I want to apply a nonlinear transformation to the following image

enter image description here

Whose code is

\begin{tikzpicture}
\draw (-4,-2) grid [step=1] (4,2);
\draw[
fill=green!30, draw=blue, very thick, rounded corners=5ex, opacity=.5, dashed] (-2.,-1.) rectangle (2.,1.);
\end{tikzpicture}

With the nonlinear transformation I found there I get enter image description here

The code is

\documentclass[tikz,border=3mm]{standalone}

\usepgfmodule{nonlineartransformations}

\makeatletter \def\mytransformation{% \pgfmathsetmacro{\myX}{\pgf@x + 4sin(4\pgf@y) } \pgfmathsetmacro{\myY}{\pgf@y} \setlength{\pgf@x}{\myX pt} \setlength{\pgf@y}{\myY pt} } \makeatother

\begin{document}

\begin{tikzpicture} \begin{scope}%Inside the scope transformation is active \pgftransformnonlinear{\mytransformation}

\draw (-4,-2) grid [step=1] (4,2);

\draw[ fill=green!30, draw=blue, very thick, rounded corners=5ex, opacity=.5, dashed] (-2.,-1.) rectangle (2.,1.);

\end{scope}

\end{tikzpicture} \end{document}

How is it possible to let the edges transform well ?

Black Mild
  • 17,569
Physor
  • 431

2 Answers2

2

The rounded cornerscommand does not seem to work in this case (see here about it Nonlinear transformation applied on decorations along path with a custom pic). I propose this way to solve this problem.

\documentclass[tikz,border=3mm]{standalone}

\usepgfmodule{nonlineartransformations}

\makeatletter \def\mytransformation{% \pgfmathsetmacro{\myX}{\pgf@x + 4sin(4\pgf@y) } \pgfmathsetmacro{\myY}{\pgf@y} \setlength{\pgf@x}{\myX pt} \setlength{\pgf@y}{\myY pt} } \makeatother

\begin{document} \begin{tikzpicture} \newcommand\xx{1} \pgftransformnonlinear{\mytransformation} \draw (-5,-5) grid [step=1] (5,5); \draw[fill=green!30, draw=blue, very thick,opacity=.5, dashed] (-2,-1+\xx) -- (-2,1-\xx).. controls (-2,1) .. (-2+\xx,1) -- (2-\xx,1).. controls (2,1) .. (2,1-\xx) -- (2,-1+\xx).. controls (2,-1) .. (2-\xx,-1) -- (-2+\xx,-1).. controls (-2,-1) .. (-2,-1+\xx); \end{tikzpicture} \end{document}

enter image description here

Of course, you can play with the \xx parameter within certain limits.

kabenyuk
  • 604
  • Apart from the fact that the object is to be defined manually, very nice solution!. It seems you've been working with my old code on this post. Thanks – Physor Jul 27 '22 at 13:53
  • So do you mean with \xx a number one must choose or is it a part of the code, a variable for example ? – Physor Jul 27 '22 at 14:08
  • @Physor Thank you. If the rectangle has dimensions a x b and a<b, then parameter xx can be chosen in the range 0<=xx<= a/2, either as described in my code \newcommand\xx{1} or like this \def\xx{0.8} In addition you can change control points or add new ones if needed. – kabenyuk Jul 27 '22 at 14:50
1

The option rounded corners is quite stubborn sometimes: it has several pitfalls. For instance it does not work well with scale, see pgfmanual.pdf, Section 14.5 Rounding Corners.

To avoid using this option, one simple way is define your own round rectangle path

\def\myround{.4}        % radius of the rounding corners    
\def\myrectangle{(0,2)
    --(3-\myround,2)   arc(90:0:\myround)
    --(3,-2+\myround)  arc(0:-90:\myround)
    --(-3+\myround,-2) arc(-90:-180:\myround)
    --(-3,2-\myround)  arc(180:90:\myround)--cycle;
}    

Hence, with your \pgftransformnonlinear{\mytransformation}

enter image description here

and without \pgftransformnonlinear{\mytransformation}

enter image description here

Complete code:

\documentclass[tikz,border=3mm]{standalone}
\usepgfmodule{nonlineartransformations}
\makeatletter
\def\mytransformation{%
    \pgfmathsetmacro{\myX}{\pgf@x + 4*sin(4*\pgf@y) }
    \pgfmathsetmacro{\myY}{\pgf@y}
    \setlength{\pgf@x}{\myX pt}
    \setlength{\pgf@y}{\myY pt}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\def\myround{.4}        % radius of the rounding corners    
\def\myrectangle{(0,2)
    --(3-\myround,2)   arc(90:0:\myround)
    --(3,-2+\myround)  arc(0:-90:\myround)
    --(-3+\myround,-2) arc(-90:-180:\myround)
    --(-3,2-\myround)  arc(180:90:\myround)--cycle;
}   
\begin{scope} % Inside the scope, the transformation is active
\pgftransformnonlinear{\mytransformation}
\draw[gray!30] (-5,-3) grid (5,3);
\draw[blue,fill=green!30,very thick,fill opacity=.5,dashed] \myrectangle;
\end{scope}
\end{tikzpicture}
\end{document}
Black Mild
  • 17,569
  • The other solution is also similar, but it seems better to use controls than acrs. Because the edges didn't stick to the grid as in the other answer. – Physor Jul 27 '22 at 13:57