3

I am trying to use tikz-3dplot to draw a cube and I was particularly interested by the possibility to define a point using \tdplotsetcoord which allows to get x/y/z/xz... coordinates. But when I try to define a point in (4,4,4) of the main coordinate system of tikz-3dplot (so \tdplotsetcoord{P}{sqrt(3)*4}{45}{45} I think), I do not get what I am expecting as you can see below. The blue and black nodes should be the same in my figure. Any ideas ?

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

\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords]

% cube
\tdplotsetcoord{P}{sqrt(3)*4}{45}{45}
\draw[dashed] 
  (0,0,0) -- (Px)
  (0,0,0) -- (Py)
  (0,0,0) -- (Pz);
\draw[->] 
  (Px) -- ++ (1,0,0) node[anchor=north east]{$x$};
\draw[->]
   (Py) -- ++(0,1,0) node[anchor=north west]{$y$};
\draw[->] 
  (Pz) -- ++(0,0,1) node[anchor=south]{$z$};

\draw[thick]
  (Pxz) -- (P) -- (Pxy) -- (Px) -- (Pxz) -- (Pz) -- (Pyz) -- (P); 
\draw[thick]
  (Pyz) -- (Py) -- (Pxy);

\filldraw[dashed,blue] (0,0,0)-- (P) circle (2pt);

\filldraw[dashed] (0,0,0)-- (4,4,4) circle (2pt);

\end{tikzpicture}
\end{document}

enter image description here

  • 2
    Welcome to TeX-SE! Could you please explain why you expect that(r*cos(theta)*cos(phi),r*cos(theta)*sin(phi),r*sin(theta))=(4,4,4) for theta=phi=45? – user121799 Apr 2 at 13:49
3

Welcome to TeX-SE! One way to compute the spherical coordinates of (4,4,4) is to solve

(r*cos(theta)*cos(phi),r*cos(theta)*sin(phi),r*sin(theta))=(4,4,4)

which gives r=4*sqrt(3) and phi=45, as you got, and theta=asin(sqrt(2/3)), which differs from what you have. BTW, you can use also the spherical coordinates of the 3d library, which follow slightly different conventions. The following MWE shows that the outcome is consistent with the above considerations.

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

\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords]
\pgfmathsetmacro{\mytheta}{asin(sqrt(2/3))}
\typeout{\mytheta}
% cube
\tdplotsetcoord{P}{sqrt(3)*4}{\mytheta}{45}
\draw[dashed] 
  (0,0,0) -- (Px)
  (0,0,0) -- (Py)
  (0,0,0) -- (Pz);
\draw[->] 
  (Px) -- ++ (1,0,0) node[anchor=north east]{$x$};
\draw[->]
   (Py) -- ++(0,1,0) node[anchor=north west]{$y$};
\draw[->] 
  (Pz) -- ++(0,0,1) node[anchor=south]{$z$};

\draw[thick]
  (Pxz) -- (P) -- (Pxy) -- (Px) -- (Pxz) -- (Pz) -- (Pyz) -- (P); 
\draw[thick]
  (Pyz) -- (Py) -- (Pxy);

\filldraw[dashed,blue] (0,0,0)-- (P) circle (2pt);

\draw[green!70!black] (4,4,4) circle (3pt);

\draw[red] (xyz spherical cs:radius={4*sqrt(3)},
  longitude=45,latitude=90-\mytheta) circle (4pt);
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.