# tikz-3dplot : discordance between polar and Cartesian coordinates ? (solved)

Recently I used the package tikz-3dplot to draw a figure until I was confronted to a very strange issue. I finally build a Minimal Working Example to isolate the problem and fully annotated it to simplify comprehension. My observation is simple. In a Cartesian frame I draw a vector using two methods. The first one, simply from its x,y,z coordinates and the second one from its polar coordinates. With the numerical values used, these two vectors, P and M (or N, using another method), must must be drawn identically as a vector collinear to the (1,1,1) vector. But they are not ! Maybe I am doing something wrong ???

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\include{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{120}
\begin{tikzpicture}[tdplot_main_coords]
% Origin
\coordinate (O) at (0,0,0) node[above left=1.0mm]{\large$O$};
% Point P, polar coordinates
\pgfmathsetmacro{\Prho}{5} %
\pgfmathsetmacro{\Ptheta}{45} %
\pgfmathsetmacro{\Pphi}{45} %
\tdplotsetcoord{P}{\Prho}{\Ptheta}{\Pphi};
% Drawt P as a vector
\draw[very thick,-latex,color=red] (O) -- (P) node[above left]{$P$};
% Draw P projection on xy plane
\draw[thick,dotted,color=red] (P) -- (Pxy) -- (O);
% Adjust frame axis lengthes (cosmetic)
\pgfmathsetmacro{\X}{\Prho+4}
\pgfmathsetmacro{\Y}{\Prho+1}
\pgfmathsetmacro{\Z}{\Prho+1.5}
% Cartesian frame
\draw[thick,-latex] (O) -- (\X,0,0) node[anchor=north east]{$x$};
\draw[thick,-latex] (O) -- (0,\Y,0) node[anchor=north west]{$y$};
\draw[thick,-latex] (O) -- (0,0,\Z) node[anchor=south]{$z$};
% Point M : cartesian coordinates
\pgfmathsetmacro{\ax}{\Prho*cos(45)}
\pgfmathsetmacro{\ay}{\Prho*cos(45)}
\pgfmathsetmacro{\az}{\Prho*cos(45)}
% Draw M
\draw[thick,-latex,color=blue] (O) -- (\ax,\ay,\az) node[above right]{$M$};
% Point N : compute its polar coordinates from M coordinates
\pgfmathsetmacro{\Nrho}{sqrt(\ax*\ax + \ay*\ay + \az*\az)}
\pgfmathsetmacro{\Ntheta}{acos(\az/\Nrho)}
\pgfmathsetmacro{\Nphi}{atan2(\ay, \ax)}
% Define and draw N using the preceding definitions
\tdplotsetcoord{N}{\Nrho}{\Ntheta}{\Nphi};
\draw[very thick,-latex,color=blue] (O) -- (N) node[below right]{$N$};
% Draw projection of N on xy plane
\draw[thick,dotted,color=blue] (N) -- (Nxy) -- (O);
% Draw the arc for phi on xy plane
\tdplotdrawarc[thick,-latex,color=blue]{(O)}{\Prho}{0}{45} {below,color=black} {$\phi=45$}
% Second method : compute for N its polar coordinates
\tdplotgetpolarcoords{\ax}{\ay}{\az}
% Arc for beta of P on (phi, z)  plane
\tdplotsetthetaplanecoords{\tdplotresphi}
\tdplotdrawarc[tdplot_rotated_coords,thin,-latex,color=red]{(O)}{\Prho}{0}{45} {left,color=black} {$45 = \theta$}
% Arc for beta of M or N on (phi, z)  plane
\tdplotdrawarc[tdplot_rotated_coords,thin,-latex,blue]{(O)}{6}{0} {\tdplotrestheta}{right,color=black}{$\theta' = \tdplotrestheta$}
\end{tikzpicture}
\end{document}


Patrick

• Welcome in this great community named TeX.SE. Commented Jun 14, 2021 at 11:57
• You might want to look at github.com/marmotghost/tikz-3dtools
– JeT
Commented Jun 14, 2021 at 14:14
• @JeT : I know tikz-3dtools but don't want to use this package. Please try the code above and look at this strange result ! I have no explanation except I am doing something wrong. Commented Jun 14, 2021 at 16:00
• @PatDae am a little padawan vs marmot. Maybe you can mention your question on tex top answers? You'll certainly get an answer.
– JeT
Commented Jun 14, 2021 at 16:26
• Figure replaced by a simpler one. Commented Jun 15, 2021 at 7:43

Juan, you understand correctly my question and you are right, the 3 angles between (1,1,1) and the 3 axis are of 54.73914° not 45°. Then vectors P and M are not collinear. I am sorry. Intuition can be a bad advisor. Thanks a lot. However, I was obliged to build a simple procedure to verify that this is true. I was not able to find an equivalent in tikz-3dplot, thus I give it to all. It works obviously in 3d :

\newcommand\AngleOfTwoVectors[6]{%
\pgfmathsetmacro{\VectorNormU}{sqrt(#1*#1 + #2*#2 + #3*#3)}%
\pgfmathsetmacro{\VectorNormV}{sqrt(#4*#4 + #5*#5 + #6*#6)}%
\pgfmathsetmacro{\ScalarProduct}{#1*#4 + #2*#5 + #3*#6}%
\pgfmathsetmacro{\AOTVtheta}{acos(\ScalarProduct/(\VectorNormU*\VectorNormV))}
Angle of vectors (#1, #2, #3) and (#4, #5, #6) : \AOTVtheta\\
}


Thanks a lot again,

Patrick

This question can be noted as [SOLVED].

• The way to mark a question as solved, is to accept an answer, by clicking the checkmark next to that answer. You have to wait 48h to accept a question of your own though (tex.stackexchange.com/help/accepted-answer). Commented Jun 15, 2021 at 15:09