# TikZ: Draw rectangular triangle by raising a perpendicular on the hypotenuse

I am given the points A, B and X within a tikzpicture environment, like this:

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (3.6,0);
\coordinate (X) at (2,0);
\end{tikzpicture}


Now I would like to construct the coordinate C as in this picture:

Using circle and straightedge, I would raise a perpendicular on AB in X and intersect it with the circle having AB as a diameter. I'm sure there is a clean way to do this in TikZ (without doing any coordinate calculations by hand). However browsing through the (substantial) documentation, I wasn't able to find a direct answer.

For portability, I would like to have a solution in plain TikZ (without the tkz-euclide package).

EDIT

For clarification: By a "clean way" I'm trying to express that I want to tell TikZ "what I mean". So for example: Introducing the midpoint O of the line segment [AB] would be ok, as well as saying that the distance of C and O should be the same as the distance of A and O. But direct computations of the coordinates, for example based on the Geometric mean theorem or trigonometric functions should be avoided.

As a fast check: If there is some function like sqrt or atan etc. in your code, then it's probabily not what I'm looking for. Also, your code should still work if I alter the starting coordinates of A, B and X (they should still be on a common line, though, but this line might not be horizontal any more.)

• Probably easier to use polar coordinates and take X as the origin?
– cfr
Jul 13 '15 at 13:56
• @cfr: I don't think so: Note that X is not the center of the circle. It's just some arbitary point on the line segment AB. Jul 13 '15 at 13:57
• Sorry. Yes. Not X. But if it is within a circle, it seems to make sense to set the coordinates in polar with the center at the origin? But you can otherwise use the intersections library to get the intersection of the arc and a line from X upwards. I'm not sure why you'd do this this way, but you can.
– cfr
Jul 13 '15 at 14:04
• Did you see Section 2.16 Intersecting Paths on tikz the user guide? Jul 13 '15 at 14:06
• Or 4.2.2 Intersecting a Line and a Circle. Jul 13 '15 at 14:10

Here a solution with intersections. The borders of the image are fixed with a clip. I am still searching for a solution without the helper lines, and let you know when i found it.

At the moment you have to use the clip and adjust it manually.

\documentclass[tikz, border=0mm]{standalone}

\usetikzlibrary{calc, intersections, through}

\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (3.6,0);
\coordinate (x) at (2,0);

\begin{scope}
\clip (0,-.01) rectangle (3.6,1.8);
\path [name path=px] (x) -- ++(90:2cm);
\node at ($(a)!.5!(b)$) [name path=pc, circle through={(a)}] {};
\draw [name intersections={of=px and pc}] (a) -- (intersection-1) -- (b) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}


Update:

Here another piece of code using the let command in combination with intersections:

\documentclass[tikz, border=6mm]{standalone}

\usetikzlibrary{calc, intersections}

\begin{document}
\begin{tikzpicture}
\coordinate [label={180:$A$}] (a) at (0,0);
\coordinate [label={0:$B$}] (b) at (3.6,0);
\coordinate [label={-90:$X$}] (x) at (2,0);

\draw [dashed] let \p1 = ($(b)-(a)$) in [name path=pa] (a) arc (180:0:\x1/2);
\draw [dashed] let \p1 = ($(b)-(a)$) in [name path=px] (x) |- ($(x)+(90:\x1/2)$);
\draw [name intersections={of=pa and px}] (a) -- (intersection-1) node [label={90:$C$}] {} -- (b) -- cycle;
\end{tikzpicture}
\end{document}


Update:

let Command

With the let command you can use a point register (\p[n]) or number register (\n[n]) for drawing your path. I used the calculation of a point (\p1) by path-calculations fo existing points and reuse the x and y coordinates (\x1, \y1) in the drawing part of the command (after in).

The let command is also described under 14.15 (p. 161) of the pgfmanual 3.0.

Coordinate (c)

Adjust the last \draw command like this to add a coordinate at the intersection point (intersection-1) called (c). This way you get your coordinate and can access it later in your code by calling it.

\draw [name intersections={of=pa and px}] (a) -- (intersection-1) coordinate (c) node [label={90:$C$}] {} -- (b) -- cycle;

• Thank you! Yes, the manual clipping isn't that great. Would it be possible to restrict the circle to the upper half circle? After that (and with the px path short enough), the automatic clipping should be fine I guess. Jul 13 '15 at 14:47
• I got many answers and I'm figuring which one I should accept. Your update comes quite close to what I was hoping for. However, at the moment the constructed coordinate has the name (intersection-1) and not (C). I have to confess that I don't understand the let ... parts of your code. Would you mind explaining a little bit what's going on there? And could you rename the coordinate into (C)? Thank you! Jul 15 '15 at 8:47

