5

I am hoping to create a triangle-solving problem generator, similar to the one here.

My goal is to automatically generate different triangle solving problems for my students. Say given A=49°, b=5 and c=7, I am hoping that the program will generate the picture below. Similarly it would be great for the system to allow me to enter the triangle data by other axioms such as SSS, ASA.

I have tagged tikz-pgf here but other drawing languages are welcome.

enter image description here

  • 3
    Are you willing to pay for this software development? – Thruston Feb 26 at 20:34
  • @Thruston, I am afraid not... I am hoping to find a good Samaritan here. – Zuriel Feb 26 at 20:45
  • 2
    This already has been achieved in this answer. I plan adjusting the code to your problem a bit later. – Schrödinger's cat Feb 26 at 20:51
  • 1
    @Schrödinger'scat, thank you so much!! – Zuriel Feb 26 at 21:03
  • I've find all six possible cases in Keit, Oldham,Myland and Spanier - An Atlas of Functions pag. 349 (without proofs). – vi pa Feb 27 at 10:08
8

Here an answer with tkz-euclide.

Solution for the second part of the question: How to use SSS, SAS and ASA Here my solution:

SSS, SAS and ASA in the example

\documentclass{standalone} 
\usepackage{tkz-euclide}
\makeatletter
\def\opttr{0}
\pgfkeys{/defTriangle/.cd,
SSS/.code args={a=#1 b=#2 c=#3}{%
                                  \def\a{#1}%
                                  \def\b{#2}%
                                  \def\c{#3}
                                  \def\opttr{0}},
SAS/.code args={b=#1 A=#2 c=#3}{%
                                  \def\b{#1}%
                                  \def\A{#2}%
                                  \def\c{#3}
                                  \def\opttr{1}},
ASA/.code args={A=#1 c=#2 B=#3}{%
                                  \def\A{#1}%
                                  \def\c{#2}%
                                  \def\B{#3}
                                  \def\opttr{2}}} 
\def\DefTriangle[#1]#2{% 
\begingroup 
\pgfqkeys{/defTriangle}{#1}    
 \ifcase\opttr% 
   \ThreeSide(\a,\b,\c)(#2)
   \or
   \TwoSide(\b,\A,\c)(#2)
   \or
   \OneSide(\A,\c,\B)(#2)
\fi    
\endgroup
}

\def\ThreeSide(#1,#2,#3)(#4,#5,#6){%
\begingroup 
\tkzDefPoints{0/0/#4,#3/0/#5}
\tkzInterCC[R](#4,#2 cm)(#5,#1 cm) \tkzGetFirstPoint{#6}
\endgroup}

\def\TwoSide(#1,#2,#3)(#4,#5,#6){%
\begingroup 
\tkzDefPoints{0/0/#4,#3/0/#5}
\tkzDefPoint(#2:#1){#6}
\endgroup}

\def\OneSide(#1,#2,#3)(#4,#5,#6){%
\begingroup 
\tkzDefPoints{0/0/#4,#2/0/#5}
  \tkzDefPointBy[rotation= center #4 angle \A](#5)
  \tkzGetPoint{a}
  \tkzDefPointBy[rotation= center #5 angle -\B](#4)
  \tkzGetPoint{b}
  \tkzInterLL(#4,a)(#5,b)
  \tkzGetPoint{#6}
\endgroup}
\makeatother

\begin{document} 

\begin{tikzpicture}
  \DefTriangle[SSS={a=4 b=5 c=5}]{A,B,C}
  \tkzDrawPolygon(A,B,C)
  \tkzDrawPoints(A,B,C)
  \tkzLabelPoints[below](A,B)
  \tkzLabelPoints[above](C)
\end{tikzpicture}

\begin{tikzpicture}
  \DefTriangle[SAS={b=4 A=30 c=5}]{D,E,F}
  \tkzDrawPolygon(D,E,F)
  \tkzDrawPoints(D,E,F)
  \tkzLabelPoints[below](D,E)
  \tkzLabelPoints[above](F)
\end{tikzpicture}

 \begin{tikzpicture}
   \DefTriangle[ASA={A=30 c=10 B=60}]{A,B,C}
   \tkzDrawPolygon(A,B,C)
   \tkzDrawPoints(A,B,C)
   \tkzLabelPoints[below](A,B)
   \tkzLabelPoints[above](C)
 \end{tikzpicture}

\end{document} 

enter image description here

A) The simplest solution:

\documentclass{standalone}
\usepackage{tkz-euclide}
\begin{document}\begin{tikzpicture}
 %def
 \tkzDefPoints{0/0/A,7/0/B}
  \tkzDefPoint(49:5){C}
  % draw
  \tkzDrawPolygon(A,B,C)
  \tkzDrawPoints(A,B,C)
  %marks
  \tkzMarkAngle[size=1.3cm](B,A,C)
  %label
  \tkzLabelSegment[below](A,B){$ 7 $}
  \tkzLabelSegment[left](A,C){$ 5 $}
  \tkzLabelSegment[right](B,C){$ a $}
  \tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
  \tkzLabelPoints(A,B)
  \tkzLabelPoints[above](C)
\end{tikzpicture}
\end{document} 

B) More complicated solution to see some possibilities of the package.

Nothing to say about A and B.

Then you need to use a special value of an angle. You can use a rotation. You get a point c.

The last macro is more subtle ... With the option linear you can get a point on the line Ac with linear normed you get a point C such as AC=1 then with linear normed,K=5 you get AC=5. That's all. You have the three points A,B and C. The you can use tkz-euclide with TikZ's options or you can use only TikZ.

\documentclass{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
 % def
  \tkzDefPoints{0/0/A,7/0/B}
  \tkzDefPointBy[rotation= center A angle 49](B)
  \tkzGetPoint{c}
  \tkzDefPointWith[linear normed ,K=5](A,c)
  \tkzGetPoint{C}
 % drawing
  \tkzDrawPolygon(A,B,C)
  \tkzDrawPoints(A,B,C)
 % marking
  \tkzMarkAngle[size=1.3cm](B,A,C)
 % labelling
  \tkzLabelSegment[below](A,B){$ 7 $}
  \tkzLabelSegment[left](A,C){$ 5 $}
  \tkzLabelSegment[right](B,C){$ a $}
  \tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
  \tkzLabelPoints(A,B)
  \tkzLabelPoints[above](C)
\end{tikzpicture}
\end{document}

enter image description here

C) minhthien_2016's solution with more tkz-euclide macros

\documentclass{standalone}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture}[declare function={a=5;b=7;myAngle=49;}] 
\path (0,0)  coordinate  (B)
({sqrt(a*a+b*b-2*a*b*cos(myAngle))},0)  coordinate  (C)
({b*b -a*b*cos(myAngle))/sqrt(a*a+b*b-2*a*b*cos(myAngle))}
,{a*b*sin(myAngle)/sqrt(a*a+b*b-2*a*b*cos(myAngle))})  coordinate  (A)
;

\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B)
\tkzLabelPoints[above](C)
\tkzLabelSegment[above](A,B){$ 7 $}
\tkzDrawPolygon(A,B,C)
\tkzLabelSegment[right](A,C){$ 5 $}
\tkzLabelSegment[below](B,C){$ a $}
\tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
\tkzMarkAngle[size=1.3cm](B,A,C)
\end{tikzpicture}

\end{document}

D) minhthien_2016's solution with only tkz-euclide macros

The problem is $tkz-euclideusesxfpto evaluate the coordinates so I need to determine these coordinates before a call to\tkzDefPoint

\documentclass{article}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture}[declare function={a=5;b=7;myAngle=49;}] 

\pgfmathparse{sqrt(a*a+b*b-2*a*b*cos(myAngle))}
\let\xc\pgfmathresult
\pgfmathparse{b*b-a*b*cos(myAngle))/sqrt(a*a+b*b-2*a*b*cos(myAngle))}
\let\xa\pgfmathresult
\pgfmathparse{a*b*sin(myAngle)/sqrt(a*a+b*b-2*a*b*cos(myAngle))}
\let\ya\pgfmathresult

\tkzDefPoints{\xa/\ya/A,0/0/B,\xc/0/C}
\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B)
\tkzLabelPoints[above](C)
\tkzLabelSegment[above](A,B){$ 7 $}
\tkzDrawPolygon(A,B,C)
\tkzLabelSegment[right](A,C){$ 5 $}
\tkzLabelSegment[below](B,C){$ a $}
\tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
\tkzMarkAngle[size=1.3cm](B,A,C)
\end{tikzpicture}

\end{document}

enter image description here

E) With tkz-euclide and xfp

\documentclass{standalone}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture}
\def\a{5}  \def\b{7}  \def\myangle{49}
\tkzDefPoints{{(\b*\b -\a*\b*cos(\myangle))/sqrt(\a*\a+\b*\b-2*\a*\b*cos(\myangle))}%
             /{\a*\b*sin(-\myangle)/sqrt(\a*\a+\b*\b-2*\a*\b*cos(\myangle))}/A,%
               0/0/B,%
              {sqrt(\a*\a+\b*\b-2*\a*\b*cos(\myangle))}/0/C}
\tkzDrawPoints(A,B,C)
\tkzLabelPoints(A,B)
\tkzLabelPoints[above](C)
\tkzLabelSegment[above](A,B){$ 7 $}
\tkzDrawPolygon(A,B,C)
\tkzLabelSegment[right](A,C){$ 5 $}
\tkzLabelSegment[below](B,C){$ a $}
\tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
\tkzMarkAngle[size=1.3cm](B,A,C)
\end{tikzpicture}

\end{document}

F) with three sides

\documentclass{standalone}
\usepackage{tkz-euclide}

\begin{document}
  \begin{tikzpicture}
    \pgfmathsetmacro{\a}{3} % BC
    \pgfmathsetmacro{\b}{5} % AC
    \pgfmathsetmacro{\c}{7} % AB

  \tkzDefPoints{0/0/A,\c/0/B}
  \tkzInterCC[R](A,\b cm)(B,\a cm) \tkzGetFirstPoint{C}
  \tkzDrawPolygon(A,B,C) 
  \tkzLabelSegment[below](A,B){$ 7 $}
  \tkzLabelSegment[above left](A,C){$ 5 $}
  \tkzLabelSegment[above right](B,C){$ 3 $}
  \tkzLabelPoints[below](A,B)
  \tkzLabelPoints[above](C)
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • This is a great answer and is much better than what I expected. Thank you so much for your time!! – Zuriel Feb 27 at 16:20
  • @Zuriel Why do not accept the answer? – minhthien_2016 Feb 28 at 0:26
  • @minhthien_2016, accepted. Thank you for reminding me! – Zuriel Feb 28 at 15:53
9

You can try this code.

\documentclass[border=2mm,12pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[
declare function={a=5;b=7;myAngle=49;}] 
\path (0,0)  coordinate  (B)
({sqrt(a*a+b*b-2*a*b*cos(myAngle))},0)  coordinate  (C)
({(b*b - a*b*cos(myAngle))/sqrt(a*a+b*b-2*a*b*cos(myAngle))},{a*b*sin(myAngle)/sqrt(a*a+b*b-2*a*b*cos(myAngle))})  coordinate  (A)
;
\foreach \p in {A,B,C}
\draw[fill=black] (\p) circle (1.5pt);
\foreach \p/\g in {A/90,B/-90,C/-90}
\path (\p)+(\g:3mm) node{$\p$};
\draw (A) -- (B) node[midway,above]{$ 7 $};
\draw (A) -- (C) node[midway,right]{$ 5 $};
\draw (B) -- (C) node[midway,below]{$ a $};
\tkzLabelAngle[pos = 0.8](B,A,C){$49^\circ$}
\tkzMarkAngle[size=1.3cm](B,A,C)
\end{tikzpicture}
\end{document} 

enter image description here

You can change the values a, b, myAngle.

\documentclass[border=2mm,12pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[
    declare function={a=5;b=3;myAngle=120;}] 
 \path (0,0)  coordinate  (B)
 ({sqrt(a*a+b*b-2*a*b*cos(myAngle))},0)  coordinate  (C)
 ({(b*b - a*b*cos(myAngle))/sqrt(a*a+b*b-2*a*b*cos(myAngle))},{a*b*sin(myAngle)/sqrt(a*a+b*b-2*a*b*cos(myAngle))})  coordinate  (A)
 ;
 \foreach \p in {A,B,C}
 \draw[fill=black] (\p) circle (1.5pt);
 \foreach \p/\g in {A/90,B/-90,C/-90}
 \path (\p)+(\g:3mm) node{$\p$};
 \draw (A) -- (B) node[midway,above]{$ a $};
 \draw (A) -- (C) node[midway,above]{$ b $};
 \draw (B) -- (C) node[midway,below]{$ \sqrt{a^2 + b^2 - 2ab\cos \alpha } $};
\tkzLabelAngle[pos = 0.3](B,A,C){$\alpha$}
\tkzMarkAngle[size=0.8cm](B,A,C)
\end{tikzpicture}
\end{document} 

enter image description here

With triangle knowing three sides (SSSTriangle), you can use this code. In this code, the triangle ABC, where AB=c, BC = a, AC = b.

\documentclass[12pt, border = 1mm]{standalone}
\usepackage{tkz-euclide}
\usepackage{tikz}
\begin{document}
     \begin{tikzpicture}[scale=1,declare function={a=3;b=5;c=7;}]
\coordinate (A) at (0,0);
\coordinate (B) at (c,0);
\coordinate (C) at  ({(pow(b,2) + pow(c,2) - pow(a,2))/(2*c)},{sqrt((a+b-c) *(a-b+c) *(-a+b+c)* (a+b+c))/(2*c)});
\foreach \p in {A,B,C}
\draw[fill=black] (\p) circle (1.5pt);
\foreach \p/\g in {A/180,C/90,B/-90}
\path (\p)+(\g:3mm) node{$\p$};
%\draw (A) -- (B) -- (C) -- cycle;
\draw (A) -- (B) node[midway,below]{$  7 $};
\draw (A) -- (C) node[midway,above]{$ 5 $};
\draw (B) -- (C) node[midway,above]{$ 3 $};
\end{tikzpicture}
\end{document}

enter image description here

Triangle ABC, knowing angles A and B and side AB

\documentclass[border=2mm,12pt]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[scale=1,declare function={c=3;AngleA=30;AngleB=90;}]
\coordinate (A) at (0,0);
\coordinate (B) at (c,0);
\coordinate (C) at  ({c*cos (AngleA)* cosec(AngleA + AngleB) *sin(AngleB)}, {c* cosec(AngleA + AngleB)*sin (AngleA)* sin (AngleB)});
\foreach \p in {A,B,C}
\draw[fill=black] (\p) circle (1.5pt);
\foreach \p/\g in {A/-90,C/90,B/-90}
\path (\p)+(\g:3mm) node{$\p$};
\draw (A) -- (B) -- (C) -- cycle;
\end{tikzpicture}
\end{document} 

enter image description here

| improve this answer | |
  • I have a problem with your code here:({b*b - a*b*cos(myAngle)) the brace is alone in brackets. Strangely the code compiles – Alain Matthes Feb 27 at 7:35
  • you need to write ({(b*b - a*b*cos(myAngle))... I edit your answer – Alain Matthes Feb 27 at 9:33
  • @AlainMatthes Thank you very much. – minhthien_2016 Feb 27 at 9:40
3

Here is another version using a pic. You can specify the sides, a, b and c, or two sides and one angle or one side and two angles. It ahould now cover all possible cases. However, if the solution is not unique, it will pick one solution. Also there are not yet sanity checks for all possible inputs in place, but there are some. The code is not very short, this may be the price one has to pay for some sort of user friendliness.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{angles,calc}
\tikzset{pics/triangle/.style={code={
    \tikzset{triangle pars/.cd,#1}%
    \def\pv##1{\pgfkeysvalueof{/tikz/triangle pars/##1}}%
    \edef\lstsides{"a","b","c"}%
    \edef\lstangles{"A","B","C"}%
    \def\tmpundef{undef}%
    \edef\temp{\pv{a}}%
    \ifx\temp\tmpundef
     \edef\nsides{0}%
    \else
     \edef\nsides{1}%
     \edef\firstside{0}%
    \fi
    \edef\temp{\pv{b}}%
    \ifx\temp\tmpundef
    \else
     \edef\nsides{\the\numexpr\nsides+1}%
     \ifnum\nsides=1
      \edef\firstside{1}%
     \else
      \edef\secondside{1}%
     \fi
    \fi
    \edef\temp{\pv{c}}%
    \ifx\temp\tmpundef
    \else
     \edef\nsides{\the\numexpr\nsides+1}%
     \ifnum\nsides=1
      \edef\firstside{2}%
     \else
      \ifcase\nsides
       \or
        \edef\firstside{2}%
       \or
        \edef\secondside{2}%
       \or
        \edef\thirdside{c}%
      \fi 
     \fi
    \fi
    \edef\temp{\pv{A}}%
    \ifx\temp\tmpundef
     \edef\nangles{0}%
    \else
     \edef\nangles{1}%
     \edef\firstangle{0}%
    \fi
    \edef\temp{\pv{B}}%
    \ifx\temp\tmpundef
    \else
     \edef\nangles{\the\numexpr\nangles+1}%
     \ifnum\nangles=1
      \edef\firstangle{1}%
     \else
      \edef\secondangle{1}%
     \fi
    \fi
    \edef\temp{\pv{C}}%
    \ifx\temp\tmpundef
    \else
     \edef\nangles{\the\numexpr\nangles+1}%
     \ifcase\nangles
     \or
      \edef\firstangle{2}%
     \or
      \edef\secondangle{2}%
     \or
      \edef\thirdangle{2}%
     \fi     
    \fi
    \ifnum\numexpr\nangles+\nsides=3 % the number of input parameters is fine
     \ifcase\nsides
      \message{You need to specify at least one side.^^J}
     \or % one side and two angles
      \pgfmathsetmacro{\mysidei}{{\lstsides}[\firstside]}%
      \pgfmathsetmacro{\myanglei}{{\lstangles}[\firstangle]}%
      \pgfmathsetmacro{\myangleii}{{\lstangles}[\secondangle]}%
      \pgfmathtruncatemacro{\thirdangle}{Mod(3-\firstangle-\secondangle,3)}%
      \pgfmathsetmacro{\myangleiii}{{\lstangles}[\thirdangle]}%
      \pgfmathtruncatemacro{\itest}{(\firstside==\firstangle)||(\firstside==\secondangle)}%
      \ifnum\itest=0 % both angles involve known side
        \draw[pic actions] (0,0) 
            coordinate[label=below:$\pv{\myangleii}$] (-B) 
         -- node[midway,auto]{$\mysidei=\pv{\mysidei}$} 
         (\pv{a},0) coordinate (-C) 
         --
          (intersection cs:first line={(-B)--($(-B)+({\pv{\myangleii}}:1)$)},
          second line={(-C)--($(-C)+({-180+\pv{\myanglei}}:1)$)})
          coordinate (-A) -- cycle;   
      \else % one angle is away from the known side
       \ifnum\firstside=\firstangle
        \draw[pic actions] (0,0) 
         coordinate[label=below:$\myangleii$] (-\myangleii) 
         -- node[midway,auto]{$\mysidei=\pv{\mysidei}$} 
         (\pv{a},0) coordinate[label=below:$\myangleiii$] (-\myangleiii) 
         --
          (intersection cs:first line={(-\myangleii)--($(-\myangleii)+({\pv{\myangleii}}:1)$)},
          second line={(-\myangleiii)--($(-\myangleiii)+({-180+\pv{\myanglei}+\pv{\myangleii}}:1)$)})
          coordinate[label=above:$\myanglei$] (-\myanglei) -- cycle
          ($(-\myangleii)+(0:\pv{r})$)arc[start angle=0,end angle=\pv{\myangleii},radius=\pv{r}]
          ($(-\myanglei)+(180+\pv{\myangleii}:\pv{r})$)
          arc[start angle=180+\pv{\myangleii},end angle=180+\pv{\myanglei}+\pv{\myangleii},radius=\pv{r}]
          ;
       \else
        \draw[pic actions] (0,0) 
         coordinate[label=below:$\myanglei$] (-\myanglei) 
         -- node[midway,auto]{\mysidei} 
         (\pv{a},0) coordinate[label=below:$\myangleiii$] (-\myangleiii) 
         --
          (intersection cs:first line={(-\myanglei)--($(-\myanglei)+({\pv{\myanglei}}:1)$)},
          second line={(-\myangleiii)--($(-\myangleiii)+({-180+\pv{\myanglei}+\pv{\myangleii}}:1)$)})
          coordinate[label=above:$\myangleii$] (-\myangleii) -- cycle
          ($(-\myanglei)+(0:\pv{r})$)arc[start angle=0,end angle=\pv{\myanglei},radius=\pv{r}]
          ($(-\myangleii)+(180+\pv{\myanglei}:\pv{r})$)
          arc[start angle=180+\pv{\myanglei},end angle=180+\pv{\myanglei}+\pv{\myangleii},radius=\pv{r}]
          ;
       \fi    
      \fi
     \or % two sides and one angle
      \pgfmathsetmacro{\mysidei}{{\lstsides}[\firstside]}%
      \pgfmathsetmacro{\mysideii}{{\lstsides}[\secondside]}%
      \pgfmathsetmacro{\myanglei}{{\lstangles}[\firstangle]}%
      \pgfmathtruncatemacro{\thirdside}{Mod(3-\firstside-\secondside,3)}%
      \pgfmathsetmacro{\mysideiii}{{\lstsides}[\thirdside]}%
      \pgfmathsetmacro{\myangleii}{{\lstangles}[\secondside]}%
      \pgfmathsetmacro{\myangleiii}{{\lstangles}[\thirdside]}%
      \pgfmathtruncatemacro{\itest}{(\firstside==\firstangle)||(\secondside==\firstangle)}%
      \ifnum\itest=0 % both sides attach to the angle
       \pgfmathsetmacro{\myangleii}{{\lstangles}[\firstside]}%
       \pgfmathsetmacro{\myangleiii}{{\lstangles}[\secondside]}%
       \draw[pic actions] (\pv{\myanglei}:\pv{\mysidei}) 
            coordinate[label=above:$\myangleiii$] (-\myangleiii)
        --   node[midway,auto]{$\mysidei=\pv{\mysidei}$} 
        (0,0) coordinate[label=below:$\myanglei$] (-\myanglei) 
        --  node[midway,auto]{$\mysideii=\pv{\mysideii}$}  
        (\pv{\mysideii},0) coordinate[label=below:$\myangleii$] (-\myangleii)
       --  cycle
       (\pv{r},0) arc[start angle=0,end angle=\pv{\myanglei},radius=\pv{r}];
      \else
       \pgfmathsetmacro{\mya}{max(\pv{\mysidei},\pv{\mysideii})}%
       \pgfmathsetmacro{\myb}{min(\pv{\mysidei},\pv{\mysideii})}%
       \pgfmathsetmacro{\myc}{\myb*cos(\pv{\myanglei})%
        +sqrt(\mya*\mya-pow(\myb*sin(\pv{\myanglei}),2)}%
       \ifnum\firstside=\firstangle
        \draw[pic actions] (\pv{\myanglei}:\myc) 
             coordinate[label=above:$\myangleii$] (-\myangleii)
          --  (0,0) coordinate[label=below:$\myanglei$] (-\myanglei) 
          --  node[midway,auto]{$\mysideii=\pv{\mysideii}$}  
         (\pv{\mysideii},0) coordinate[label=below:$\myangleiii$] (-\myangleiii)
        --  node[midway,auto]{$\mysidei=\pv{\mysidei}$}  cycle
        (\pv{r},0) arc[start angle=0,end angle=\pv{\myanglei},radius=\pv{r}];
       \else
        \pgfmathsetmacro{\myangleii}{{\lstangles}[\thirdside]}%
        \pgfmathsetmacro{\myangleiii}{{\lstangles}[\firstside]}%
        \draw[pic actions] (\pv{\myanglei}:\myc) 
             coordinate[label=above:$\myangleiii$] (-\myangleiii)
          --  (0,0) coordinate[label=below:$\myanglei$] (-\myanglei) 
          --  node[midway,auto]{$\mysidei=\pv{\mysidei}$}  
         (\pv{\mysidei},0) coordinate[label=below:$\myangleii$] (-\myangleii)
        --  node[midway,auto]{$\mysideii=\pv{\mysideii}$}  cycle
        (\pv{r},0) arc[start angle=0,end angle=\pv{\myanglei},radius=\pv{r}];
       \fi
      \fi
     \or %three sides
      %\typeout{3 sides}
      \pgfmathsetmacro{\mymax}{max(\pv{a},\pv{b},\pv{c})}%
      \pgfmathtruncatemacro{\itest}{sign(2*\mymax-\pv{a}-\pv{b}-\pv{c})}%
      \ifnum\itest<1
       \draw[pic actions] (0,0) coordinate[label=below:$B$] (-B) 
        -- node[midway,auto]{$a=\pv{a}$} 
        (\pv{a},0) coordinate[label=below:$C$] (-C) 
         -- node[midway,auto]{$b=\pv{b}$}
        (intersection cs:first line={(-B)--($(-B)+({cosinelaw(\pv{a},\pv{c},\pv{b})}:1)$)},
         second line={(-C)--($(-C)+({-cosinelaw(\pv{a},\pv{b},\pv{c})}:1)$)})
         coordinate[label=above:$A$] (-A) -- 
         node[midway,auto]{$c=\pv{c}$} cycle;
      \else
       \message{a=\pv{a},b=\pv{b},c=\pv{c} is not consistent since one side is
        longer than the sum of two other sides.^^J}
      \fi    
     \fi
    \else
     \message{Incorrect input. You need to specify three parameters.^^J}     
    \fi
    }},
  declare function={cosinelaw(\a,\b,\c)=acos((\a*\a+\b*\b-\c*\c)/(2*\a*\b));},
  triangle pars/.cd,
  A/.initial=undef,B/.initial=undef,C/.initial=undef,% angles
  a/.initial=undef,b/.initial=undef,c/.initial=undef,% sides
  r/.initial=1%radius of angles
  }
\begin{document}
\subsection*{Specify triangle by its three sides}
\begin{tikzpicture}
  \pic{triangle={a=5,b=4,c=3}};
\end{tikzpicture}

\subsection*{Specify triangle by two sides and one angle}
\begin{tikzpicture}
  \pic{triangle={a=5,b=4,A=40}};
\end{tikzpicture}

\begin{tikzpicture}
  \pic{triangle={a=5,b=4,B=40}};
\end{tikzpicture}

\begin{tikzpicture}
  \pic{triangle={a=5,b=4,C=40}};
\end{tikzpicture}


\subsection*{Specify triangle by one side and two angles}
\begin{tikzpicture}
  \pic{triangle={a=5,A=60,B=70}};
\end{tikzpicture}

\begin{tikzpicture}
  \pic{triangle={a=5,A=60,C=70}};
\end{tikzpicture}

\end{document}

enter image description here

enter image description here

| improve this answer | |
  • Best solution actually for the second part of the question – Alain Matthes Feb 27 at 8:09
  • @Schrödinger's cat I've find all six possible cases in Keit, Oldham,Myland and Spanier - An Atlas of Functions pag. 349 (without proofs). – vi pa Feb 27 at 10:19
  • @vipa Triangles are uniquely determined by specifying three sides (SSS theorem), two angles and a side (AAS theorem), or two sides with an adjacent angle (SAS theorem) [mathworld.wolfram.com/Triangle.html] – Alain Matthes Feb 27 at 12:44
  • @AlainMatthes Sure, this is what this code does. – Schrödinger's cat Feb 27 at 14:53
  • @AlainMatthes An old edition of the book that I mention above is available to borrow (legally I think) here. In that edition the cases are at pag. 329. They are 1- three sides (SSS); 2- two sides and the angle opposite the longer of those two sides; 3- two sides and the angle opposite the shorter of those two sides; 4- two sides and the angle between them (SAS); 5- two angles and the side opposite one of them; 6- two angles and the side between them (ASA) – vi pa Feb 27 at 18:58
1

All possible solutions (I think):

\documentclass{article}
\usepackage{booktabs}
\usepackage{isonums} %for comma in number instead of point
\usepackage{tikz}
\usetikzlibrary{math,calc,angles,backgrounds}
%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\sideLabel}[3]{\node at ($($#1!0.5!#2$)!3mm!90:#2$) {$#3$};}
%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\sideMark}[3]{\node[red] at ($($#1!0.5!#2$)!3mm!90:#2$) {$#3$};}
%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\angleMark}[4]{
\coordinate (A) at #1;
\coordinate (B) at #2;
\coordinate (C) at #3;
\begin{scope}[on background layer]
\path pic[red,pic text=$#4$,draw,angle eccentricity=1.6]{angle=C--B--A};
\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\angleLabel}[4]{
\coordinate (A) at #1;
\coordinate (B) at #2;
\coordinate (C) at #3;
\path pic[pic text=$#4^\circ$,draw,angle eccentricity=1.6]{angle=C--B--A};
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\solutions}[1]{
\pgfkeys{/pgf/number format/precision={2}}
\node[anchor=north west,red] at (current bounding box.south west){[#1]};}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\SSS}[3]{%
\tikzmath{%
\angleA = acos((#2^2 + #3^2 -(#1^2))/(2*#2*#3));
\angleB = acos((#1^2 + #3^2 -(#2^2))/(2*#1*#3));
\angleC = 180 - \angleA - \angleB;
}
% draw triangle
\draw (0,0) -- (\angleB:#3) -- (#1,0) -- cycle;
% labels
\sideLabel{(0,0)}{(\angleB:#3)}{#3}
\sideLabel{(\angleB:#3)}{(#1,0)}{#2}
\sideLabel{(#1,0)}{(0,0)}{#1}
%unknows
\angleMark{(#1,0)}{(\angleB:#3)}{(0,0)}{A}
\angleMark{(\angleB:#3)}{(0,0)}{(#1,0)}{B}
\angleMark{(0,0)}{(#1,0)}{(\angleB:#3)}{C}
% Solutions
\solutions{$A=\pgfmathprintnumber{\angleA}^\circ$, $B=\pgfmathprintnumber{\angleB}^\circ$, $C=\pgfmathprintnumber{\angleC}^\circ$};
}
%%%%%%%%%%%%%%%%%%%
\newcommand{\SAS}[3]{%
\tikzmath{%
\ThirdSide = sqrt(#1^2 + #3^2 - 2*#1*#3*cos(#2));
\AngleOppositeFirstSide = acos((#3^2 + \ThirdSide^2 -(#1^2))/(2*#3*\ThirdSide));
\AngleOppositeSecondSide = acos((#1^2 + \ThirdSide^2 -(#3^2))/(2*#1*\ThirdSide));
}
%draw triangle
\draw (0,0) -- (\AngleOppositeSecondSide:\ThirdSide) -- (#1,0) -- cycle;
%labels
\sideLabel{(\AngleOppositeSecondSide:\ThirdSide)}{(#1,0)}{#3}
\sideLabel{(#1,0)}{(0,0)}{#1}
\angleLabel{(0,0)}{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{#2}
% unknows
\sideMark{(0,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{a}
\angleMark{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{A}
\angleMark{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{(#1,0)}{B}
% solutions
\solutions{$a=\pgfmathprintnumber{\ThirdSide}$, $A=\pgfmathprintnumber{\AngleOppositeFirstSide}^\circ$, $B=\pgfmathprintnumber{\AngleOppositeSecondSide}^\circ$}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ASA}[3]{%
\tikzmath{%
\ThirdAngle = 180 - #1 - #3;
\SideOppositeFirstAngle =   #2*sin(#1)/sin(\ThirdAngle);
\SideOppositeSecondAngle =  #2*sin(#3)/sin(\ThirdAngle);
}
% draw triangle
\draw (0,0) -- (#1:\SideOppositeSecondAngle) -- (#2,0) -- cycle;
% labels
\angleLabel{(#1:\SideOppositeSecondAngle)}{(0,0)}{(#2,0)}{#1}
\sideLabel{(#2,0)}{(0,0)}{#2}
\angleLabel{(0,0)}{(#2,0)}{(#1:\SideOppositeSecondAngle)}{#3}
% unknows
\sideMark{(#1:\SideOppositeSecondAngle)}{(#2,0)}{a}
\sideMark{(0,0)}{(#1:\SideOppositeSecondAngle)}{b}
\angleMark{(#2,0)}{(#1:\SideOppositeSecondAngle)}{(0,0)}{A}
% solutions
\solutions{$a=\pgfmathprintnumber{\SideOppositeFirstAngle}$, $b=\pgfmathprintnumber{\SideOppositeSecondAngle}$, $A=\pgfmathprintnumber{\ThirdAngle}^\circ$}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\AAS}[3]{%
\tikzmath{%
\ThirdAngle = 180 - #1 - #2;
\SideOppositeSecondAngle =  #3*sin(#2)/sin(#1);
\SideOppositeThirdAngle =   #3*sin(\ThirdAngle)/sin(#1);
}
% draw triangle
\draw (0,0) -- (#1:\SideOppositeSecondAngle) -- (\SideOppositeThirdAngle,0) -- cycle;
% labels
\angleLabel{(#1:\SideOppositeSecondAngle)}{(0,0)}{(\SideOppositeThirdAngle,0)}{#1}
\angleLabel{(0,0)}{(\SideOppositeThirdAngle,0)}{(#1:\SideOppositeSecondAngle)}{#2}
\sideLabel{(#1:\SideOppositeSecondAngle)}{(\SideOppositeThirdAngle,0)}{#3}
%unknows
\sideMark{(\SideOppositeThirdAngle,0)}{(0,0)}{a}
\sideMark{(0,0)}{(#1:\SideOppositeSecondAngle)}{b}
\angleMark{(\SideOppositeThirdAngle,0)}{(#1:\SideOppositeSecondAngle)}{(0,0)}{A}
%solutions
\solutions{$a=\pgfmathprintnumber{\SideOppositeThirdAngle}$, $b=\pgfmathprintnumber{\SideOppositeSecondAngle}$, $A=\pgfmathprintnumber{\ThirdAngle}^\circ$}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\SsA}[3]{%
\tikzmath{%
\AngleOppositeSecondSide = asin((#2/#1)*sin(#3));
\AngleOppositeThirdSide = 180 - #3 -\AngleOppositeSecondSide;
\ThirdSide = #1*sin(\AngleOppositeThirdSide)/sin(#3);
}
%draw triangle
\draw (0,0) -- (\AngleOppositeSecondSide:\ThirdSide) -- (#1,0) -- cycle;
%labels
\sideLabel{(#1,0)}{(0,0)}{#1}
\sideLabel{(\AngleOppositeSecondSide:\ThirdSide)}{(#1,0)}{#2}
\angleLabel{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{#3}
%unknows
\sideMark{(0,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{a}
\angleMark{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{(#1,0)}{A}
\angleMark{(0,0)}{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{B}
%solutions
\solutions{$a=\pgfmathprintnumber{\ThirdSide}$, $A=\pgfmathprintnumber{\AngleOppositeSecondSide}^\circ$, $B=\pgfmathprintnumber{\AngleOppositeThirdSide}^\circ$}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%First Solution
\newcommand{\sSAfirst}[3]{%
\tikzmath{%
\AngleOppositeSecondSide = asin((#2/#1)*sin(#3));
\AngleOppositeThirdSide = 180 - #3 -\AngleOppositeSecondSide;
\ThirdSide = #1*sin(\AngleOppositeThirdSide)/sin(#3);
}
% draw triangle
\draw (0,0) -- (\AngleOppositeSecondSide:\ThirdSide) -- (#1,0) -- cycle;
% labels
\sideLabel{(#1,0)}{(0,0)}{#1}
\sideLabel{(\AngleOppositeSecondSide:\ThirdSide)}{(#1,0)}{#2}
\angleLabel{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{#3}
% unknows
\sideMark{(0,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{a}
\angleMark{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{(#1,0)}{A}
\angleMark{(0,0)}{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{B}
% solutions
\solutions{$a=\pgfmathprintnumber{\ThirdSide}$, $A=\pgfmathprintnumber{\AngleOppositeSecondSide}^\circ$, $B=\pgfmathprintnumber{\AngleOppositeThirdSide}^\circ$}
}
%%%Second Solution
\newcommand{\sSAsecond}[3]{%
\tikzmath{%
\AngleOppositeSecondSide = 180 - asin((#2/#1)*sin(#3));
\AngleOppositeThirdSide = 180 - #3 -\AngleOppositeSecondSide;
\ThirdSide = #1*sin(\AngleOppositeThirdSide)/sin(#3);
}
% draw triangle
\draw (0,0) -- (\AngleOppositeSecondSide:\ThirdSide) -- (#1,0) -- cycle;
% labels
\sideLabel{(#1,0)}{(0,0)}{#1}
\sideLabel{(\AngleOppositeSecondSide:\ThirdSide)}{(#1,0)}{#2}
\angleLabel{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{#3}
% unknows
\sideMark{(0,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{a}
\angleMark{(\AngleOppositeSecondSide:\ThirdSide)}{(0,0)}{(#1,0)}{A}
\angleMark{(0,0)}{(#1,0)}{(\AngleOppositeSecondSide:\ThirdSide)}{B}
% solutions
\solutions{$a=\pgfmathprintnumber{\ThirdSide}$, $A=\pgfmathprintnumber{\AngleOppositeSecondSide}^\circ$, $B=\pgfmathprintnumber{\AngleOppositeThirdSide}^\circ$}
}
\begin{document}
\pagestyle{empty}
\section{SSS} %the sum of any two sides must less then the third side.
\tikz{\SSS{3}{4}{5}}

\section{SAS} %the angle must be less than 180
\tikz{\SAS{5}{40}{4}}

\section{ASA} %the sum of two angle must be less then 180 
\tikz{\ASA{30}{5}{40}}

\section{AAS} %the sum of two angle must be less then 180 
\tikz{\AAS{30}{40}{5}}

\section{SsA} %S must be greater or equal to s. If S is equal to s (isoscele triangle) the angle must be less then 90
\tikz{\SsA{5}{4}{40}}

\section{sSA} %S must be greater or equal to s. In this case it's possible to draw two different triangles. 
%If S is equal to s (isoscele triangle) the triangle is unique and the same as SsA.
%The angle A must be less then arcsin(s/S)
\begin{tabular}{ll}
\tikz{\sSAfirst{4}{5}{40}}&
\tikz{\sSAsecond{4}{5}{40}}\\
\end{tabular}
\end{document}

construct a triangle form three element-1 enter image description here

| improve this answer | |

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.