12

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.

My preferred output

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?

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

2 Answers 2

12
\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}  

enter image description here

5
  • 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. Commented Nov 27, 2011 at 23:38
  • @N3buchadnezzar: I believe that Altermundus wrote tkz-euclide!
    – qubyte
    Commented Nov 28, 2011 at 0:11
  • I know! He is one awesome guy! Commented Nov 28, 2011 at 0:22
  • @N3buchadnezzar, it should be enough to remove the \par.
    – quinmars
    Commented Nov 28, 2011 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. Commented Nov 28, 2011 at 6:36
16

\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}

enter image description here

3
  • you can write \pgfmathsetmacro{\totallength}{10} at the beginning of your code and outside the tikzpicture environment Commented Nov 27, 2011 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
    Commented Nov 27, 2011 at 21:31
  • @amorua: Of course! That's one silly error.. Commented Nov 27, 2011 at 21:36

You must log in to answer this question.

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