Another alternative with calc frenzy

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label=180:$A$] (A) at (0,0);
\coordinate[label=0:$B$] (B) at (3.6,0);
\coordinate[label=270:$X$] (X) at (0.5,0);
\coordinate (O) at ($(A)!0.5!(B)$);
\draw (A) let \p1=($(B)-(A)$),\p2=($(O)-(X)$),\n1={acos(2*\x2/\x1)} in
arc (180:0:0.5*\x1) ($(O)!1!-\n1:(A)$)
coordinate[label=90:$C$] (C) --(X);
\draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}
\end{document}


• Wow, thanks. I tried it, and it works. However, I have no idea what the line \draw (A) let \p1... does. Would you mind explaining that line a little bit? Jul 13 '15 at 15:25
• @azimut Here is some tex.stackexchange.com/a/247315/3235 Jul 13 '15 at 15:49

Althogh the OP required a solution without using tkz-euclide, I think this might be useful for somebody else:

\documentclass{article}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}

\begin{tikzpicture}
% Initial points A,B, X, O
\tkzDefPoint(0,0){A}
\tkzDefPoint(2,0){X}
\tkzDefPoint(8,0){B}
\tkzDefPoint(4,0){O}
% The arc
\tkzDrawArc[dashed,color=gray!60,thin](O,B)(A)
% Finding C
\tkzDefLine[orthogonal=through X](A,X){O}
\tkzInterLC[R](X,tkzPointResult)(O,4cm)
\tkzGetSecondPoint{C}
% Drawing lines
\tkzDrawSegments(X,C A,B A,C C,B)
\tkzDrawPoints(A,B,X,O,C)
\tkzLabelPoints(A,B,X,O)
\tkzLabelPoints[above](C)
\end{tikzpicture}

\end{document}


• Even though the question specified no tkz-euclide ;).
– cfr
Jul 13 '15 at 19:23
• @cfr. True. I didn't notice it. Initially I thought about deleting the answer, then I decided to keep it adding a remark. Jul 13 '15 at 19:35
• Well, it is a useful answer if somebody else doesn't have the OP's restrictions. So I don't see why you should delete it. People answer tikz questions with pstricks solutions and vice-versa for that kind of reason all the time.
– cfr
Jul 13 '15 at 20:38

Here's another solution with intersections like moospit's answer, but this one uses an arc (half a circle) to compute the intersection, and it only uses the intersections library.

## Code

\documentclass[tikz, margin=10pt]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}

\foreach \x/\l in {0/A, 2/X, 3.6/B}{
\coordinate (\l) at (\x,0);
\node[circle, fill=black, inner sep=0, minimum size=1pt, label=270:\l] at (\x,0) {};
}
\path[name path=xpath] (X) --++ (0,2);
\draw[line width=.3pt, gray, name path=arc] (A) arc (180:0:1.8);

\path[name intersections={of=xpath and arc, by=C}];
\node[circle, fill=black, inner sep=0, minimum size=1pt, label=90:C] at (C) {};

\draw (A) -- (C) -- (B) -- cycle;
\draw[dashed] (X) -- (C);

\end{tikzpicture}
\end{document}


Also... Animation time!

• how you get the animation? Apr 10 '17 at 18:32

EDIT: Warning removed. If both Euclid and egreg say it is true, it must be true.

Here's a solution which requires no additional libraries.

My solution uses the fact that AB is a diameter of the circle and that C is a point on the circumference of that circle. This means that angle CAB is equal to angle BCX since ACB is a right-angle. So tan CAB is CX/AX and tan BCX is (AB-AX)/CX. Hence CX/AX = (AB-AX)/CX. Since AB and AX are known, we can solve for CX = root of AX(AB-AX).

\documentclass[tikz,border=5pt]{standalone}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (X) at (\myl,0);
\coordinate (C) at (\myl,{sqrt(\myl*(2*\myradius - \myl))});
\draw [densely dashed, gray] (A) arc (180:0:\myradius);
\draw (A) node [left] {$A$} -- (B) node [right] {$B$} -- (C) node [above] {$C$} edge node [below, pos=1] {$X$} (X) -- cycle;
\end{tikzpicture}
\end{document}


• By what we call Euclid's second theorem (not sure it's international), CX² = AX · BX. Jul 13 '15 at 16:12
• Thank you for this answer. According to en.wikipedia.org/wiki/Geometric_mean_theorem the name is right triangle altitude theorem or geometric mean theorem. Actually, this is the kind of solution which I wanted to exclude by "without doing any coordinate calculations by hand". Jul 14 '15 at 7:25
• @azimut I don't really understand what would count, in that case, as an acceptable solution. percusse's solution doesn't count as doing calculations by hand but this does? That solution is great, but I don't see that it avoids doing the calculations any more than mine does. Indeed, you said in comments I hope for a solution using coordinate computations only.... So you want to do it with coordinate computations rather than calculations? What is the difference exactly?
– cfr
Jul 14 '15 at 10:18
• @azimut I think the name varies. See egreg's comment above.
– cfr
Jul 14 '15 at 10:20
• @cfr: I've added an attept for an explanation the question. Jul 15 '15 at 8:57

