# Phasors with tikz

This afternoon I was looking for a way to draw phasors with tikz (you know the thing which is represented in the complex plane as r*exp(i \theta) or simply x + iy)

For me, the first representation would be much easier to deal with.

Should I use the polar system simply ? But then it comes tricky to make vector summations...

-
Are you meaning something like an "A" followed by an angle symbol with the angle measure inside it? The one explained in en.wikipedia.org/wiki/Angle_notation – egreg May 23 '12 at 21:10
Do you just want to draw a vector? TikZ can everything in polar coordinates (including the +(coordinate) movement/addition operation). – Caramdir May 23 '12 at 21:22
mhh intersting. thanks for the reply – s__C May 25 '12 at 7:45
@s__C Would you please better state what you need? – egreg Jun 2 '12 at 22:37

The summation is not that tricky if you just use the regular polar form. Here is a simple starter:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[>=latex]
\draw[style=help lines] (0,0) grid[step=1cm] (3,2);

\coordinate (vec1) at (60:1); % Define some complex numbers
\coordinate (vec2) at (20:2); % with r*exp(i \theta) form

\draw[->,thick,blue] (0,0) -- (vec1);
\draw[->,thick,red] (0,0) -- (vec2);
\draw[dashed] (vec2) -- ++(vec1); % This how to add a vector to another
\draw[dashed] (vec1) -- ++(vec2); % using the coordinates as vectors
\draw[->,thick,orange] (0,0) -- ($(vec1) + (vec2)$); % This is how to do arithmetic
% operations on the coordinates.
% (requires calc library of TikZ.
% See the manual )
\end{tikzpicture}
\end{document}


-
This is exactly what I was looking for. Thank you ! – s__C Jun 4 '12 at 11:40
@s__C: If this answers you question you may accept it by clicking the gray checkmark below the vote buttons. This will bring some reputation to percusse and you. – Tobi Jul 29 '12 at 8:52

Here are 3 variants: for complex numbers a+bi, re^phi*i(where phi is an angle in rad) and re^phi*i (where phi in an angle in deg). For the first you specify the origin, a, b and the draw style, for the others the origin, r, phi and the draw style:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows}

\newdimen\xorig
\newdimen\yorig

