[Tex/LaTex] Draw with TikZ a Pythagorean triangle with the squares of its sides and labels

tikz-pgf

I've made this checking the old questions and the TikZ manual and I want to draw the squares of the sides of my Pythagorean triangle.

So far I have

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1.25]%,cap=round,>=latex]

\coordinate [label=left:$C$] (A) at (-1.5cm,-1.cm);
\coordinate [label=right:$A$] (C) at (1.5cm,-1.0cm);
\coordinate [label=above:$B$] (B) at (1.5cm,1.0cm);
\draw (A) -- node[above] {$a$} (B) -- node[right] {$c$} (C) -- node[below] {$b$} (A);

\draw (1.25cm,-1.0cm) rectangle (1.5cm,-0.75cm);

\end{tikzpicture}

\end{document}

which produces

output

Best Answer

First things first: let's make the width and height of the triangle into constants, so that we can change them later if we need to. These are the values that you used, but by loading them once and computing everything else on the fly, it makes it easier to change things around later:

\newcommand{\pythagwidth}{3cm}
\newcommand{\pythagheight}{2cm}

Next, relabel your coordinates so that the name matches the label which gets printed, otherwise we'll get horribly confused.

\coordinate [label={below right:$A$}] (A) at (0, 0);
\coordinate [label={above right:$B$}] (B) at (0, \pythagheight);
\coordinate [label={below left:$C$}] (C) at (-\pythagwidth, 0);

Two of the rectangles (the ones matching the horizontal and vertical edges) are easy to draw, if a little verbose:

\draw [dashed] (A) -- node [below] {$b$} ++ (-\pythagwidth, 0)
                -- node [right] {$b$} ++ (0, -\pythagwidth)
                -- node [above] {$b$} ++ (\pythagwidth, 0)
                -- node [left] {$b$} ++ (0, \pythagwidth);

\draw [dashed] (A) -- node [right] {$c$} ++ (0, \pythagheight)
                -- node [below] {$c$} ++ (\pythagheight, 0)
                -- node [left] {$c$} ++ (0, -\pythagheight)
                -- node [above] {$c$} ++ (-\pythagheight, 0);

These changes get us most of the way:

enter image description here

and then we need to draw the square corresponding to the hypotenuse. Computing the hypotenuse itself seems excessive (read: I’m tired and can’t remember how to do it now :P). Instead, we can use a little plane geometry:

enter image description here

We can find another edge of the square by rotating the original triangle through 90 degrees, and then translating appropriately. We can use the same method to find the two extra coordinates of the hypotenuse square in TikZ:

\coordinate (D1) at (-\pythagheight, \pythagheight + \pythagwidth);
\coordinate (D2) at (-\pythagheight - \pythagwidth, \pythagwidth);

and then drawing this square is simple:

\draw [dashed] (C) -- node [above left] {$a$} (B)
                   -- node [below left] {$a$} (D1)
                   -- node [below right] {$a$} (D2)
                   -- node [above right] {$a$} (C);

So putting this all together, we have:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\newcommand{\pythagwidth}{3cm}
\newcommand{\pythagheight}{2cm}

\begin{tikzpicture}

  \coordinate [label={below right:$A$}] (A) at (0, 0);
  \coordinate [label={above right:$B$}] (B) at (0, \pythagheight);
  \coordinate [label={below left:$C$}] (C) at (-\pythagwidth, 0);

  \coordinate (D1) at (-\pythagheight, \pythagheight + \pythagwidth);
  \coordinate (D2) at (-\pythagheight - \pythagwidth, \pythagwidth);

  \draw [very thick] (A) -- (C) -- (B) -- (A);

  \newcommand{\ranglesize}{0.3cm}
  \draw (A) -- ++ (0, \ranglesize) -- ++ (-\ranglesize, 0) -- ++ (0, -\ranglesize);

  \draw [dashed] (A) -- node [below] {$b$} ++ (-\pythagwidth, 0)
            -- node [right] {$b$} ++ (0, -\pythagwidth)
            -- node [above] {$b$} ++ (\pythagwidth, 0)
            -- node [left] {$b$} ++ (0, \pythagwidth);

  \draw [dashed] (A) -- node [right] {$c$} ++ (0, \pythagheight)
            -- node [below] {$c$} ++ (\pythagheight, 0)
            -- node [left] {$c$} ++ (0, -\pythagheight)
            -- node [above] {$c$} ++ (-\pythagheight, 0);

  \draw [dashed] (C) -- node [above left] {$a$} (B)
                     -- node [below left] {$a$} (D1)
                     -- node [below right] {$a$} (D2)
                     -- node [above right] {$a$} (C);

\end{tikzpicture}

\end{document}

which produces

enter image description here