# TikZ: two figures corresponding to each other

Now I have not yet started making this picture, and I would like some pointers. Any method to produce the image is sufficient, alas I would prefer TikZ and perhaps tkz-euclide over asympote or similar

A rope is 10 meters long. We cut the rope in half, where the first part is made into a equilateral triangle and the second part is made into a square.

The part that is used for the triangle is x meters long.

What my problem is, is that I do not know how to change the size of the square accordingly to the triangle.

For example if I say that 2 meters goes to the triangle. How would I make TikZ know that 8 would go to the square?

-
You mean '8 would go to the square'? – Count Zero Nov 27 '11 at 14:53
yes =) sorry for the confusion – N3buchadnezzar Nov 27 '11 at 14:55
No problem, I edited it for you. Hope it's ok. :) – Count Zero Nov 27 '11 at 14:58

\documentclass{scrartcl}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\def\totallength{10}

\foreach \len in {2,5,8}
{%
\begin{tikzpicture}
\tkzDefPoint(0,0){A};
\tkzDefPoint(\len/3,0){B};
\tkzDefShiftPoint[B](0.5,0){C};
\tkzDefShiftPoint[C](\totallength/4-\len/4,0){D};
\tkzDrawTriangle[equilateral](A,B)
\tkzDrawSquare(C,D)
\end{tikzpicture}\par}

\end{document}


-
Really nice solution. You seem so good using tkz-euclide ^^ Are there any way to "force" these three results to be in one line? I am able to do it, splitting them up into minipages, but that seems like an ugly hack. – N3buchadnezzar Nov 27 '11 at 23:38
@N3buchadnezzar: I believe that Altermundus wrote tkz-euclide! – qubyte Nov 28 '11 at 0:11
I know! He is one awesome guy! – N3buchadnezzar Nov 28 '11 at 0:22
@N3buchadnezzar, it should be enough to remove the \par. – quinmars Nov 28 '11 at 0:35
@N3buchadnezzar I wrote tkz-euclide for mathematics teachers who do not have enough time to study Tikz and for me to avoid repetitive works but in other cases and in contexts, I recommend learning and working with Tikz. You can remove the \parto get results on the same line. – Alain Matthes Nov 28 '11 at 6:36

\pgfmathsetmacro is your friend:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\pgfmathsetmacro{\totallength}{10}

\begin{tikzpicture}
% --- only edit the next ---
\pgfmathsetmacro{\trianglelength}{2}
% --- --- --- --- --- --- --- ---
\pgfmathsetmacro{\triangleside}{\trianglelength/3}
\pgfmathsetmacro{\squarelength}{\totallength-\trianglelength}
\pgfmathsetmacro{\squareside}{\squarelength/4}
\draw (-0.1,0) -- ++(180:\triangleside) -- ++(60:\triangleside) -- cycle;
\draw (0.1,0) rectangle ++(\squareside,\squareside);
\end{tikzpicture}

\begin{tikzpicture}
% --- only edit the next ---
\pgfmathsetmacro{\trianglelength}{5}
% --- --- --- --- --- --- --- ---
\pgfmathsetmacro{\triangleside}{\trianglelength/3}
\pgfmathsetmacro{\squarelength}{\totallength-\trianglelength}
\pgfmathsetmacro{\squareside}{\squarelength/4}
\draw (-0.1,0) -- ++(180:\triangleside) -- ++(60:\triangleside) -- cycle;
\draw (0.1,0) rectangle ++(\squareside,\squareside);
\end{tikzpicture}

\begin{tikzpicture}
% --- only edit the next ---
\pgfmathsetmacro{\trianglelength}{8}
% --- --- --- --- --- --- --- ---
\pgfmathsetmacro{\triangleside}{\trianglelength/3}
\pgfmathsetmacro{\squarelength}{\totallength-\trianglelength}
\pgfmathsetmacro{\squareside}{\squarelength/4}
\draw (-0.1,0) -- ++(180:\triangleside) -- ++(60:\triangleside) -- cycle;
\draw (0.1,0) rectangle ++(\squareside,\squareside);
\end{tikzpicture}

\end{document}


-
you can write \pgfmathsetmacro{\totallength}{10} at the beginning of your code and outside the tikzpicture environment – Alain Matthes Nov 27 '11 at 16:51
In your example \totallength is not really used. Did you mean \pgfmathsetmacro{\squarelength}{\totallength-\trianglelength} instead of \pgfmathsetmacro{\squarelength}{__10__-\trianglelength} – amorua Nov 27 '11 at 21:31
@amorua: Of course! That's one silly error.. – Tom Bombadil Nov 27 '11 at 21:36