6

I want to draw several different triangles ABC with the angles marked and the angle value written in it. Thanks to answers on on another question, I've managed to get this code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all} %% om allerhande objecten te gebruiken zoals gradenboog...
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}


\begin{document}

\makeatletter
\newcommand\ddToddmmss[1]{%
\pgfmathtruncatemacro\deg@tmp{#1}%
\pgfmathsetmacro\min@tmp{(#1-\deg@tmp)*60}%
\pgfmathtruncatemacro\Min@tmp{\min@tmp}
\pgfmathtruncatemacro\sec@tmp{(\min@tmp-floor(\min@tmp))*60}%
\ensuremath{\deg@tmp^\circ \Min@tmp ' \sec@tmp ''}}
\makeatother


\begin{tikzpicture}
\tkzInit[ymin=0,ymax=9,xmin=0,xmax=15]
\tkzClip
\tkzGrid
\tkzDefPoints{1/3/A, 7/8/B, 12/1/C}
\tkzDrawPolygon(A,B,C) \tkzDrawPoints(A,B,C) \tkzLabelPoints(A,B,C)

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](C,A,B);
\tkzFindAngle(C,A,B) \tkzGetAngle{angleCAB};
%\FPround\angleBAC\angleCAB{2}
\tkzLabelAngle[pos = 2.3](C,A,B){\ddToddmmss{\angleCAB}};  

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](A,B,C);
\tkzFindAngle(A,B,C) \tkzGetAngle{angleABC};
%\FPround\angleABC\angleABC{2}
\tkzLabelAngle[pos = 2.3](A,B,C){\ddToddmmss{\angleABC}};

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](B,C,A);
\tkzFindAngle(B,C,A) \tkzGetAngle{angleBCA};
%\FPround\angleBCA\angleBCA{2}
\tkzLabelAngle[pos = 2.3](B,C,A){\ddToddmmss{\angleBCA}};
\end{tikzpicture}


\begin{tikzpicture}
\tkzInit[ymin=0,ymax=9,xmin=0,xmax=15]
\tkzClip
\tkzGrid
\tkzDefPoints{1/3/A, 7/8/B, 12/5/C}
\tkzDrawPolygon(A,B,C) \tkzDrawPoints(A,B,C) \tkzLabelPoints(A,B,C)

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](C,A,B);
\tkzFindAngle(C,A,B) \tkzGetAngle{angleCAB};
%\FPround\angleBAC\angleCAB{2}
\tkzLabelAngle[pos = 2.3](C,A,B){\ddToddmmss{\angleCAB}};  

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](A,B,C);
\tkzFindAngle(A,B,C) \tkzGetAngle{angleABC};
%\FPround\angleABC\angleABC{2}
\tkzLabelAngle[pos = 2.3](A,B,C){\ddToddmmss{\angleABC}};

\tkzMarkAngle[fill= red,size=1.5cm, opacity=.4](B,C,A);
\tkzFindAngle(B,C,A) \tkzGetAngle{angleBCA};
%\FPround\angleBCA\angleBCA{2}
\tkzLabelAngle[pos = 2.3](B,C,A){\ddToddmmss{\angleBCA}};
\end{tikzpicture}

\end{document}

triangles 1 and 2

Problem is, in version 2, where point C is higher than point A, he takes the wrong angle.

Any suggestions?

3

If you look at how \tkzFindAngle is defined in tkz-obj-angles.tex, you will see that it uses quite simple way to calculate the angle.

\def\tkzFindAngle(#1,#2,#3){%
\begingroup
      \tkzFindSlopeAngle(#2,#1)\tkzGetAngle{tkz@FirstAngle}
      \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@SecondAngle}
      \FPadd\tkz@Angle{\tkz@SecondAngle}{-\tkz@FirstAngle}
     \global\let\tkzAngleResult\tkz@Angle
\endgroup
}

Similarly \tkz@LabelAngle calculates quite simply where to put the label.

