我想对下图应用非线性变换
谁的代码是
\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}
通过非线性变换我发现那里我明白了
代码如下
\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}
\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}
如何才能让边缘很好地变换?
答案1
在这种情况下,该rounded corners
命令似乎不起作用(请参阅此处使用自定义图片对路径上的装饰应用非线性变换)我建议用这种方法来解决这个问题。
\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}
\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}
当然,您可以\xx
在一定范围内使用该参数。
答案2
这个选项rounded corners
有时相当顽固:它有几个缺陷。例如,它不能很好地与 配合使用scale
,请参阅手册.pdf,14.5 圆角。
为了避免使用此选项,一种简单的方法是定义自己的圆角矩形路径
\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;
}
因此,\pgftransformnonlinear{\mytransformation}
并且没有\pgftransformnonlinear{\mytransformation}
完整代码:
\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}