# TiKz. Angle between two vectors and a projection

I'm having trouble to draw something with TiKz. What I want to draw I think is simple:

SIM is a vector in the YZ-plane with a Beta = 38º, source is just a random vector with positive x and y components and negative z. Theta is the angle between SIM and source vectors and Phi is the angle formed by the source projection in the XZ-plane with the X-axis.

Here is a tikz-3dplot approach. The arcs are drawn using:

% define three points
\tdplotdefinepoints(vx, vy, vz)(ax, ay, az)(bx, by, bz)
% draw arc using the three points and a radius
\tdplotdrawpolytopearc[draw style]{r}{label options}{label}

The viewpoint can be modified using:

\tdplotsetmaincoords{rotation around x}{rotation around z}

The x-axis and z-axis can be exchanged using:

\let\raarotold\raarot \let\rbarotold\rbarot
\let\rabrotold\rabrot \let\rbbrotold\rbbrot
\let\racrotold\racrot \let\rbcrotold\rbcrot

\let\raarot\racrotold \let\rbarot\rbcrotold
\let\rabrot\rabrotold \let\rbbrot\rbbrotold
\let\racrot\raarotold \let\rbcrot\rbarotold

Note: The -z-axis in the following picture is actually the z-axis. I have just changed the label.

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

\begin{document}
\tdplotsetmaincoords{60}{120}

% customized coordinate system
\let\raarotold\raarot \let\rbarotold\rbarot
\let\rabrotold\rabrot \let\rbbrotold\rbbrot
\let\racrotold\racrot \let\rbcrotold\rbcrot

\let\raarot\racrotold \let\rbarot\rbcrotold
\let\rabrot\rabrotold \let\rbbrot\rbbrotold
\let\racrot\raarotold \let\rbcrot\rbarotold

\begin{tikzpicture}[tdplot_main_coords]

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

% vector 1
\pgfmathsetmacro{\ax}{5}
\pgfmathsetmacro{\ay}{5}
\pgfmathsetmacro{\az}{2}
\draw[very thick,->,red] (0,0,0) -- (\ax,\ay,\az) node[anchor=west]{source};

% vector 2
\pgfmathsetmacro{\bx}{0}
\pgfmathsetmacro{\by}{3}
\pgfmathsetmacro{\bz}{4}
\draw[very thick,->,blue] (0,0,0) -- (\bx,\by,\bz) node[anchor=north]{sim};

% vector 3 (projection)
\pgfmathsetmacro{\cx}{\ax*1.2}
\pgfmathsetmacro{\cy}{0}
\pgfmathsetmacro{\cz}{\az*1.2}
\draw[very thick,green] (0,0,0) -- (\cx,\cy,\cz);

% dashed lines
%  \draw[dashed,gray] (\ax,\ay,\az) -- (\ax,\ay,0);
\draw[dashed,gray] (\ax,\ay,\az) -- (\ax,0,\az);
\draw[dashed,gray] (\ax,\ay,\az) -- (0,\ay,\az);
%  \draw[dashed,gray] (\ax,0,0) -- (\ax,\ay,0) -- (0,\ay,0);
\draw[dashed,gray] (\ax,0,0) -- (\ax,0,\az) -- (0,0,\az);
\draw[dashed,gray] (0,0,\az) -- (0,\ay,\az) -- (0,\ay,0);

% arcs
\tdplotdefinepoints(0,0,0)(\ax,\ay,\az)(\bx,\by,\bz)
\tdplotdrawpolytopearc[<->]{2}{anchor=north west}{$\theta$}
\tdplotdefinepoints(0,0,0)(0,0,1)(\bx,\by,\bz)
\tdplotdrawpolytopearc[<->]{3}{anchor=north}{$\beta$}
\tdplotdefinepoints(0,0,0)(1,0,0)(\cx,\cy,\cz)
\tdplotdrawpolytopearc[<->]{4}{anchor=north}{$\phi$}

\end{tikzpicture}
\end{document}
• Beautiful solution. I always underestimate the drawing possibilities of tikz-3dplot. Is it possible to setup the axes like the OP had them in his question? – Philipp Imhof Sep 12 '15 at 15:41
• @PhilippImhof Thanks. The easiest would be just to change the axis node labels. Rotating the axis is kind of tricky. I am trying to avoid it. – sergej Sep 12 '15 at 15:46
• Sorry for asking (I could surely look it up in the manual, but maybe you know it right away): Is it another mechanism than in standard TikZ? Otherwise, you may try to copy the code from my example. – Philipp Imhof Sep 12 '15 at 15:54
• @PhilippImhof Many thanks for the hint! Axis configuration is described in section 2.1 "TikZ 3d Plotting" in the tikz-3dplot manual. Please see my edited answer. There is an issue though, \tdplotdefinepointsdoes not work as expected after changing the axis configuration. – sergej Sep 12 '15 at 16:48
• Thank you very much for this solution, finally Phi was the projection of the vector source in a plane containing the X-axis and normal to vector SIM, I will try to understand the code and adapt it to that new definition – JUAN G R Sep 13 '15 at 8:54

