# Parallelogram Rule and Head to Tail Rule

Here's my current MWE:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{calc}

\tikzset{>=stealth}
\colorlet{veccol}{green!45!black}
\tikzstyle{vector}=[->,very thick,veccol]

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw[<->] (0,0)--(5.5,0) node[right]{$x$};
\draw[<->] (0,0)--(0,5.5) node[above]{$y$};

\coordinate (O) at (0,0);
\coordinate (A) at (125:5);
\coordinate (B) at (0:4);
\coordinate (A+B) at ($(A)+(B)$);

\draw[vector,blue] (0,0)--(A) node[midway,left] {$\vec{x}$};
\draw[vector,red] (0,0)--(B) node[midway,above] {$\vec{y}$};

\draw[vector,thin,dashed] (A)--(A+B);
\draw[vector,thin,dashed] (B)--(A+B);

\draw[vector,purple] (O)--(A+B) node[above,rotate=-60] {$\vec{x}+\vec{y}$};
\draw[rotate=180] (O)--(A+B);
\end{tikzpicture}
\end{figure}
\end{document}


which produces this pdf:

Here's the issue. I'm always writing this code out. I have it in 10+ places. Is there a way that I can create something like a function, feed it the two (or more) vectors using polar coordinates, and graphs it like this?

Also, how would I draw using the same exact vectors using the head-to-tail rule instead of the parallelogram rule? Would it also be possible to create some sort of reusable function like the question from above, but use it for the head-to-tail rule instead of the parallelogram rule?

For the parallelogram part:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{calc}

\newcommand{\parallelogramRule}[2]{%
\begin{tikzpicture}[inner sep=1pt,>=stealth]
\draw[<->] (0,0)--(5.5,0) node[right]{$x$};
\draw[<->] (0,0)--(0,5.5) node[above]{$y$};

\tikzstyle{vector label} = [midway,fill=white,sloped]
\tikzstyle{vector}=[->,very thick]
\tikzstyle{construction} = [->,thin,dashed,draw=green!45!black]

\coordinate (O) at (0,0);
\coordinate (A) at (#1);
\coordinate (B) at (#2);
\coordinate (A+B) at ($(A)+(B)$);

\draw[vector,blue] (O)--(A) node[vector label] {$\vec{x}$};
\draw[vector,red] (O)--(B) node[vector label] {$\vec{y}$};

\draw[construction] (A)--(A+B);
\draw[construction] (B)--(A+B);

\draw[vector,purple] (O)--(A+B) node[vector label] {$\vec{x}+\vec{y}$};
\end{tikzpicture}
}

\begin{document}
\begin{figure}
\centering
\parallelogramRule{30:5}{0:4}%
\parallelogramRule{130:2}{90:2}%
\parallelogramRule{130:4}{-10:2}
\end{figure}
\end{document}


Implementing the head-to-tail rule is simple, just replace the addition by subtraction and change the label of the resultant vector.

Edit

\documentclass{article}

\usepackage{listofitems}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{tikzpicture}[inner sep=0pt]
\tikzstyle{vector}=[->,very thick,blue]
\tikzstyle{vector label} = [midway,fill=white,sloped]
\tikzstyle{resultant}=[->,very thick,red]

\coordinate (O) at (0,0);
\draw [->] (O) -- (4,0) node [right] {$x$};
\draw [->] (O) -- (0,4) node [right] {$y$};

\setsepchar{;}
\coordinate (resultant) at (O);
\foreach \i in {1,2,...,\veclistlen}{%
\coordinate (vec\i) at (\veclist[\i]);
\draw [vector] (O) -- (vec\i) node [vector label] {$\vec{v}_\i$};
\coordinate (resultant) at ($(resultant)+(vec\i)$);
}
\draw [resultant] (O) -- (resultant) node [vector label] {resultant};
\end{tikzpicture}
}

\begin{document}
\begin{figure}
\centering
\end{figure}
\end{document}


Edit 2

Following suggestions of Qrrbrbirlbel and SebGlav, using tikzset and \pgffor directly.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{tikzpicture}[inner sep=0pt]
\tikzset{%
vector/.style={->,very thick,blue},
vector label/.style={midway,fill=white,sloped},
resultant/.style={->,very thick,red}
}

\coordinate (O) at (0,0);
\draw [->] (O) -- (4,0) node [right] {$x$};
\draw [->] (O) -- (0,4) node [above] {$y$};

\coordinate (resultant) at (O);
\foreach \vecPos [count=\vecId] in {#1}{%
\draw [vector] (O) -- \vecPos node [vector label] {$\vec{v}_\vecId$};
\draw (resultant) +\vecPos coordinate (resultant);
}
\draw [resultant] (O) -- (resultant) node [vector label] {resultant};
\end{tikzpicture}
}

\begin{document}
\begin{figure}
\centering

• If you use (1, 1), (-2, 3), … (parentheses around the coordinates), you can use it directly as a list for \foreach. See Special handling of pairs. Commented Nov 9, 2022 at 12:47
• Use of tikzstyle is deprecated. You should use tikzset instead. Commented Nov 9, 2022 at 21:32