\newcommand{\sumphasor}[4]% origin, a, b, style; (for z=a+bi)
{   \path (#1);
\pgfgetlastxy{\xorig}{\yorig}
\pgfmathsetmacro{\xfirst}{\xorig/28.453}
\pgfmathsetmacro{\yfirst}{\yorig/28.453}
\pgfmathsetmacro{\xsecond}{\xorig/28.453+#2}
\pgfmathsetmacro{\ysecond}{\yorig/28.453+#3}
\draw[#4] (\xfirst,\yfirst) -- (\xsecond,\ysecond);
}

\newcommand{\expphasorpi}[4]% origin, r, phi(pi), style
{   \path (#1);
\pgfgetlastxy{\xorig}{\yorig}
\pgfmathsetmacro{\xfirst}{\xorig/28.453}
\pgfmathsetmacro{\yfirst}{\yorig/28.453}
\pgfmathsetmacro{\xsecond}{\xorig/28.453+#2*cos(#3/pi*180)}
\pgfmathsetmacro{\ysecond}{\yorig/28.453+#2*sin(#3/pi*180)}
\draw[#4] (\xfirst,\yfirst) -- (\xsecond,\ysecond);
}

\newcommand{\expphasordeg}[4]% origin, r, phi(pi), style
{   \path (#1);
\pgfgetlastxy{\xorig}{\yorig}
\pgfmathsetmacro{\xfirst}{\xorig/28.453}
\pgfmathsetmacro{\yfirst}{\yorig/28.453}
\pgfmathsetmacro{\xsecond}{\xorig/28.453+#2*cos(#3)}
\pgfmathsetmacro{\ysecond}{\yorig/28.453+#2*sin(#3)}
\draw[#4] (\xfirst,\yfirst) -- (\xsecond,\ysecond);
}

\begin{document}

\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0);
\draw[->] (0,-4) -- (0,4);
\foreach \x in {-4,...,4}
{   \draw (-0.1,\x) -- (0.1,\x);
\draw (\x,-0.1) -- (\x,0.1);
}
\sumphasor{0,0}{3}{-2}{red,->,thick}
\sumphasor{0,0}{1}{3}{blue,->,thick}
\sumphasor{0,0}{4}{-0.3}{green,->,thick}
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0);
\draw[->] (0,-4) -- (0,4);
\foreach \x in {-4,...,4}
{   \draw (-0.1,\x) -- (0.1,\x);
\draw (\x,-0.1) -- (\x,0.1);
}
\expphasorpi{0,0}{3}{0.785}{red,->,thick} % pi/4
\expphasorpi{0,0}{2}{1.047}{blue,->,thick} % 1/3*pi
\expphasorpi{0,0}{4}{-0.785}{green,->,thick} %-pi/4
\end{tikzpicture}

\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0);
\draw[->] (0,-4) -- (0,4);
\foreach \x in {-4,...,4}
{   \draw (-0.1,\x) -- (0.1,\x);
\draw (\x,-0.1) -- (\x,0.1);
}
\expphasordeg{0,0}{3}{-30}{red,->,thick} % pi/4
\expphasordeg{0,0}{2}{75}{blue,->,thick} % 2/3*pi
\expphasordeg{0,0}{4}{-45}{green,->,thick} %-pi/4
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0);
\draw[->] (0,-4) -- (0,4);
\foreach \x in {-4,...,4}
{   \draw (-0.1,\x) -- (0.1,\x);
\draw (\x,-0.1) -- (\x,0.1);
}
\sumphasor{0,0}{3}{-2}{red,-latex,thick}
\sumphasor{0,0}{1}{3}{blue,-latex,thick}
\sumphasor{3,-2}{1}{3}{densely dashed,thick}
\sumphasor{1,3}{3}{-2}{densely dashed,thick}
\sumphasor{0,0}{4}{1}{green!50!gray,-latex,thick}
\end{tikzpicture}

\end{document}


-

It is taken from Phasor Diagram:

%& -shell-escape
% Phasor diagram
% Author: Urs Zellweger (urs@zellweger.li)
% GNUPLOT required
\documentclass[11pt]{article}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{5pt}%
%%%>

\begin{comment}
:Title: Phasor diagram

\end{comment}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgffor}
\usepgfmodule{shapes}
\usepgfmodule{plot}
\usetikzlibrary{decorations}
\usetikzlibrary{arrows}
\usetikzlibrary{snakes}

\newcommand{\Gitter}[4]{
\draw[very thin,color=gray] (#1,#3) grid (#2,#4);
}
\newcommand{\Koordinatenkreuz}[6]{
\draw[->, >=latex, color=green!50!black] (#1,0) -- (#2,0) node[right] {#5};
\draw[->, >=latex, color=green!50!black] (0,#3) -- (0,#4) node[left] {#6};
}
\newcommand{\KoordinatenkreuzOhneLabelsVerschobenKeinPfeil}[5]{
\draw[-] (#1,0) -- (#2,0);
\draw[-] (#5,#3) -- (#5,#4);

}
\newcommand{\ZeigerdiagrammText}[4]{
\begin{tikzpicture}[scale=.72, samples=100, >=latex]

\def\Alpha{#1}
\def\Phase{#2}
\def\AmplitudeSpannung{#3}
\def\AmplitudeStrom{#4}
\def\SpannungsWert{{\AmplitudeSpannung*sin(\Alpha)}}
\def\StromWert{{\AmplitudeStrom*sin(\Alpha+\Phase)}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\FarbeSpannung{blue!90!white}
\def\FarbeStrom{red!90!white}
\def\FarbeWinkelZeichnung{green}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\Beta{\Alpha+\Phase}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Gitter{-.1}{7.1}{-3.1}{3.1}
\Koordinatenkreuz{-.2}{7.3}{-3.2}{3.3}{$\omega t$}{}
\draw (1.570795,0) node[below]{$\frac{\pi}{2}$};
\draw (3.14159,0) node[below]{${\pi}$};
\draw (4.71238898,0) node[below]{$\frac{3\pi}{2}$};
\draw (6.283185307,0) node[below]{${2\pi}$};
\draw (-4,0) circle (3cm);
\KoordinatenkreuzOhneLabelsVerschobenKeinPfeil{-7.2}{-.8}{-3.6}{3.6}{-4}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% voltage
\draw[color=\FarbeSpannung, very thick] plot[id=voltage, domain=0:7] function{\AmplitudeSpannung*sin(x)} node[right] {$U(t)$};
% voltage circle
\draw[color=\FarbeSpannung, loosely dashed] (-4,0) circle (\AmplitudeSpannung cm);
% angle
\draw[color=\FarbeWinkelZeichnung!50!black, thick] (\AlphaRad, \SpannungsWert)--(\AlphaRad,\StromWert) node[below=18pt] {$\alpha$};
% angle in the circle
\filldraw[fill=\FarbeWinkelZeichnung!20,draw=\FarbeWinkelZeichnung!50!black] (-4,0) -- (-3,0) arc (0:\Alpha:1) -- cycle node[right] {$\alpha$};
% voltage pointer
\draw[<-,color=\FarbeSpannung, very thick] (\Alpha:\AmplitudeSpannung)++(-4,0) --(-4,0);
% current
\draw[color=\FarbeStrom, very thick] plot[id=current, domain=0:7] function{\AmplitudeStrom*sin(x+\PhaseRad)} node[right] {$I(t)$};
% current circle
\draw[color=\FarbeStrom, loosely dashed]    (-4,0) circle (\AmplitudeStrom cm);
% current pointer
\draw[<-,color=\FarbeStrom, very thick] (\Beta:\AmplitudeStrom)++(-4,0) --(-4,0);
% phase difference
\ifthenelse{\Phase<0}{
\draw[snake=brace] (pi/2 ,3.3)--(pi/2-\PhaseRad ,3.3) node[above=7pt, left=10pt] {$\phi$};
}
{
\draw[snake=brace] (pi/2-\PhaseRad ,3.3)--(pi/2 ,3.3) node[above=7pt, left=10pt] {$\phi$};
}
% angular velocity \omega
\draw[->, xshift=-4cm]  (120:2.4cm) arc (120:170:2) node[below] {$\omega$};
\end{tikzpicture}
}
% % % % % % % % % % % % % % % % % % % % % % % % % % %

\begin{document}

\begin{preview}
% usage: {alpha}{phasedifferenceUI}{voltage}{current}{Ualpha}{Ialpha}
% resistor
\ZeigerdiagrammText{60}{0}{2.7}{1.8}

\end{preview}

\end{document}

-