\def\tkz@LabelAngle[#1](#2,#3,#4)#5{%
\begingroup
\pgfkeys{tkzlabelangle/.cd,
         dist  = 1}
\pgfqkeys{/tkzlabelangle}{#1}
\tkzFindSlopeAngle(#3,#2)\tkzGetAngle{tkz@dirOne}
\tkzFindSlopeAngle(#3,#4)\tkzGetAngle{tkz@dirTwo}
\FPeval\labelAngle{( \tkz@dirOne +\tkz@dirTwo)/2}
\path (#3) --+(\labelAngle:\labeldist) node[/tkzmkangle/.cd,#1] {#5};
\endgroup
}

Whereas \tkzMarkAngle uses a lot more complex calculation for the angle in \tkzDrawArcRAN to mark the angle.

\def\tkzDrawArcRAN[#1](#2,#3)(#4,#5){%
 \begingroup
 \pgfmathparse{#4}\edef\tkz@FirstAngle{\pgfmathresult}%
 \pgfmathparse{#5}\edef\tkz@SecondAngle{\pgfmathresult}%
  \pgfmathgreaterthan{\tkz@FirstAngle}{0}
  \ifdim\pgfmathresult pt=1 pt\relax%
    \pgfmathgreaterthan{\tkz@FirstAngle}{\tkz@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
     \pgfmathsubtract{\tkz@FirstAngle}{360}
     \edef\tkz@FirstAngle{\pgfmathresult}%
 \fi
 \else
     \pgfmathgreaterthan{\tkz@FirstAngle}{\tkz@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
     \pgfmathadd{\tkz@SecondAngle}{360}
     \edef\tkz@SecondAngle{\pgfmathresult}%
 \fi
 \fi
     \draw[shift = {(#2)},/drawarc/.cd,#1]%
       (\tkz@FirstAngle:#3) arc (\tkz@FirstAngle:\tkz@SecondAngle:#3);
\endgroup
}

Using the same angle normalization as \tkzMarkAngle is probably what you are after.

\makeatletter
\def\tkzFindAngle(#1,#2,#3){%
\begingroup
  \tkzFindSlopeAngle(#2,#1)\tkzGetAngle{tkz@FirstAngle}
  \tkzFindSlopeAngle(#2,#3)\tkzGetAngle{tkz@SecondAngle}
  \my@NormalizeAngle(\tkz@FirstAngle,\tkz@SecondAngle)
  \FPadd\tkz@Angle{\my@SecondAngle}{-\my@FirstAngle}
  \global\let\tkzAngleResult\tkz@Angle
\endgroup
}

\def\tkz@LabelAngle[#1](#2,#3,#4)#5{%
\begingroup
  \pgfkeys{tkzlabelangle/.cd,dist=1}
  \pgfqkeys{/tkzlabelangle}{#1}
  \tkzFindSlopeAngle(#3,#2)\tkzGetAngle{tkz@dirOne}
  \tkzFindSlopeAngle(#3,#4)\tkzGetAngle{tkz@dirTwo}
  \my@NormalizeAngle(\tkz@dirOne,\tkz@dirTwo)
  \FPeval\labelAngle{(\my@FirstAngle +\my@SecondAngle)/2}
  \path (#3) --+(\labelAngle:\labeldist) node[/tkzmkangle/.cd,#1] {#5};
\endgroup
}

\def\my@NormalizeAngle(#1,#2){%
\begingroup
  \pgfmathparse{#1}\xdef\my@FirstAngle{\pgfmathresult}%
  \pgfmathparse{#2}\xdef\my@SecondAngle{\pgfmathresult}%
  \pgfmathgreaterthan{\my@FirstAngle}{0}
  \ifdim\pgfmathresult pt=1 pt\relax%
    \pgfmathgreaterthan{\my@FirstAngle}{\my@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
      \pgfmathsubtract{\my@FirstAngle}{360}
      \xdef\my@FirstAngle{\pgfmathresult}%
    \fi
  \else
    \pgfmathgreaterthan{\my@FirstAngle}{\my@SecondAngle}
    \ifdim\pgfmathresult pt=1 pt\relax%
      \pgfmathadd{\my@SecondAngle}{360}
      \xdef\my@SecondAngle{\pgfmathresult}%
    \fi
  \fi
\endgroup
}
\makeatother

enter image description here

  • 1
    Thanks for your remark ! I need to make an update version of tkz-euclide – Alain Matthes Jul 8 '15 at 6:42

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.