2

I want to make a simple macro, that projects a 3-d vector to the XY plane, then projects the projected-to-the-plane vector to the axes (enabling and illustrating the way as the vector length can be calculated). The MWE stops working for the opposite direction. (In the MWE, there same 3-D coordinates are used, but for visibility, at a higher Z value). What do I wrong?

\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}
2
  • 1
    I cannot see any inconsistency. The first blue arrow is (TBeginXY) -- (TBeginX), i.e. from (#1,#2,#3) to (0,#2,#3). In your second command, \DrawWithXYProjections{0}{0.5}{2.5}{-.3}{0}{2.1}{}{IsBad}, #1 is 0, so you get an arrow of length 0, as expected. Likewise for the second blue arrow. Could you perhaps explain in more detail what you want to achieve?
    – user194703
    May 29, 2020 at 18:19
  • Well, I do have two vectors, from the beginning to the end, and rom the end to the beginning. I want to project both to the XY plane , that is what I get. IsGood produces the projections on the X and Y, respectively, IsBad does not. How can I branch the good and branch cases (I think I need some if..then..else)
    – katang
    May 29, 2020 at 18:39

1 Answer 1

2

I think there is no inconsistency, you get the arrows you are asking for. Yet you may be looking for this.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .