矩阵变换网格 tikz

矩阵变换网格 tikz

我想制作图表来展示矩阵乘法对 xy 平面中的向量的影响。我已经制作了一个,但是自己创建每条线非常麻烦。有没有简单的方法可以做到这一点?这是我制作的:

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}
        \definecolor{newgrid}{RGB}{0,40,180}
        \tikzstyle{ann} = [fill=white,font=\normalsize,inner sep=1pt];
        \draw[step=1.4cm,gray,very thin] (0,0) grid (16.8,16.8);
        \draw[newgrid,very thin] (2.8,0) -- (14,16.8); % J-Transformed Gridline Start
        \draw[newgrid,very thin] (0,0.7) -- (10.73333,16.8);
        \draw[newgrid,very thin] (0,5.6) -- (7.46667,16.8);
        \draw[newgrid,very thin] (0,10.5) -- (4.2,16.8);
        \draw[newgrid,very thin] (0,15.4) -- (0.93333,16.8);
        \draw[newgrid,very thin] (6.06667,0) -- (16.8,16.1);
        \draw[newgrid,very thin] (9.33333,0) -- (16.8,11.2);
        \draw[newgrid,very thin] (12.6,0) -- (16.8,6.3);
        \draw[newgrid,very thin] (15.86667,0) -- (16.8,1.4); % J-Transformed Gridline End
        \draw[newgrid,very thin] (9.1,16.8) -- (16.8,1.4); % I-Transformed Gridline Start
        \draw[newgrid,very thin] (14,16.8) -- (16.8,11.2);
        \draw[newgrid,very thin] (4.2,16.8) -- (12.6,0);
        \draw[newgrid,very thin] (0,15.4) -- (7.7,0);
        \draw[newgrid,very thin] (0,5.6) -- (2.8,0);
        \draw[very thick,arrows=->] (8.4,8.4) -- (9.8,8.4); % ihat
        \draw[very thick,arrows=->] (8.4,8.4) -- (8.4,9.8); % jhat
        \draw[very thick,arrows=->] (8.4,8.4) -- (11.2,7); % v
        \path (9.1,8.4) node[ann] {$\ihat$} (8.4,9.1) node[ann] {$\jhat$} (9.8,7.7) node[ann] {$\vec{v}$} (8.4,8.4) node[below left] {$O$};
        \draw[newgrid,very thick,arrows=->] (8.4,8.4) -- (11.2,12.6); % A ihat
        \draw[newgrid,very thick,arrows=->] (8.4,8.4) -- (7,11.2); % A jhat
        \draw[newgrid,very thick,arrows=->] (8.4,8.4) -- (15.4,14); % A v
        \path (9.8,10.5) node[ann] {\textcolor{newgrid}{$A \ihat$}} (7.7,9.8) node[ann] {\textcolor{newgrid}{$A \jhat$}} (11.9,11.2) node[ann] {\textcolor{newgrid}{$A \vec{v}$}} (5.6,13.09) node[ann] {$ A =
                \begin{pmatrix}
                    2 & -1\\
                    3 & 2
                \end{pmatrix}
            $
        } (5.6,12.11) node[ann] {$ \vec{v} = 
                \begin{pmatrix}
                    2\\
                    -1
                \end{pmatrix}
            $
        };
    \end{tikzpicture}
    \caption{\label{fig:unit-vectors} Matrix-transformed grid in the XY Plane}
\end{figure}

(抱歉代码太长,需要好几行)输出如下: 相关代码输出

有没有更简单的方法可以做到这一点?

答案1

欢迎!让我们关注倾斜网格。这是非常有用的情况之一transform canvas,您只需通过键(字面上)使用您的转换矩阵cm,这在 pgfmanual v3.1.5 的第 378 页中有描述。(我还要提一下,这\tikzstyle已被弃用。)

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \draw[step=1.4cm,gray,very thin] (0,0) coordinate(bl) grid (16.8,16.8)
 coordinate (tr);
 \path[overlay] (-16.8,-16.8) coordinate (bl');
 \begin{scope}
  \clip (bl) rectangle (tr);
  \draw[transform canvas={cm={2,3,-1,2,(bl)}},step=1.4cm,blue]
  (bl') grid (tr);
 \end{scope} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

设置网格后,您可以在变换后的坐标中工作并执行类似

\begin{scope}[cm={2,3,-1,2,(O)}]
  \draw[thick,->,blue] (O) -- ++ (vx,vy) node[above]{\contour{white}{$\vec  v\,'$}};
\end{scope}

我还会将的全局因素分解出来1.4,坐标会变得更加直观。

\documentclass[tikz,border=3mm]{standalone}
\usepackage[outline]{contour}
\contourlength{1pt}
\begin{document}
\begin{tikzpicture}[scale=1.4,>=stealth,declare function={vx=2;vy=-1;}]
 \draw[gray,very thin] (-6,-6) coordinate(bl) grid (6,6)
 coordinate (tr);
 \path[overlay] (-18,-18) coordinate (bl') 
  (0,0) coordinate (O);
 \begin{scope}
  \clip (bl) rectangle (tr);
  \draw[transform canvas={cm={2,3,-1,2,(O)}},blue]
  (bl') grid (tr);
 \end{scope} 
 %
 \draw[thick,->] (O) node[below left]{\contour{white}{$O$}}
  -- ++ (vx,vy) node[above]{\contour{white}{$\vec v$}}; 
 \begin{scope}[cm={2,3,-1,2,(O)}]
  \draw[thick,->,blue] (O) -- ++ (vx,vy) node[above]{\contour{white}{$\vec  v\,'$}};
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容