Pythagorean theorem

The Pythagorean theorem is a special case of the law of cosines.

Given a right triangle with legs a,b and hypotenuse c,

a2+b2=c2

Visually,

\documentclass[tikz,margin={3cm 0cm}]{standalone}
%
\definecolor{colorminor}{HTML}{FB464C}
\definecolor{colormajor}{HTML}{45CF6E}
\definecolor{colorhypotenuse}{HTML}{53DFDD}
%
\begin{document}
    \def\x{3}
    \def\y{4}
    % https://en.wikipedia.org/wiki/Pythagorean_triple
    \pgfmathsetmacro\h{int(sqrt(\x^2+\y^2))}
    \pgfmathsetmacro\tilt{int(atan2(\y,\x))}
    %
    \begin{tikzpicture}[scale=1/2]
        \draw[very thick, rounded corners = 0.1pt] (0,0) -- (\x,0) -- (\x,\y) -- cycle;
        \draw[thick] (\x,0) rectangle ++(-\h/20,\h/20);
        %
        \fill[colorminor,opacity=0.8] (0,-\x+1) rectangle ++(\x,-1);
        \fill[colormajor,opacity=0.8] (\x+\y-1,0) rectangle ++(1,\y);
        \fill[colorhypotenuse,opacity=0.8] (-\y,\x) -- ++(\x,\y) -- ++(\y/\h,-\x/\h) -- ++(-\x,-\y) -- cycle;
        %
        \foreach \i in {0,...,\x}{\draw (\i,0) -- (\i,-\x);}
        \foreach \j in {1,...,\x}{\draw (0,-\j) -- (\x,-\j);}
        %
        \foreach \i in {0,...,\y}{\draw (\x,\i) -- (\x+\y,\i);}
        \foreach \j in {1,...,\y}{\draw (\x+\j,0) -- (\x+\j,\y);}
        %
        \foreach \i in {0,...,\h}{\draw (\x/\h*\i,\y/\h*\i) -- (-\y+\x/\h*\i,\x+\y/\h*\i);}
        \foreach \j in {1,...,\h}{\draw (-\y/\h*\j,\x/\h*\j) -- (\x-\y/\h*\j,\y+\x/\h*\j);}
        %
        \path (0,-\x) -- (\x,-\x) node[midway,below] {\Large$a$};
        \path (\x+\y,0) -- (\x+\y,\y) node[midway,right,rotate=90,anchor=north] {\Large$b$};
        \path (-\y,\x) -- ++(\x,\y) node[midway,above left,rotate=\tilt,anchor=south] {\Large$c$};
\end{tikzpicture}
\end{document}

Powered by Forestry.md