Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to draw three congruent, equilateral triangles unicursally in TikZ (see figure below). Per se, this is rather simple, but I want to write the TikZ code so that I can easily change the shape of the figure by adjusting a few geometrical parameters. More specifically, I want the coordinates in the TikZ figure to vary, depending on the value of the parameters.

three unicursal, congruent, equilateral triangles

The two parameters I want to change are the distances from a to q (later referred to as L) and from q to b (later referred to as Δ) in the figure above. By choosing a to be the origin, the expressions for the coordinates will be as follows

a: ( 0 , 0 )
b: ( cos(60°)*(L + Δ) , sin(60°)*(L + Δ) )
c: ( bx + cos(60°)*L , by - sin(60°)*L )
d: ( L - cx , cy )
e: ( L - bx, by )
f: ( L , 0 )

where bx, by, cx and cy denote the x and y coordinates of points b and c respectively. (The coordinates of q are not necessary in order to draw the figure.)

I tried the following approach, but this gives the output shown below, where only the coordinates of a and f are correct. If this had worked, I would be able to simply change the definitions of \l and \delta and recompile to see how the geometry changes with L and Δ.

\begin{tikzpicture}

\def\cossixty{0.5}
\def\sinsixty{0.866025}

\def\l{6}
\def\delta{2}
\def\ldelta{\l+\delta}
\def\bx{\cossixty*\ldelta}
\def\by{\sinsixty*\ldelta}
\def\cx{\bx+\cossixty*\l}
\def\cy{\by-\sinsixty*\l}
\def\dx{\l-\cx}
\def\ex{\l-\bx}

\coordinate (a) at (0,0);
\coordinate (b) at (\bx,\by);
\coordinate (c) at (\cx,\cy);
\coordinate (d) at (\dx,\cy);
\coordinate (e) at (\ex,\by);
\coordinate (f) at (\l,0);

\begin{scope}[thick]
    \draw (a) -- (b) -- (c) -- (d) -- (e) -- (f) -- (a);
\end{scope}

\end{tikzpicture}

erroneous output

So my question is this: how can I make TikZ compute the coordinate expressions listed above into floating point numbers and get the correct output?

(This is only a small part of a larger image which I am to make in TikZ, so there is, of course, a good reason why I choose to draw the figure unicursally, and with adjustable parameters.)

share|improve this question
    
You can't do this with TikZ intersection functionality? –  Seamus Jul 22 '12 at 20:06
    
@Seamus I don't see how I can do this. Perhaps you could elaborate? –  eiterorm Jul 22 '12 at 20:43

3 Answers 3

up vote 10 down vote accepted

You code works just fine once you let pgf do the math for you:

enter image description here

Notes:

  • \delta is a greek letter so better to use a different name.
  • I also added a node to label the two parameters.

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\def\l{4}
\def\MyDelta{2}
\pgfmathsetmacro\ldelta{\l+\MyDelta}
\pgfmathsetmacro\bx{cos(60)*\ldelta}
\pgfmathsetmacro\by{sin(60)*\ldelta}
\pgfmathsetmacro\cx{\bx+cos(60)*\l}
\pgfmathsetmacro\cy{\by-sin(60)*\l}
\pgfmathsetmacro\dx{\l-\cx}
\pgfmathsetmacro\ex{\l-\bx}

\coordinate (a) at (0,0);
\coordinate (b) at (\bx,\by);
\coordinate (c) at (\cx,\cy);
\coordinate (d) at (\dx,\cy);
\coordinate (e) at (\ex,\by);
\coordinate (f) at (\l,0);

\begin{scope}[thick]
    \draw (a) -- (b) -- (c) -- (d) -- (e) -- (f) -- (a)
        node [below, midway] {$l = \l, \Delta = \MyDelta$};
\end{scope}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks! I see the solution was rather simple. Do you know why the \def command did not give the intended output? –  eiterorm Jul 22 '12 at 20:41
    
@eiterorm: I don't think LaTeX can do complex math by itself. You can include the calc package and do some rudimentary math, but that was not enough for this use. I did not try to use \numepxr <calculation> \relax. I just always use \pgfmathsetmcro{}{}. See What is the preferred way of defining a TikZ constant? for reference. –  Peter Grill Jul 22 '12 at 21:11

Just for the fun of it:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\def\qadist{3cm}
\def\qbshift{1cm}
\pgfmathsetmacro{\myheight}{\qadist/sin(60)}
\tikzset{equilatri/.style={regular polygon,regular polygon sides=3,
                           draw, anchor=corner 2,minimum height=\myheight
                           }
         }
\node[equilatri] (a) {};
\node[equilatri] at ([shift={(60:\qbshift)}]a.corner 2) (b) {};
\node[equilatri] at ([shift={(120:\qbshift)}]a.corner 2) (c) {};
\end{tikzpicture}
\end{document}

enter image description here

and a little more compact with the same output

\begin{tikzpicture}
\def\l{3}
\def\d{1}
\draw (0,0) -- (60:\l+\d) -- ++(-60:\l) -- ++({-(\l+\d)},0) -- ++(60:\l) -- ++(-60:\l+\d) -- cycle;
\end{tikzpicture}
share|improve this answer

There are several others possibilities to create to compute the coordinates. It's possible to make calculations without defines variables. To get the final pictures, I prefer avoid nodes because it's difficult to scale the whole picture if you mix drawing and text. The only problem is to name the points used in the picture.

A) first idea : we create a macro to build an equilateral triangle. The second argument is used to place the new triangle. It's \Delta and an angle. The first argument is the length of a side. All the coordinates are a,b and c. I used \pgfnodealias to rename the coordinates.

\documentclass{article}
\usepackage{tikz}

\newcommand\equi[2]{%
     \begin{scope}[shift={(#2)}]  
         \draw (0,0)   coordinate (a)
                    -- (0:#1)  coordinate (b) 
                    -- (60:#1) coordinate (c)--cycle; 
     \end{scope}
} 

\begin{tikzpicture}
\equi{6}{0:0}  \node[below] at (a){a} ; 
\equi{6}{60:2}
\pgfnodealias{e}{a} 
 \node[below right] at (e){e} ;  
\equi{6}{120:2}
\end{tikzpicture}

\end{document} 

B) It's like percusse's solution but I use a macro.

\documentclass{article}
\usepackage{tikz}

 \newcommand\equilatri[2]{%
    \draw (0,0) -- (60:#1+#2)
                -- ++(-60:#1) 
                -- ++(0:-#1-#2)
                -- ++(60:#1)
                -- ++(-60:#1+#2)  
                --  cycle;
   \node [below] at (0:#1/2) {$l = #1, \Delta = #2$}; 
        } 

\begin{document}
\begin{tikzpicture}

\begin{scope}[thick,red]
\equilatri{4}{2}
\end{scope}
\begin{scope}[thick,xshift=6cm,blue]
\equilatri{6}{1}
\end{scope}
\end{tikzpicture} 

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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