3

I'm trying to draw a right triangle with sqares over its sides to illustrate Pythagoras' theorem. Here's my code

    \begin{tikzpicture}[scale=4]
\coordinate[label=200:$A$]  (A) at (0,0) ;
\coordinate[label=120:$B$]  (B) at (0,1)  ;
\coordinate[label=right:$C$] (C) at (1,0) ;
\draw (A) -- (B) -- (C) -- (A) ;
\draw (A) rectangle (0.08,0.08);
\draw[rotate=90] (A)    let
                        \p1=(B),
                        \p2=(A),
                        \n1={veclen(\x2-\x1,\y2-\y1)}
                    in
                        rectangle (\n1,\n1);
\draw[rotate=270] (A)   let
                        \p1=(C),
                        \p2=(A),
                        \n3={veclen(\x2-\x1,\y2-\y1)}
                    in
                        rectangle (\n3,\n3);
\draw[rotate=45] (C) let 
                        \p2=(B),
                        \p1=(C),
                        \n2={veclen(\x2-\x1,\y2-\y1)}
                    in
                        rectangle (\n2,\n2);
\end{tikzpicture}

As far as I can see I'm doing nothing wrong. But the picture I get is the following:

The hypothenuse rectangle is all wrong

Now there are more elegant solutions to this problem, for instance here; but I want to know why my code doesn't do what it is expected.

Thanks in advance!

1 Answer 1

2

Tray

    \begin{tikzpicture}[scale=4]
\coordinate[label=200:$A$]  (A) at (0,0) ;
\coordinate[label=120:$B$]  (B) at (0,1)  ;
\coordinate[label=right:$C$] (C) at (1,0) ;
\draw (A) -- (B) -- (C) -- cycle ;
\draw (A) rectangle (0.08,0.08);
\draw[red] (A)    let% <---
                        \p1=(B),
                        \p2=(A),
                        \n1={veclen(\x2-\x1,\y2-\y1)}
                    in
                        rectangle +(-\n1,\n1);% <---
\draw[blue] (A)   let% <---
                        \p1=(A),
                        \p2=(B),
                        \n2={veclen(\x2-\x1,\y2-\y1)}
                    in

                        rectangle +(\n2,-\n2);% <---
\draw[rotate=45] (C) let
                        \p2=(B),
                        \p1=(C),
                        \n3={veclen(\x2-\x1,\y2-\y1)}
                    in
                        rectangle + (\n3,\n3);% <---
\end{tikzpicture}

Differences to your MWE are designated by `% <---%. This code gives:

enter image description here

Note: angle for rotate third rectangle is `45 degrees˙only if the first and second are of the same size ...

Addadendum: Your triangle have equal cathetus, so the \n2 is not necessary to calculate. Shorter code is:

\draw   let \p1=(A),
            \p2=(B),
            \n1={veclen(\x2-\x1,\y2-\y1)} in
            (A) rectangle +(-\n1,\n1) 
            (A) rectangle +(\n1,-\n1);
5
  • Magic! Thank you... could you please refer me to the explanation? What does the '+' do? Feb 29, 2016 at 1:37
  • + means relative coordinate. This in case of red rectangle it start at coordinate (A) and end in coordinate ((A) + (-\n1,\n1)).
    – Zarko
    Feb 29, 2016 at 1:42
  • I added possible simplification in case of equal triangle cathetus.
    – Zarko
    Feb 29, 2016 at 1:52
  • Thanks a lot! Do you know how to get the coordinates for the edges of the square over the hypothenuse? I need to draw a line that ends there. Should I do that a separate question? Feb 29, 2016 at 2:04
  • 1
    Two coordinates are know: B and C,. Other two edges can be calculated by ($(B)!\n3!90:(C)$) for coordinate perpendicular to coordinate (C) and ($(C)!\n3!90:(B)$) (not tested, but should work). For details see 13.5.3 The Syntax of Partway Modifiers in TikZ manual.
    – Zarko
    Feb 29, 2016 at 2:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .