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.

Visual representation of Pythagorean theorem


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.

\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}};}}}
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);
 \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$};

enter image description here

EDIT: Made the node labels upright and added the Pythagoras relation.

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.

  • Can you add some comment to the code ? How can I make the text a and b straight ? ( they are rotated in the picture ) What are the lengths of the triangle sides ? – Robur_131 Jun 18 '18 at 10:41
  • @Robur_131 I made the labels upright (and close to the edges as in your screenshot), added the actual Pythagoras relation and added a short explanation. – user121799 Jun 18 '18 at 14:32
    \rput{!4 3 atan}(0,0){\psframe[fillcolor=blue](5,5)}

enter image description here

\input tikz.tex
\input pgfmath.tex

{\vskip 5mm plus 2mm minus 2mm
\leftskip=0mm plus 1fil
\rightskip=0mm plus 1fil



    \coordinate (A) at (\xa,\ya);
    \coordinate (B) at (\xb,\yb);
    \coordinate (C) at (\xc,\yc);







        (A) -- (B) -- ++ (\angleab-90:\lenab) -- ++ (\angleab-180:\lenab) -- cycle;

        (B) -- (C) -- ++ (\anglebc-90:\lenbc) -- ++ (\anglebc-180:\lenbc) -- cycle;

        (C) -- (A) -- ++ (\angleca-90:\lenca) -- ++ (\angleca-180:\lenca) -- cycle;

\vskip 2mm plus 1mm minus 1mm
\line{\hfil{Figure. My Lovely Picture}\hfil}


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.