# (Right) angle relative to line in TikZ

Is there an equivalent of the tkz-euclide command \tkzDefPointWith[orthogonal normed](Rx,Px) \tkzGetPoint{PRx} in TikZ?

• Perhaps you should post some MWE demonstrating what the commands you mention actually do, for people unfamiliar with tkz-euclide. – jubobs Feb 10 '14 at 0:32
• Do you want to draw a perpendicular line from a point on to a segment? – user11232 Feb 10 '14 at 0:35
• @Jubobs: That's the only command I do. Given the coordinates Rx and Px, it finds a point perpendicular to that segment: PRx. – Neil G Feb 10 '14 at 0:39
• @HarishKumar, The reverse, I think: I want to find a coordinate perpendicular to line. – Neil G Feb 10 '14 at 0:40

Given two coordinates named (A) and (B) you can use

($(A) ! {sin(90)} ! 90:(B)$)


to draw a line perpendicular through (A) to the segment joining (A) and (B):

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

\begin{document}

\begin{tikzpicture}
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (4,3);
\draw (A) -- (B);
\node [fill=red,inner sep=1pt,label=above:$D$] (D) at
($(A) ! {sin(90)} ! 90:(B)$) {};
\draw[red] (A) -- (D);
\end{tikzpicture}

\end{document}


A little generalization: now the perpendicular can be drawn through any point of the segment joining the two given coordinates and can have any desired length. A command was defined:

\Perp[<number>]{<coord1>}{<coord2>}{<name>}[<length>]


<coord1> and <coord1> are the two given points and <name> will be used to internally label the end point of the perpendicular. <number> can be any real number; if 0<=<number><=1 the perpendicular will lie inside the segment (0,5, the default value, gives the perpendicular through the middle point). <length> allows control over the length of the perpendicular. For example

\Perp{(L)}{(M)}{T}


draws a perpendicular through the middle point of the segment defined by (L) and (M); the end point of this perpendicular will be labelled T.

The code, including some more examples of use:

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}

\NewDocumentCommand\Perp{O{0.5}mmmO{1cm}}{%
\coordinate (#4) at
($($ #2!#1!#3 $) ! {sin(90)} ! 90:#3$) {};
\draw ($#2!#1!#3$) -- ($($ #2!#1!#3 $) ! #5 ! (#4)$);
\coordinate (#4) at ($($ #2!#1!#3 $) ! #5 ! (#4)$);
}

\begin{document}

\begin{tikzpicture}
\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (4,3);
\draw (A) -- (B);
\Perp{(A)}{(B)}{K}

\coordinate [label=left:$C$] (C) at (5,0);
\coordinate [label=right:$D$] (D) at (7,-1);
\draw (C) -- (D);
\Perp[0.3]{(C)}{(D)}{F}[2cm]
\Perp[0.6]{(C)}{(D)}{G}[4cm]
\Perp[0.9]{(C)}{(D)}{H}[6cm]

\foreach \Nom in {K,F,G,H}
\node[circle,fill=red,inner sep=1.5pt,label={above:$\Nom$}] at (\Nom) {};
\end{tikzpicture}

\end{document}


• This is a great solution. It might be better to have perp define the coordinate and let the caller draw the edge. – Neil G Feb 10 '14 at 3:42
• @NeilG I updated my answer introducing your modification. – Gonzalo Medina Feb 10 '14 at 3:48
• Since sin(90)=1, why not just write 1? – JDH Dec 4 '16 at 1:39

Easiest with longest code. I have drawn to make things visible.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (p) at (0,0);
\coordinate (q) at (3,4);
\draw (p) -- (q) node[draw,pos=.2,minimum height=1cm,minimum width=1cm,anchor=center,sloped] (pqn){};
\draw[fill] (pqn.north) circle (1pt);
\draw (pqn.center) -- (pqn.north);
\end{tikzpicture}
\end{document}


(pqn.north) and (pqn.south) are the perpendicular points at pos=0.2.

A third alternative.

Use the orthogonal identifiers |- and -| for the intersections of different coordinates. To find the intersection point C, one lets A go horizontally and B vertically. The the expression ($(A)!(C)!(B)$) yields the projection of point (C) on the line connecting (A) and (B).

Code

\documentclass[border=20pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label=right:{#1}}]
\node [dot=A]  (A) at (0,0){};
\node [dot=B]  (B) at (4,3){};
\node [dot=C]  (C) at (A-|B){};
\draw (A)--(B);
\draw ($(A)!(C)!(B)$) -- (C);
\end{tikzpicture}

\end{document}