如何将三维矢量投影到 XY 平面

如何将三维矢量投影到 XY 平面

我想制作一个简单的宏,将三维向量投影到 XY 平面,然后将投影到平面的向量投影到轴(启用并说明可以计算向量长度的方式)。 MWE 停止对相反方向的工作。(在 MWE 中,使用相同的三维坐标,但为了可见性,使用更高的 Z 值)。我做错了什么?

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}


\newcommand{\DrawWithXYProjections}[8]
{
    \coordinate (O) at (#1,#2,#3);  % First endpoint in space
    \coordinate (E) at (#4,#5,#6);  % Second endpoint in space
    \coordinate (TBegin) at (0,0,#3);   % First endpoint on axis t
    \coordinate (TBeginXY) at (#1,#2,#3);   % First endpoint on axis t
    \coordinate (TBeginX) at (0,#2,#3); % First endpoint on axis t
    \coordinate (TBeginY) at (#1,0,#3); % First endpoint on axis t
    \coordinate (TBegin) at (0,0,#3);   % First endpoint on axis t
    \coordinate (TEnd) at (0,0,#6); % First endpoint on axis t
    \coordinate (TEndX) at (0,#5,#3);   % First endpoint on axis t
    \coordinate (TEndY) at (#4,0,#3);   % First endpoint on axis t
    \coordinate (TEndXY) at (#4,#5,#3); % First endpoint on axis t
    \draw[vector,thin,color=blue] (TBeginXY) -- (TBeginX);
    \draw[vector,thin,color=blue] (TEndXY) -- (TEndY); % Draw Y projection in XY plane
    \draw[vector,thin,color=red] (TBeginXY) -- (TEndXY); % Draw projection to XY plane
    \draw[vector,color=green,dotted,#7] (O) -- (E) node[midway,above,black]{\scriptsize #8}; 
}

\begin{document}

\begin{tikzpicture}[scale=1.,axis/.style={->,blue,thick}, 
/pgfplots/view={15}{21},
vector/.style={-stealth,red,very thick}, 
vector guide/.style={dashed,red,thick}]
\begin{axis}[
axis lines=center,
axis on top,
every inner z axis line/.append style={opacity=0},
xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
domain=0:1,
y domain=0:2*pi,
xmin=-0.6, xmax=0.8,
ymin=-0.6, ymax=1.2, zmin=0.0,zmax=4.1,ztick={4},
every axis x label/.style={at={(rel axis cs:1,0.55,0)},anchor=north},
every axis y label/.style={at={(rel axis cs:0.35,.8,0)},anchor=south},
every axis z label/.style={at={(rel axis cs:0.25,0.45,0.36)},anchor=west},
samples=30]

\addplot3 [domain=0:360,samples y=1,name path=top,draw=none] ({1*cos(deg(x))},{1*sin(deg(x))},{1});
\path[name path=zline] (0,0,0) -- (0,0,1.5) coordinate(ztop);
\path[name intersections={of=top and zline,by={aux1}}];
\draw[-latex] (aux1) -- (ztop);
%standard tikz coordinate definition using x, y, z coords

\DrawWithXYProjections{-.3}{0}{0.1}  {0}{0.5}{0.5} {} {IsGood}

\DrawWithXYProjections {0}{0.5}{2.5} {-.3}{0}{2.1}  {} {IsBad}

\end{axis}
\end{tikzpicture}
\end{document}

答案1

我认为没有矛盾,你得到了你想要的箭头。但你可能正在寻找这个。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}


\newcommand{\DrawWithXYProjections}[8]
{
    \coordinate (O) at (#1,#2,#3);  % First endpoint in space
    \coordinate (E) at (#4,#5,#6);  % Second endpoint in space
    \coordinate (TBegin) at (0,0,#3);   % First endpoint on axis t
    \coordinate (TBeginXY) at (#1,#2,#3);   % First endpoint on axis t
    \coordinate (TBeginX) at (#4,#2,#3); % First endpoint on axis t
    \coordinate (TBeginY) at (#1,0,#3); % First endpoint on axis t
    \coordinate (TBegin) at (0,0,#3);   % First endpoint on axis t
    \coordinate (TEnd) at (0,0,#6); % First endpoint on axis t
    \coordinate (TEndX) at (0,#5,#3);   % First endpoint on axis t
    \coordinate (TEndY) at (#4,0,#3);   % First endpoint on axis t
    \coordinate (TEndXY) at (#4,#5,#3); % First endpoint on axis t
    \draw[vector,thin,color=blue] (#1,#2,#3) -- (#4,#2,#3);
    \draw[vector,thin,color=blue] (#4,#2,#3) -- (TEndXY); % Draw Y projection in XY plane
    \draw[vector,thin,color=red] (TBeginXY) -- (TEndXY); % Draw projection to XY plane
    \draw[vector,color=green,dotted,#7] (O) -- (E) node[midway,above,black]{\scriptsize #8}; 
}

\begin{document}

\begin{tikzpicture}[scale=1.,axis/.style={->,blue,thick}, 
/pgfplots/view={15}{21},
vector/.style={-stealth,red,very thick}, 
vector guide/.style={dashed,red,thick}]
\begin{axis}[
axis lines=center,
axis on top,
every inner z axis line/.append style={opacity=0},
xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
domain=0:1,
y domain=0:2*pi,
xmin=-0.6, xmax=0.8,
ymin=-0.6, ymax=1.2, zmin=0.0,zmax=4.1,ztick={4},
every axis x label/.style={at={(rel axis cs:1,0.55,0)},anchor=north},
every axis y label/.style={at={(rel axis cs:0.35,.8,0)},anchor=south},
every axis z label/.style={at={(rel axis cs:0.25,0.45,0.36)},anchor=west},
samples=30]

\addplot3 [domain=0:360,samples y=1,name path=top,draw=none] ({1*cos(deg(x))},{1*sin(deg(x))},{1});
\path[name path=zline] (0,0,0) -- (0,0,1.5) coordinate(ztop);
\path[name intersections={of=top and zline,by={aux1}}];
\draw[-latex] (aux1) -- (ztop);
%standard tikz coordinate definition using x, y, z coords

\DrawWithXYProjections{-.3}{0}{0.1}{0}{0.5}{0.5}{}{IsGood}

\DrawWithXYProjections{0}{0.5}{2.5}{-.3}{0}{2.1}{}{IsFine}

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容