我正在尝试理解 Tikz 的基础变化。为此,我尝试模仿\tdplotsetmaincoords{\alpha}{\beta}
。tikz-3dplot
这是一个代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
% Definition and projection on the screen of basis A
\coordinate (Ax) at (1,0);%,0);
\coordinate (Ay) at (0,1);%,0);
\coordinate (Az) at (0,0);%,1);
% Draw basis A
\draw[blue,-latex] (0,0,0) -- (Ax) node[shift={(0.1,0,0)}] {$e_x$};
\draw[red,-latex] (0,0,0) -- (Ay) node[shift={(0,0.1,0)}] {$e_y$};
\draw[green,-latex] (0,0,0) -- (Az) node[shift={(0,0,0.1)}] {$e_z$};
% Define angles
\pgfmathsetmacro\alpha{70}
\pgfmathsetmacro\beta{110}
% Rotation matrix from basis A (canonical) and basis B (R = Rx(-\alpha)*Rz(-\beta))
\pgfmathsetmacro\Rxx{cos(\beta)}
\pgfmathsetmacro\Rxy{-cos(\alpha)*sin(\beta)}
\pgfmathsetmacro\Rxz{sin(\alpha)*sin(\beta)}
\pgfmathsetmacro\Ryx{sin(\beta)}
\pgfmathsetmacro\Ryy{cos(\alpha)*cos(\beta)}
\pgfmathsetmacro\Ryz{-sin(\alpha)*cos(\beta)}
\pgfmathsetmacro\Rzx{0}
\pgfmathsetmacro\Rzy{sin(\alpha)}
\pgfmathsetmacro\Rzz{cos(\alpha)}
% Point X in basis A
\pgfmathsetmacro\xa{1}
\pgfmathsetmacro\ya{1}
\pgfmathsetmacro\za{1}
% Change basis from natural tikz (with fake perspective) to A
\begin{scope}[shift={(0,0,0)},x={(Ax)},y={(Ay)},z={(Az)}]
% Draw X in A
\draw (0,0,0) -- (\xa,\ya,\za) node {\tiny $\bullet$};
\draw (0,0,0) -- (\xa,\ya,\za) node[right] {\tiny $X_a$};
\end{scope}
% Point X in basis B (Xb = R*Xa)
\pgfmathsetmacro\xb{\Rxx*\xa+\Ryx*\ya+\Rzx*\za}
\pgfmathsetmacro\yb{\Rxy*\xa+\Ryy*\ya+\Rzy*\za}
\pgfmathsetmacro\zb{\Rxz*\xa+\Ryz*\ya+\Rzz*\za}
% Projection of B on the screen
\coordinate (Bx) at (\Rxx,\Rxy);%,\Rxz);
\coordinate (By) at (\Ryx,\Ryy);%,\Ryz);
\coordinate (Bz) at (\Rzx,\Rzy);%,\Rzz);
% Change basis from A to B
\begin{scope}[shift={(0,0,0)},x={(Bx)},y={(By)},z={(Bz)}]
% Draw basis B
\draw[blue,-latex] (0,0,0) -- (1,0,0) node[shift={(0.1,0,0)}] {$e_{x'}$};
\draw[red,-latex] (0,0,0) -- (0,1,0) node[shift={(0,0.1,0)}] {$e_{y'}$};
\draw[green,-latex] (0,0,0) -- (0,0,1) node[shift={(0,0,0.1)}] {$e_{z'}$};
% Draw X in B
\draw (0,0,0) -- (\xb,\yb,\zb) node {\tiny $\bullet$};
\draw (0,0,0) -- (\xb,\yb,\zb) node[right] {\tiny $X_b$};
\end{scope}
\end{tikzpicture}
\end{document}
为什么点 Xa 和 Xb 不在同一位置?它们应该是在同一位置,不是吗?我做错了什么?
附录
任何有关包装矩阵操作的想法都非常棒。
答案1
我认为你只是忘了转置,即反转变换矩阵,这是所谓的“主动”和“被动”转变关键字“transpose”已经被 StefanH 提及,但是由于某种原因,他似乎忘记了实现它,或者他通过“转置基础”表示其他意思。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[scale=2]
% Definition and projection on the screen of basis A
\coordinate (Ax) at (1,0);%,0);
\coordinate (Ay) at (0,1);%,0);
\coordinate (Az) at (0,0);%,1);
% Draw basis A
\draw[blue,-latex] (0,0,0) -- (Ax) node[shift={(0.1,0,0)}] {$e_x$};
\draw[red,-latex] (0,0,0) -- (Ay) node[shift={(0,0.1,0)}] {$e_y$};
\draw[green,-latex] (0,0,0) -- (Az) node[shift={(0,0,0.1)}] {$e_z$};
% Define angles
\pgfmathsetmacro\alpha{70}
\pgfmathsetmacro\beta{110}
% Rotation matrix from basis A (canonical) and basis B (R = Rx(-\alpha)*Rz(-\beta))
\pgfmathsetmacro\Rxx{cos(\beta)}
\pgfmathsetmacro\Rxy{-cos(\alpha)*sin(\beta)}
\pgfmathsetmacro\Rxz{sin(\alpha)*sin(\beta)}
\pgfmathsetmacro\Ryx{sin(\beta)}
\pgfmathsetmacro\Ryy{cos(\alpha)*cos(\beta)}
\pgfmathsetmacro\Ryz{-sin(\alpha)*cos(\beta)}
\pgfmathsetmacro\Rzx{0}
\pgfmathsetmacro\Rzy{sin(\alpha)}
\pgfmathsetmacro\Rzz{cos(\alpha)}
% Point X in basis A
\pgfmathsetmacro\xa{1}
\pgfmathsetmacro\ya{1}
\pgfmathsetmacro\za{1}
% Change basis from natural tikz (with fake perspective) to A
\begin{scope}[shift={(0,0,0)},x={(Ax)},y={(Ay)},z={(Az)}]
% Draw X in A
\draw[ultra thick,blue,-Circle] (0,0,0) -- (\xa,\ya,\za)
node[left=2pt,font=\tiny] {$X_a$};
\end{scope}
% Point X in basis B (Xb = R*Xa)
% <- transposed this matrix by replacing \Rxy with \Ryx etc.
\pgfmathsetmacro\xb{\Rxx*\xa+\Rxy*\ya+\Rxz*\za}
\pgfmathsetmacro\yb{\Ryx*\xa+\Ryy*\ya+\Ryz*\za}
\pgfmathsetmacro\zb{\Rzx*\xa+\Rzy*\ya+\Rzz*\za}
% Projection of B on the screen
\coordinate (Bx) at (\Rxx,\Rxy);%,\Rxz);
\coordinate (By) at (\Ryx,\Ryy);%,\Ryz);
\coordinate (Bz) at (\Rzx,\Rzy);%,\Rzz);
\typeout{\Rxx,\Rxy;\Ryx,\Ryy;\Rzx,\Rzy}
% Change basis from A to B
\begin{scope}[shift={(0,0,0)},x={(Bx)},y={(By)},z={(Bz)}]
% Draw basis B
\draw[blue,-latex] (0,0,0) -- (1,0,0) node[shift={(0.1,0,0)}] {$e_{x'}$};
\draw[red,-latex] (0,0,0) -- (0,1,0) node[shift={(0,0.1,0)}] {$e_{y'}$};
\draw[green,-latex] (0,0,0) -- (0,0,1) node[shift={(0,0,0.1)}] {$e_{z'}$};
% Draw X in B
\draw[red,thick,-Circle] (0,0,0) -- (\xb,\yb,\zb)
node[right,font=\tiny] {$X_b$};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
转置有问题。基础(Bx,By,Bz)
或映射(\xb,\yb,\yc)
被转置了,但我不确定是哪一个(老实说,我开始搞不清楚哪个是什么了:)。如果我使用
\pgfmathsetmacro\xc{\Rxx*\xa+\Rxy*\ya+\Rxz*\za}
\pgfmathsetmacro\yc{\Ryx*\xa+\Ryy*\ya+\Ryz*\za}
\pgfmathsetmacro\zc{\Rzx*\xa+\Rzy*\ya+\Rzz*\za}
并在范围内添加
\draw[red,fill=red,opacity=0.5] (\xc,\yc,\zc) circle (1pt);
我明白了
3D 坐标
代码中有些地方有两个坐标,有些地方有三个。如果我把它们都改成 3D,就会得到
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
% Definition and projection on the screen of basis A
\coordinate (Ax) at (1,0,0);
\coordinate (Ay) at (0,1,0);
\coordinate (Az) at (0,0,1);
% Draw basis A
\draw[blue,-latex] (0,0,0) -- (Ax) node[shift={(0.1,0,0)}] {$e_x$};
\draw[red,-latex] (0,0,0) -- (Ay) node[shift={(0,0.1,0)}] {$e_y$};
\draw[green,-latex] (0,0,0) -- (Az) node[shift={(0,0,0.1)}] {$e_z$};
% Define angles
\pgfmathsetmacro\alpha{70}
\pgfmathsetmacro\beta{110}
% Rotation matrix from basis A (canonical) and basis B (R = Rx(-\alpha)*Rz(-\beta))
\pgfmathsetmacro\Rxx{cos(\beta)}
\pgfmathsetmacro\Rxy{-cos(\alpha)*sin(\beta)}
\pgfmathsetmacro\Rxz{sin(\alpha)*sin(\beta)}
\pgfmathsetmacro\Ryx{sin(\beta)}
\pgfmathsetmacro\Ryy{cos(\alpha)*cos(\beta)}
\pgfmathsetmacro\Ryz{-sin(\alpha)*cos(\beta)}
\pgfmathsetmacro\Rzx{0}
\pgfmathsetmacro\Rzy{sin(\alpha)}
\pgfmathsetmacro\Rzz{cos(\alpha)}
% Point X in basis A
\pgfmathsetmacro\xa{1}
\pgfmathsetmacro\ya{1}
\pgfmathsetmacro\za{1}
% Change basis from natural tikz (with fake perspective) to A
\begin{scope}[shift={(0,0,0)},x={(Ax)},y={(Ay)},z={(Az)}]
% Draw X in A
\draw (0,0,0) -- (\xa,\ya,\za) node {\tiny $\bullet$} node[right] {\tiny $X_a$};
\end{scope}
% Point X in basis B (Xb = R*Xa)
\pgfmathsetmacro\xb{\Rxx*\xa+\Ryx*\ya+\Rzx*\za}
\pgfmathsetmacro\yb{\Rxy*\xa+\Ryy*\ya+\Rzy*\za}
\pgfmathsetmacro\zb{\Rxz*\xa+\Ryz*\ya+\Rzz*\za}
\pgfmathsetmacro\xc{\Rxx*\xa+\Rxy*\ya+\Rxz*\za}
\pgfmathsetmacro\yc{\Ryx*\xa+\Ryy*\ya+\Ryz*\za}
\pgfmathsetmacro\zc{\Rzx*\xa+\Rzy*\ya+\Rzz*\za}
% Projection of B on the screen
\coordinate (Bx) at (\Rxx,\Rxy,\Rxz);
\coordinate (By) at (\Ryx,\Ryy,\Ryz);
\coordinate (Bz) at (\Rzx,\Rzy,\Rzz);
% Change basis from A to B
\begin{scope}[shift={(0,0,0)},x={(Bx)},y={(By)},z={(Bz)}]
% Draw basis B
\draw[blue,-latex] (0,0,0) -- (1,0,0) node[shift={(0.1,0,0)}] {$e_{x'}$};
\draw[red,-latex] (0,0,0) -- (0,1,0) node[shift={(0,0.1,0)}] {$e_{y'}$};
\draw[green,-latex] (0,0,0) -- (0,0,1) node[shift={(0,0,0.1)}] {$e_{z'}$};
% Draw X in B
\draw (0,0,0) -- (\xb,\yb,\zb) node {\tiny $\bullet$} node[right] {\tiny $X_b$};
\draw[red,fill=red,opacity=0.5] (\xc,\yc,\zc) circle (1pt);
\end{scope}
\end{tikzpicture}
\end{document}