# [Tex/LaTex] Draw a demonstration of Pythagorean theorem with TikZ

tikz-pgf

I have to draw a visual representation of Pythagorean theorem. I had the initial approach of drawing the triangle in the middle and then work my way up by handling the squares. But it seems to much brute-force work. So is there any easy way to do this which could mean avoiding the brute-force approach ? (maybe with some library). Some sort of explanation or useful hint would suffice.

This is really just for fun and no competitor to Alain Matthes nice macros and routines. However, it is not too difficult to let TikZ draw squares above/below a given line such that this line becomes one edge of the square.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,positioning}
\begin{document}
\tikzset{square/.style={minimum size=#1,draw},
measureme/.style={execute at begin to={
\path let \p1=($(\tikztostart) - (\tikztotarget)$),\n1={veclen(\x1,\y1)}
in \pgfextra{\xdef#1{\n1}};}}}
\begin{tikzpicture}
\draw[measureme=\mylen](0,0)
to node[midway,sloped,above,square=\mylen,fill=blue!20]{\xdef\mylenC{\mylen}} node[midway,left=3pt]{$c$} (2,4)
to node[midway,sloped,above,square=\mylen,fill=red!20]{\xdef\mylenB{\mylen}} node[midway,right=3pt]{$b$} (2,0)
to node[midway,sloped,below,square=\mylen,fill=purple!20]{\xdef\mylenA{\mylen}} node[midway,below=3pt]{$a$} (0,0);
\begin{scope}[yshift=-5cm]
\node[square=\mylenB,fill=red!20](B) {$b^2$};
\node[left=2pt of B] (plus) {$+$};
\node[left=2pt of plus,square=\mylenA,fill=purple!20](A) {$a^2$};
\node[right=2pt of B] (eq) {$=$};
\node[right=2pt of eq,square=\mylenC,fill=blue!20](C) {$c^2$};
\end{scope}
\end{tikzpicture}
\end{document}


EXPLANATION: The measureme style makes TikZ measure the length of the edges (and store the result in a macro, which is called \mylen in the example). The square style produces a, well, square, which is placed in the middle of a given edge such that it coincides with the edge.