[Tex/LaTex] Drawing a line segment of a specific length that is perpendicular to a given line

tikz-pgf

I have a typical geometric construction that I want to draw using TikZ. The following code gives a line segment PQ. How do I get TikZ to draw a point on the line perpendicular to PQ and through P that is 2\sqrt{2} units below the line segment PQ? I think that I have to have \usetikzlibrary{calc} in the preamble to do this. (I want to label this point R and draw right triangle QPR.) If I want to use \tkzMarkRightAngle(Q,P,R); to indicate that the triangle is a right triangle, do I have to have \usepackage{tkz-euclide} in the preamble?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,intersections,quotes}


\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-1.75) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

\end{tikzpicture}
\end{document}

Best Answer

Check the code for explanatory comments:

\documentclass{amsart}
\usepackage{tikz}

%% you need the following 2 lines to use \tkzMarkRightAngle
%\usepackage{tkz-euclide}
%\usetkzobj{all}

\usetikzlibrary{shapes,positioning,intersections,quotes,calc}


\begin{document}

\begin{tikzpicture}

\draw[yellow, line width=0.1pt] (-1.75,-5) grid[xstep=0.5, ystep=0.5]  (2.75,1.75);
\draw[draw=gray!30,latex-latex] (0,1.75) +(0,0.25cm) node[above right] {$y$} -- (0,-1.75) -- +(0,-0.25cm);
\draw[draw=gray!30,latex-latex] (-1.75,0) +(-0.25cm,0) -- (2.75,0) -- +(0.25cm,0) node[below right] {$x$};

\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$P$}] (P) at (-1,-1) {};
\node[outer sep=0pt,circle, fill,inner sep=1.5pt, label={[fill=white]right:$Q$}] (Q) at (2,1) {};

\draw[green!20!white] (P) -- (Q);

%% the perpendicular
\pgfmathparse{2*sqrt(5)}
\node[outer sep=0pt,circle, fill,inner sep=1.5pt,label={[fill=white]left:$R$}] (R) at ($ (P)!\pgfmathresult cm! -90:(Q) $) {};
\draw[green!20!white] (P) -- (R) -- (Q);


%% right angle with tkz-euclide
%\coordinate (p) at (P);
%\tkzMarkRightAngle[color=green!20!white](Q,p,R)
%\fill (p) circle (2.1pt);  %% to make the dot above right angle again.

\coordinate (a) at ($ (P)!5mm! -45:(Q) $);
\draw[green!20!white] (a) -- ($(P)!(a)!(Q)$);
\draw[green!20!white] (a) -- ($(P)!(a)!(R)$);

\end{tikzpicture}
\end{document}

enter image description here