我编写了一个非线性变换,能够正确变换在 TiKZ 中绘制的图形(将左侧的矩形变换为右侧的圆弧),但在 \includegraphics 上执行相同操作不起作用。我怎样才能对其应用相同的变换?这是我的变换代码
%!tikz editor 1.0
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage[graphics, active, tightpage]{preview}
\PreviewEnvironment{tikzpicture}
%!tikz preamble begin
\usepgfmodule{nonlineartransformations}
\usepgfmodule[nonlineartransformations]
\usepackage{mathtools}
\makeatletter
\newcommand*{\length}{50}%
\newcommand*{\width}{20}%
\newcommand*{\PI}{3.14}%
\newcommand*{\Rad}{30}%
\newcommand*{\rad}{27.5}%
\newcommand*{\slant}{20}%
\pgfmathsetmacro{\alpha}{(2*\PI*(\Rad-\rad))/\slant}%
\pgfmathsetmacro{\tconst}{(\rad*\slant)/(\Rad-\rad)}%
\tikzset{declare function={zeta(\x,\y) = \tconst + ((\x*\slant)/\length);}}
\tikzset{declare function={theta(\x,\y)=(\alpha*\y)/\width;}}
\tikzset{declare function={shi(\x)=\x+60;}}
\def\polartransformation
{
\pgfmathsetmacro{\costheta}{cos(deg(theta(\pgf@x,\pgf@y)))}
\pgfmathsetmacro{\sintheta}{sin(deg(theta(\pgf@x,\pgf@y)))}
\pgfmathsetmacro{\resz}{zeta(\pgf@x,\pgf@y)}
\pgfmathsetmacro{\xcoord}{\resz*\costheta}
\pgfmathsetmacro{\ycoord}{\resz*\sintheta}
\setlength{\pgf@x}{\xcoord pt}
\setlength{\pgf@y}{\ycoord pt}
}
\makeatother
%!tikz preamble end
\begin{document}
%!tikz source begin
\begin{tikzpicture}
\draw [black, left color=white, right color=gray ] (100pt,20pt) rectangle (150pt,0pt);
{
\pgftransformnonlinear{\polartransformation}
\pgfsettransformnonlinearflatness{0.2pt}
\draw [black, left color=white, right color=gray ] (0pt,20pt) rectangle (50pt,0pt);
}
\end{tikzpicture}
%!tikz source end
\end{document}
答案1
这几乎完全抄袭自这非常很好的答案除了我将地图改为极坐标变换之外。
为了解释这些变化,首先回想一下fx
和fy
是变换的图像,即
(x,y) \mapsto (fx(x,y),fy(x,y)) .
其他函数fxx
、fxy
和均由这些函数派生而来(fyx
并且fyy
只是函数在格点意义上的导数)。为了获得极坐标变换,可以将 解释x
为角度,y
将 解释为半径。然后极坐标变换的一个可能选择是
fx(x,y) = -(y+10)*cos(x*5) ,
fy(x,y) = (y+10)*sin(x+y) ,
其中数值常数10
和5
只是手动选择以获得合理的输出。如果你移动三角函数的参数,你将旋转图像,等等。
\documentclass[border=9,tikz]{standalone}
\begin{document}
\pgfmathdeclarefunction{fx}{2}{\pgfmathparse{-(#2+10)*sin(#1*5)}}
\pgfmathdeclarefunction{fy}{2}{\pgfmathparse{(#2+10)*cos(#1*5)}}
\pgfmathdeclarefunction{fxx}{2}{\pgfmathparse{fx(#1+1,#2)-fx(#1,#2)}}
\pgfmathdeclarefunction{fxy}{2}{\pgfmathparse{fy(#1+1,#2)-fy(#1,#2)}}
\pgfmathdeclarefunction{fyx}{2}{\pgfmathparse{fx(#1,#2+1)-fx(#1,#2)}}
\pgfmathdeclarefunction{fyy}{2}{\pgfmathparse{fy(#1,#2+1)-fy(#1,#2)}}
\begin{tikzpicture}
\path(-15,-5)(15,18);
\foreach\i in{-10,...,9}{
\foreach\j in{-5,...,4}{
\pgfmathsetmacro\aa{fxx(\i,\j)}
\pgfmathsetmacro\ab{fxy(\i,\j)}
\pgfmathsetmacro\ba{fyx(\i,\j)}
\pgfmathsetmacro\bb{fyy(\i,\j)}
\pgfmathsetmacro\xx{fx (\i,\j)}
\pgfmathsetmacro\yy{fy (\i,\j)}
\pgflowlevelobj{
\pgfsettransformentries{\aa}{\ab}{\ba}{\bb}{\xx cm}{\yy cm}
}{
\fill[black!10](1,0)--(0,0)--(0,1);
\clip(1,0)--(0,0)--(0,1)--cycle;
\tikzset{shift={(-\i,-\j)}}
\path(0,0)node{\includegraphics[width=20cm,height=10cm]{example-image-duck}};
}
\pgfmathsetmacro\aa{fxx(\i ,\j+1)}
\pgfmathsetmacro\ab{fxy(\i ,\j+1)}
\pgfmathsetmacro\ba{fyx(\i+1,\j )}
\pgfmathsetmacro\bb{fyy(\i+1,\j )}
\pgfmathsetmacro\xx{fx (\i+1,\j+1)}
\pgfmathsetmacro\yy{fy (\i+1,\j+1)}
\pgflowlevelobj{
\pgfsettransformentries{\aa}{\ab}{\ba}{\bb}{\xx cm}{\yy cm}
}{
\clip(0,0)--(-1,0)--(0,-1)--cycle;
\tikzset{shift={(-\i-1,-\j-1)}}
\path(0,0)node{\includegraphics[width=20cm,height=10cm]{example-image-duck}};
}
}
}
\end{tikzpicture}
\end{document}