# How to find angle of vector sum

(Hey all, i think this is my first question here, so I'll try to get it right)

I am trying to create a space vector sine wave. The phasor is created as the sum of the three phases, but I cannot get the actual angle of the space vector (black phasor in image).

What I need:

• The polar coordinates of the end of the black arrow (vstip) relative to the circle center (origo).

or

• The cartesian coordinates relative to origo, and a function that can properly convert those two numbers to an angle. (i cannot use atan{} alone as it has a few limitations for angles greater than 90)

Code:

 \documentclass[a4paper,landscape]{article}
\usepackage{pgfplots}
\usepackage{amsmath} % Required for \varPsi below
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}

%Definitions%
%===========================================================
\def \Vs {4}; %Magnitude of space vector in cm
\def \Angle {70}; %Angle with regards to phase A
\pgfmathsetmacro \Marker {\Angle/45)};
\pgfmathsetmacro \Vmax {\Vs * 2/3}; %Max phase voltage
\def \Axisheight {\Vmax *3};
\pgfmathsetmacro \Va {\Vmax * sin{(\Angle+0)}}; %Magnitude of phase A at angle \Angle
\pgfmathsetmacro \Vb {\Vmax * sin{(\Angle+120)}}; %Magnitude of phase B at angle \Angle
\pgfmathsetmacro \Vc {\Vmax * sin{(\Angle+240)}}; %Magnitude of phase C at angle \Angle
\pgfmathsetmacro \Radius {\Vmax}; %Radius of maximum line to line voltage output

%Locations%
%===========================================================
\def \OrigoX {-5 cm};
\def \OrigoY {0 cm};
\coordinate (origo) at (\OrigoX,\OrigoY); %Center of phasor diagram
\coordinate (phase_a) at (0:\Va cm); %Tip of phase A
\coordinate (phase_b) at (120:\Vb cm); %Tip of phase B
\coordinate (phase_c) at (240:\Vc cm) ; %Tip of phase C

%Circle background rings%
%===========================================================
\node at (origo) [circle,draw=black!80,fill=red!0, inner sep=0pt,minimum size=\Vs*2 cm] {}; %Circle with radius 128%
\node at (origo) [circle,draw=black!80,fill=green!0, inner sep=0pt,minimum size=\Vmax*2 cm, dashed] {}; %Circle with radius 100%

%Black guide lines with angle notation
\foreach \x in {45,90,...,360} {
\draw[-,color=black,dotted, thin] (origo)  -- ++(\x:\Vs cm) node[label={[label distance=-0.18cm]\x: $\x^{\circ}$}] {};
}

%Phasor guide lines%
%===========================================================
\draw[-,color=red] (origo)  -- ++(0:\Vmax cm)  coordinate (Y) node[anchor=west] {\large $i_a$};
\draw[-,color=blue] (origo) -- ++(120:\Vmax cm) node[anchor=south] {\large $i_b$};
\draw[-,color=green] (origo)  -- ++(240:\Vmax cm) node[anchor=north] {\large $i_c$};

%Actual phasors%
%===========================================================
\draw[->,color=red, very thick] (origo)  -- ++(phase_a); %Phase A phasor
\draw[->,color=blue, very thick] (origo)  -- ++(phase_b); %Phase B phasor
\draw[->,color=green, very thick] (origo)  -- ++(phase_c); %Phase C phasor

\draw[->,ultra thick,black] (origo) -- ++($(phase_a) + (phase_b) + (phase_c)$) coordinate(Vstip); %Space vector resultant
\pgfgetlastxy{\XCoord}{\YCoord}; % X and Y coordinates of Space vector tip
\draw[->,color=blue, thin] (origo) ++(phase_a) -> ++(phase_b); %Resultant help lines
\draw[->,color=green, thin] (origo) ++(phase_a) ++(phase_b) -> ++(phase_c); %Resultant help lines

%Three phase sinusoidal voltages%
%===========================================================
\draw[color=red,thick,smooth,domain=0:8] plot(\x,{\Vmax * sin(\x*45)}); % Phase A
\draw[color=blue,thick,,smooth,domain=0:8] plot(\x,{\Vmax * sin((\x*45)+120)}); %Phase B
\draw[color=green,thick,,smooth,domain=0:8] plot(\x,{\Vmax * sin((\x*45)+240)}); % Phase C

\draw[->, color=black] (0,0) -- (9,0) node[anchor=west] {$\theta$}; %X axis
\draw[->, color=black] (0,-\Vs) -- (0,\Vs) node[anchor=south] {$V$}; %Y axis

\draw[color=red,dashed] (\Marker,\Va) -- (-0.3,\Va)  node[anchor=east] {$i_a$};; %A phase line to Y-axis
\draw[color=blue,dashed] (\Marker,\Vb)-- (-0.3,\Vb)  node[anchor=east] {$i_b$};; %B phase line to Y-axis
\draw[color=green,dashed] (\Marker,\Vc)-- (-0.3,\Vc)  node[anchor=east] {$i_c$};; %C phase line to Y-axis

\foreach \x in {1, 2,...,8} {
\pgfmathsetmacro \tick {\x * 45};
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {\small\pgfmathprintnumber[fixed, precision=10]{\tick}$^{\circ}$}; %X Axis tick marks
}

\draw[color=black] (\Marker cm, \Radius cm) -- (\Marker cm, -\Vs cm) node[anchor=north,align=center]{Current \\ position: \\ $\Angle^{\circ}$}; %Marker

\draw[color=gray] (-5,\Vmax) -- (9,\Vmax) ; %X axis TOP
\draw[color=gray] (-5,-\Vmax) -- (9,-\Vmax) ; %X axis bottom
\draw[help lines] (0,-\Vs) grid (9 ,\Vs); %Grid

\end{tikzpicture}

\end{document}

• Edit: I have found the coordinates in cartesian form. They were there in the code all the way: \pgfgetlastxy{\XCoord}{\YCoord};. I thought it didnt work because I did not realize that the variables are case sensitive. Now i need a proper way to convert the numbers to angle. – fluxmodel Jun 14 '15 at 5:07

## 1 Answer

\pgfmathanglebetweenpoints helps:

\draw[->,ultra thick,black] (origo) -- ++($(phase_a) + (phase_b) + (phase_c)$) coordinate(Vstip);

\pgfmathanglebetweenpoints{\pgfpointanchor{origo}{center}}
{\pgfpointanchor{Vstip}{center}}
\let\PhaseBlack\pgfmathresult


Result is phase in macro \PhaseBlack: 19.99997

• Holy christ. It worked perfectly! – fluxmodel Jun 14 '15 at 5:57