# Drawing some simple orthogonal projections

I'm a beginner with TikZ and I'd like to show that a vector y can be decomposed as How can I tell TikZ to draw a dashed line orthogonal to yp onto X and a vector ε orthogonal to X, starting at the origin? This is the code, but the projections are missing.

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning,arrows.meta}
\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]

\draw[thick,->] (-4.5,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);

\coordinate (A) at (0,0);
\coordinate (B) at (-4,3);

\draw [extended line=0.5cm, <->] (A) -- (B)
node[pos=0.8,left=1em, font=\small]{$X$};
\draw [ ->] (0,0) -- (-2.6, 4.3) node[anchor=north east,font=\small] {$y$};

\end{tikzpicture}
\end{document}

• This related post could be useful. – Marian G. Apr 29 at 17:48
• \draw [ ->] (0,0) -- (-2.6, 4.3) coordinate (yn) node[anchor=north east,font=\small] (y) {$y$}; \draw[dashed] (yn) -- ($(A)!(yn)!(B)$); – user121799 Apr 29 at 18:02

## 2 Answers

One can project a coordinate, say, (yn) on the line between, say, (A) and (B) by using ($(A)!(yn)!(B)$), as explained in the post linked by Marian G.. To get the picture, you may thus want to simply (meaning no intersections, explicit angles and so on) do

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\draw[thick,-stealth] (-4.5,0) -- (4.5,0);
\draw[thick,-stealth] (0,0) -- (0,4.5);
\coordinate (A) at (0,0);
\coordinate (B) at (-4,3);
\draw [extended line=0.5cm, stealth-stealth] (A) -- (B) node[pos=1.15,font=\small]{$X$};
\draw [ -stealth] (0,0) -- (-2.6, 4.3) coordinate (yn) node[right]{$y$};
\draw[dashed] (yn) --  node[midway,above left]{$\varepsilon$} ($(A)!(yn)!(B)$) node[below left]{$y_p$};
\end{tikzpicture}
\end{document} as starting point:

\documentclass[12pt,tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
calc,
intersections,
quotes}

\begin{document}
\begin{tikzpicture}[
lbl/.style = {inner sep=2pt, font=\footnotesize, sloped},
every path/.style = {-Straight Barb},
extended line/.style = {shorten >=-#1,shorten <=-#1},
extended line/.default = 1cm
]
\draw[semithick] (-4.5,0) -- (4.5,0);
\draw[semithick] (0,-0.5) -- (0,4.5);
%
\coordinate (A) at (0,0);
\coordinate (B) at (-3.2,1.6);
\coordinate (C) at (-2.6,4.3);
%
\path[name path=X] (A) -- (C);                  % <---
\path[name path=P] (B) -- ($(B)!1!90:(A)$);     % <---
\draw[name intersections={of=P and X, by={p}},  % <---
dashed,->]                                % <---
(B) -- node[lbl,above] {$y_q$} (p);         % <---
\draw[thick] (A) -- node[lbl,above] {$y$}
(p)  node[above] {$Y$};
\draw[dashed] (A) -- node[lbl,below] {$y_d$} (B);
\end{tikzpicture}
\end{document} • Thank you for your answer, even though I wanted to avoid the use of paths. – Nenne Apr 29 at 19:26
• why? they are essential for proposed solution. however, you still can add your original lines or use them instead of paths. but i must confess, that i don't understand your question well (what you like to obtain). it would be helpful, if you would add image of your code to question and on it add sketch what you looking for. – Zarko Apr 29 at 19:35