Just for fun, my first test with rulercompass. You don't need any coordinate calculation by hand, only some knowledge of technical drawing.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{rulercompass}

\begin{document}
\begin{frame}[plain]
\hfill\begin{tikzpicture}[stop jumping, max size={\textwidth}{\textheight}, ruler compass/ruler length=5cm]
% Three original points
\path (0,0) node[name=A, ruler compass/point=red, label={A}];
\path (3.6,0) node[name=B, ruler compass/point=red, label={B}];
\path (2,0) node[name=X, ruler compass/point=red, label={X}];
\pause
% Line through A and B
\ruler<+->{A}{B}
% We need center of A-B segment, so we draw two circles
\compass<+->{A}{B}
\compass<+->{B}{A}
% and the line through circles intersections give us A-B midpoint
\point<.->{cAB}{cBA}{1} % point a
\point<.->{cAB}{cBA}{2} % point b
\ruler<+->{a}{b}
\point<.->{rAB}{rab}{1} % point c
% We can draw a circle through A and B
\compass<+->{c}{A}
% Now we need a perpendicular through X
\compass<+->{X}{c}
\point<.->{cXc}{rAB}{1} % point d
\compass<+->{c}{d}
\compass<+->{d}{c}
\point<.->{ccd}{cdc}{1} % point e
\point<.->{ccd}{cdc}{2} % point f
% We got it!
\ruler<+->{e}{f}
% Intersection between perpendicular thorugh X and circle thorugh A-B
\point<.->{ref}{ccA}{1} % point g
% We can draw the final triangle
\draw<+->[thick,red] (A)--(B)--(g)--cycle;

\end{tikzpicture}
\end{frame}
\end{document}


tan CBX = tan ACX then CX^2 = AX*BX : graphic illustration of the geometric mean.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{angles}

\begin{document}

\begin{tikzpicture}

\coordinate (A) at (-2,0);
\coordinate (B) at (1.6,0);
\coordinate (X) at (0,0);

\pgfmathsetmacro{\Yc}{sqrt(2*1.6)}

\coordinate (C) at (0,\Yc);

\draw (A)--(B)--(C)--cycle (X)--(C);

\foreach \Coor/\Text/\Pos in
{A/$A$/below,
B/$B$/below,
X/$X$/below,
C/$C$/above%
}
\node[label={[label distance=-2pt]\Pos:\Text}] at (\Coor) {} ;

\node[label=below:$a\vphantom{b}$] at ($(A)!.5!(X)$) {} ;
\node[label=below:$b$] at ($(B)!.5!(X)$) {} ;
\node[label={[label distance=-2pt]left:$\sqrt{ab}$}] at ($(C)!.5!(X)$) {} ;

\draw

\draw[dashed] let   \p1=(C),
\n1={veclen(\x1,\y1)} in
(B) arc (0:180:\n1) ;

\end{tikzpicture}
\end{document}

• Isn't this what I said? Though it is a nice diagram to illustrate the method.
– cfr
Jul 13 '15 at 19:24
• Yes it is. Sorry for making a double. Jul 13 '15 at 19:43

An example showing how it can be done with MetaPost, for whom it may interest. It uses the intersectionpoint instruction, which searches and gives the first intersection point between two given paths (when it exists, of course) as its names points out. So the core instruction here are:

perp = X -- (X+(B-A)) rotatedaround(X, 90);


which creates a perpendicular to (AB) starting from X, of sufficient length (it must intersect the half circle), and

C = perp intersectionpoint halfcirc;


which is pretty self-explanatory.

Included in a LuaLaTeX program for typesetting convenience (many LaTeX users don't know how to run MetaPost).

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
pair A, B, C, X; path halfcirc, perp;
A = origin; B = (3.6cm, 0); X = (2cm, 0);
halfcirc = halfcircle zscaled (B-A) shifted (.5[A,B]);
perp = X -- (X+(B-A)) rotatedaround(X, 90);
C = perp intersectionpoint halfcirc;
beginfig(0);
draw halfcirc dashed evenly;
draw X--C; draw A--B--C--cycle;
forsuffixes M = A, B, X: label.bot("$" & str M & "$", M); endfor;
label.top("$C$", C);
endfig;
\end{mplibcode}
\end{document}