Tikz 中的 2D 和 3D 向量

Tikz 中的 2D 和 3D 向量

我想在 Tikz 中繪製一些具有某些屬性的 2D 和 3D 向量。下面的原始圖總結了我想要的:

G

  • 我只需要笛卡爾座標系的第一象限來表示。

  • 我需要將向量連接到不同維度上的對應值(不是投影,只是正交虛線)。

下面的程式碼段實現了我想要在 3D 中實現的目標,但有兩個例外:

  • 給出 xy 平面和 z 維度上的投影,而不是將向量的頭連接到每個維度中的對應值。

  • 尺寸中缺少數值。

代碼:

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}

\tdplotsetmaincoords{60}{120}
\begin{tikzpicture}
    [scale=3,
    tdplot_main_coords,
    axis/.style={->,blue,thick},
    vector/.style={-stealth,red,very thick},
    vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
    \draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
    \draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);
\end{tikzpicture}
\end{document}

答案1

此替代方案提供笛卡爾座標,作為 Perter Grill 解決方案的補充。

在此輸入影像描述

程式碼

\documentclass[tikz,border=1cm]{standalone} 
\usepackage{tikz-3dplot} 
\begin{document}

\tdplotsetmaincoords{60}{120} 
\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick}, 
vector/.style={-stealth,red,very thick}, 
vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using x, y, z coords

\pgfmathsetmacro{\ax}{0.8}
\pgfmathsetmacro{\ay}{0.8}
\pgfmathsetmacro{\az}{0.8}

\coordinate (P) at (\ax,\ay,\az);

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide]         (O) -- (\ax,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (P);
\draw[vector guide]         (P) -- (0,0,\az);
\draw[vector guide] (\ax,\ay,0) -- (0,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (0,\ay,0);
\draw[vector guide] (\ax,\ay,0) -- (\ax,0,0);
\node[tdplot_main_coords,anchor=east]
at (\ax,0,0){(\ax, 0, 0)};
\node[tdplot_main_coords,anchor=west]
at (0,\ay,0){(0, \ay, 0)};
\node[tdplot_main_coords,anchor=south]
at (0,0,\az){(0, 0, \az)};
\end{tikzpicture}
\end{document}

答案2

我認為黑色(或洋紅色)的線條是您正在尋找的線條:

在此輸入影像描述

為了計算笛卡爾座標,我使用\pgfmathsetmacro.

筆記:

  • 我不知道是否可以輕鬆地直接從設定中提取 x、y 和 z 座標,\tdplotsetcoord因此我必須單獨定義它們。

代碼:

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}

\tdplotsetmaincoords{60}{120}

\newcommand{\Prho}{.8}%
\newcommand{\Ptheta}{55}%
\newcommand{\Pphi}{60}%

\begin{tikzpicture}
    [scale=3,
    tdplot_main_coords,
    axis/.style={->,blue,thick},
    vector/.style={-stealth,red,very thick},
    vector guide/.style={dashed,red,thick}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{\Prho}{\Ptheta}{\Pphi}

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);

% Compute x,y,z
\pgfmathsetmacro{\PxCoord}{\Prho * sin(\Pphi) * cos(\Ptheta)}%
\pgfmathsetmacro{\PyCoord}{\Prho * sin(\Pphi) * sin(\Ptheta)}%
\pgfmathsetmacro{\PzCoord}{\Prho * cos(\Pphi)}%

\draw[vector guide, black] (Pxy) -- (Px) node [left]  {\PxCoord};
\draw[vector guide, black] (Pxy) -- (Py) node [above right] {\PyCoord};

\draw[vector guide, magenta] (P) -- (Pxz) node [left]  {\PxCoord};
\draw[vector guide, magenta] (P) -- (Pyz) node [right] {\PyCoord};

\end{tikzpicture}
\end{document}

相關內容