This should do the job. Feel free to adapt the styles.

I suggest you define the coordinates of source individually, so that the projection onto the two planes and the axes can be done more easily. (The let...in syntax is not an option, because it does not work with 3d coordinates.)

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{3d,angles,quotes,calc}

\begin{document}
\begin{tikzpicture}[axes/.style={thick,->},
z={(.5,{.5*sqrt(3)})},
y={({.5*sqrt(3)},-.5)},
x={(0,-.5,{.5*sqrt(3)})}]

\draw[axes] (0,0,0) coordinate (O) -- (6,0,0) coordinate (X) node [right]{$x$};
\draw[axes] (O) -- (0,6,0) node [above]{$y$};
\draw[axes,<->] (0,0,-6) coordinate (Z') node [below left] {$-z$}
-- (0,0,6) coordinate (Z) node [above right] {$z$} ;

\pgfmathsetmacro{\sourcex}{5}
\pgfmathsetmacro{\sourcey}{5}
\pgfmathsetmacro{\sourcez}{-2}
\coordinate (Source) at (\sourcex,\sourcey,\sourcez);
\coordinate (Source on xz) at (\sourcex,0,\sourcez);
\coordinate (Source on yz) at (0,5,-2);
\coordinate (Source on xz) at (5,0,-2);
\coordinate (Source on x axis) at (5,0,0);
\coordinate (Source on y axis) at (0,5,0);
\coordinate (Source on z axis) at (0,0,-2);

\begin{scope}[canvas is yz plane at x=0]
\coordinate (SIM) at (-38:4);
\end{scope}

\draw[dotted] (\sourcex,0,\sourcez) -- (Source) -- (0,\sourcey,\sourcez);
\draw[dotted] (\sourcex,0,0) -- (Source on xz) -- (0,0,\sourcez);
\draw[dotted] (0,\sourcey,0) -- (0,\sourcey,\sourcez) -- (0,0,\sourcez);

\pic [draw,angle radius=.6cm,angle eccentricity=1.4,"$\phi$"]
{angle = X--O--Source on xz};
\pic [draw,angle radius=.5cm,angle eccentricity=1.5,"$\beta$"]
{angle = Z'--O--SIM};
\pic [draw,fill=white,angle radius=.7cm,angle eccentricity=.6,"$\theta$"]
{angle = SIM--O--Source};

\draw (O) -- (Source on xz);
\draw[->] (0,0,0) -- (Source) node[above right] {Source};
\draw[->] (0,0) --  (SIM) node[below] {Sim};
\end{tikzpicture}
\end{document}

You can draw this shape in geogebra, and export it to tikz, easily. This is a sample:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}
\begin{document}
\definecolor{qqwuqq}{rgb}{0.,0.39215686274509803,0.}
\definecolor{qqqqff}{rgb}{0.,0.,1.}
\definecolor{cqcqcq}{rgb}{0.7529411764705882,0.7529411764705882,0.7529411764705882}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw [color=cqcqcq,dash pattern=on 3pt off 3pt, xstep=1.0cm,ystep=1.0cm] (-4.3,-2.66) grid (7.3,6.3);
\clip(-4.3,-2.66) rectangle (7.3,6.3);
\draw [shift={(1.,0.)},color=qqwuqq,fill=qqwuqq,fill opacity=0.1] (0,0) -- (-26.56505117707799:0.6) arc (-26.56505117707799:90.:0.6) -- cycle;
\draw (1.,3.)-- (1.,0.);
\draw (1.,0.)-- (5.,-2.);
\begin{scriptsize}
\draw [fill=qqqqff] (1.,3.) circle (1.5pt);
\draw[color=qqqqff] (1.14,3.28) node {$A$};
\draw [fill=qqqqff] (1.,0.) circle (1.5pt);
\draw[color=qqqqff] (1.14,0.28) node {$B$};
\draw[color=black] (0.74,1.68) node {$a$};
\draw [fill=qqqqff] (5.,-2.) circle (1.5pt);
\draw[color=qqqqff] (5.14,-1.72) node {$C$};
\draw[color=qqwuqq] (2.64,0.44) node {$\alpha = 116.57\textrm{\degre}$};
\draw[color=black] (2.9,-1.12) node {$b$};
\end{scriptsize}
\end{tikzpicture}
\end{document}