4

I am just about getting my head around the tikz point grid and have managed to draw triangles using this method, but is there a way I can draw by directly inputing line lengths and angles?

e.g. if I wanted angles of 125 degrees, 40 Degrees and 15 degrees with the Hypotenuse at 15cm, how would I set this out?

  • 1
    May be try Intersections as explained in Section 13.3.2 of Tikz Manual version 3.0.1a. Or you can tweak the trapezium shape as defined in Section 67.3 of the same manual. – berkus Jul 9 '17 at 22:25
9

For comparison, here is an alternative method using Metapost. (The code is ConTeXt code, but you can also use metapost code in LaTeX using the gmp package).

In Metapost, unknown numeric values may be specified using the whatever keyword. Metapost figures out the value of whatevers so that all equations are satisfied.

Let's label the vertices of the triangle as A, B, and C. Suppose we want to draw AB to be parallel to the x-axis, AC to be the hypotenuse, and angle A to be 40, and angle B to be 125. (Angle C will automatically be 15). We can specify this in Metapost as:

numeric angleA, angleB;
angleA := 40;
angleB := 125;

numeric AC;
AC := 15cm;

We choose point A as origin. Then point C is completely specified

pair A, B, C;

A := origin;
C := (AC,0) rotated angleA;

To specify point B, we give two equations for B. First is that B should be AB distance from A along the x-axis, i.e.,

B = (whatever, 0);

Second, that CB should be at an angle B, i.e.,

B = ((whatever,0) rotated -angleB) shifted C;

Metapost figures out a consistent solution for these two specifications. Here is the complete code:

\starttext
\startMPpage[offset=3mm]
  begingroup;
    numeric angleA, angleB, angleC;
    angleA := 40;
    angleB := 125;

    numeric AC;
    AC := 15cm;

    pair A, B, C;

    A := origin;
    C := (AC,0) rotated angleA;

    % Let Metapost figure out B.
    B = (whatever, 0);
    B = ((whatever,0) rotated -angleB) shifted C;

    path triangle ;
    triangle := A -- B -- C --cycle;

    draw triangle;

    pair c; c := center triangle;

    freedotlabel("$A$", A, c);
    freedotlabel("$B$", B, c);
    freedotlabel("$C$", C, c);

  endgroup;
\stopMPpage
\stoptext

which gives

enter image description here

  • This is indeed a very nice application of whatever (and nice use of freedotlabel). May I ask why you include the begingroup; and endgroup;? Is it for "good coding style" only? – mickep Jul 10 '17 at 5:29
  • @mickep: Using beinggroup and endgroup is out of habit. In ConTeXt, all MP drawings are processed using a single metapost instance, which can sometimes create interference across multiple drawings. So, I always use beinggroup and endgroup to keep the definitions local. – Aditya Jul 10 '17 at 5:48
  • Thanks for clarification. I like that all MP drawings are processed in a single instance. That way, one can "borrow" settings/variables from other (earlier) drawings, like boundingboxes. – mickep Jul 10 '17 at 5:51
  • Nice. Here's one tiny addition. center gives you the center of the bounding box of a path, rather than the centroid of the path. As a quick alternative for a triangle, you could use the point 2/3[A,1/2[B,C]]. In some cases this might give you a better result with freedotlabel. – Thruston Jul 17 '17 at 11:09
8

Like this?

\documentclass[margin=1cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
  \begin{tikzpicture}
  \def\angf{40} %First angle
  \def\angs{125} %Second angle
  \def\hypo{15} %Hypotenus
  \coordinate (O) at (0,0);
  \draw[name path=line 1] (O) --++ (\angf:\hypo) coordinate (A);
  \path[name path=line 2] (O) --++ (0:2\hypo);
  \path[name path=line 3] (A) --++ (-\angs:2\hypo);
  \path [name intersections={of=line 2 and line 3,by=E}];
  \pgfresetboundingbox
  \draw (O)--(E)--(A);
  \end{tikzpicture}
\end{document}

enter image description here

  • 4
    A \pgfresetboundingbox before \draw (O)--(E)--(A); will reduce the bounding box to the triangle and not include the (too long) paths line 2 and line 3. And as a remark: since the hypotenuse is the longest side of a triangle, a length of just \hypo for these paths would be enough. – Mike Jul 9 '17 at 23:28
7

Know your math !

The angle-lengths relations are given by the law of sines.

The output

enter image description here

The code

\documentclass[12pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.5]
  % "hypothenuse"
  \def\A{15}
  % the angles
  \def\angA{125}
  \def\angB{40}
  \pgfmathsetmacro{\angC}{180-\angA-\angB}
  % the law of sines
  \pgfmathsetmacro{\d}{\A/sin(\angA)}
  \pgfmathsetmacro{\C}{\d*sin(\angC)}
  \draw (0,0) -- (\angB:\A) -- (0:\C) -- cycle;
\end{tikzpicture}
\end{document}

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.