7

MWE:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    % \coordinate (C) at ($(A) + ({-60}:4)$);
    \coordinate (C) at (2,-3);

    \draw (A)--(B)--(C)--cycle;

    % circumcircle
    \tkzCircumCenter(A,B,C)\tkzGetPoint{O}
    % \tkzDrawPoint(O)
    \tkzDrawCircle(O,A)

    % incircle
    \tkzDefCircle[in](A,B,C)\tkzGetPoint{I}\tkzGetLength{rIN}
    \tkzDrawPoint(I)
    \tkzDrawCircle[R](I,\rIN pt)
\end{tikzpicture}  
\end{document}

Gives,

enter image description here

But

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (C) at ($(A) + ({-60}:4)$);
    % \coordinate (C) at (2,-3);

    \draw (A)--(B)--(C)--cycle;

    % circumcircle
    \tkzCircumCenter(A,B,C)\tkzGetPoint{O}
    % \tkzDrawPoint(O)
    \tkzDrawCircle(O,A)

    % incircle
    \tkzDefCircle[in](A,B,C)\tkzGetPoint{I}\tkzGetLength{rIN}
    \tkzDrawPoint(I)
    \tkzDrawCircle[R](I,\rIN pt)
\end{tikzpicture}  
\end{document}

Gives,

enter image description here

Am I missing something, or there is a problem with tikz-euclid doing its calculations with the calculated coordinate? (Why should it?)

ADDENDUM

This purely tikz-euclid way also seems problematic:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
    \tkzDefPoint(0,0){A}
    \tkzDefPoint(4,0){B}
    \tkzDefTriangle[equilateral](B,A)
    \tkzGetPoint{D}
    \tkzDrawPolygon(B,A,D)

    % circumcircle
    \tkzCircumCenter(A,B,D)\tkzGetPoint{O}
    \tkzDrawCircle(O,A)

    % incircle
    \tkzDefCircle[in](A,B,D)\tkzGetPoint{I}\tkzGetLength{rIN}
    \tkzDrawPoint(I)
    \tkzDrawCircle[R](I,\rIN pt)
\end{tikzpicture}
\end{document}

Gives:

enter image description here

blackened
  • 4,181
  • 2
    If you do \coordinate (C) at ($(A) + (2,-3)$);, you are back at the first result. I guess that this simply means that there is a problem with setting C at ({-60}:4), but not with using the calc syntax to set the coordinates. This is confirmed by the fact that just doing \coordinate (C) at ({-60}:4); also leads to the incorrect incircle. That is, there might be an issue in tkz-euclide, but as far as I can see it is not tied to using the calc syntax. –  Nov 17 '18 at 17:40
  • @user21799 It's a bug in the macro that determines the incircle. You can use the new version of tkz-euclide 2.40b altermundus. This is a beta version but I hope to finish it soon. – Alain Matthes Nov 22 '19 at 14:23

2 Answers2

6

IMHO there is a numerical instability in the computations done by the macros of the tkz-euclide package. This has nothing to do with the coordinates set via the calc syntax. To see this, consider

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    % \coordinate (C) at ($(A) + (2,-3)$); %({-60}:4)
    \coordinate (C) at ({-60.01}:4); %({-60}:4)
    % \coordinate (C) at (2,-3);

    \draw (A)--(B)--(C)--cycle;

    % circumcircle
    \tkzCircumCenter(A,B,C)\tkzGetPoint{O}
    % \tkzDrawPoint(O)
    \tkzDrawCircle(O,A)

    % incircle
    \tkzDefCircle[in](A,B,C)\tkzGetPoint{I}\tkzGetLength{rIN}
    \tkzDrawPoint(I)
     \tkzDrawCircle[R](I,\rIN pt)
\end{tikzpicture}  
\end{document}

enter image description here

Everything is cool (except for the bounding box, which could be computed correctly if tkz-euclide were to put overlay when it is computing the auxiliary paths, but this is off-topic here).

However, if I remove the last digit of the angle that defines C, i.e. do

\coordinate (C) at ({-60}:4);

I get what you got

enter image description here

I stress that I have even made an attempt to figure out what's going on internally, simply because I don't speak French and don't understand many things in the manual or code.

Bottom-line: To me it seems that you have discovered an issue in tkz-euclide.

ADDENDUM: Why does that happen here? To understand this, let us recall how incircles are computed. To this end, I follow this nice answer, which uses Heron's formula to compute the radius \inrad of the incircle. This tells us that the incircle is where the lines that are parallel to the edges with distance \incircle intersect. However, for a given edge there are two parallel lines with that distance.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}
% from https://tex.stackexchange.com/a/299451/121799
% calculate semiperimeter
% \pgfmathsetmacro{\semip}{(\sideA+\sideB+\sideC)/2}
% calculate radius of incircle: area (given by Heron's formula) divided by semiperimeter
% \pgfmathsetmacro{\inrad}{sqrt(\semip*(\semip-\sideA)*(\semip-\sideB)*(\semip-\sideC))/\semip}

\begin{tikzpicture}[globalize/.code n args={2}{\xdef#2{#1}}]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    % \coordinate (C) at ($(A) + (2,-3)$); %({-60}:4)
    \coordinate (C) at ({-60.01}:4); %({-60}:4)
    % \coordinate (C) at (2,-3);
    \path let \p1=($(A)-(B)$), \p2=($(B)-(C)$),\p3=($(C)-(A)$),
    \n1={0.5*(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
    \n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1-veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))}
    in  [globalize={\n2}{\inrad}];
    \foreach \X/\Y in {A/B,B/C,C/A}
    {\draw[gray,thin] ($(\X)!\inrad!90:(\Y)$) -- ($(\Y)!\inrad!-90:(\X)$);
    \draw[gray,thin] ($(\X)!\inrad!-90:(\Y)$) -- ($(\Y)!\inrad!90:(\X)$);}
    \draw (A)--(B)--(C)--cycle;

    % circumcircle
    \tkzCircumCenter(A,B,C)\tkzGetPoint{O}
    % \tkzDrawPoint(O)
    \tkzDrawCircle(O,A)

    % incircle
    \tkzDefCircle[in](A,B,C)\tkzGetPoint{I}\tkzGetLength{rIN}
    \tkzDrawPoint(I)
     \tkzDrawCircle[R](I,\rIN pt)
\end{tikzpicture}  
\end{document}

enter image description here

Looking at this picture, I think that tkz-euclide computes the intersection of the wrong (i.e. outer) parallels. This picture also shows that the incircle center is not precisely at the intersection of the parallel lines.

This suggest that, at least as long tkz-euclide requires proficiency in the French language, to compute the incircle radius and center without that package, but following this nice answer.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}[globalize/.code n args={2}{\xdef#2{#1}}]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (C) at ($(A)+({-60}:4)$); 
    \draw (A) -- (B) -- (C) -- cycle;
    %
    \path let \p1=($(A)-(B)$), \p2=($(B)-(C)$),\p3=($(C)-(A)$),
    \n1={0.5*(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
    \n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1-veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))}
    in  [globalize={\n2}{\inrad}];
    \foreach \X/\Y in {A/B,B/C,C/A}
    {\path[name path global=path-\X-\Y-1] ($(\X)!\inrad!90:(\Y)$) -- ($(\Y)!\inrad!-90:(\X)$);
    \path[name path global=path-\X-\Y-2] ($(\X)!\inrad!-90:(\Y)$) -- ($(\Y)!\inrad!90:(\X)$);}
    \foreach \X in {1,2}
    {\foreach \Y in {1,2}
     {\path[name intersections={of={path-A-B-\X} and {path-B-C-\Y},total=\t}]
     \ifnum\t=1 
     (intersection-1) coordinate (incenter)
     \else
     \fi
     ;}
    }
    \draw (incenter) circle (\inrad);
\end{tikzpicture}
\end{document}

enter image description here

One can even go a step further and get rid of the intersections library, and make things more TikZy by using elementary trigonometry the determination of the center based on Cartesian coordinates.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[incircle/.style n args={3}{%
insert path={
let \p1=($(#2)-(#1)$), \p2=($(#3)-(#1)$),\p3=($(#2)-(#3)$),
    \n1={0.5*(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
    \n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1-veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))},
    \n3={veclen(\x1,\y1)}, % length #1 -- #2
    \n4={veclen(\x2,\y2)}, % length #1 -- #3
    \n5={veclen(\x3,\y3)}, % length #2 -- #3
    \n6={\n3+\n4+\n5}
    in % \pgfextra{\typeout{\n1,\n2,\n3,\n4,\n5,\n6}}
    (${(\n5/\n6)}*(#1)+{(\n4/\n6)}*(#2)+{(\n3/\n6)}*(#3)$) circle (\n2)
}}]
    \coordinate (A) at (0,0);
    \coordinate (B) at (4,0);
    \coordinate (C) at ($(A)+({-60}:4)$); 
    \draw (A) -- (B) -- (C) -- cycle;
    %
    \draw[incircle={A}{B}{C}];
\end{tikzpicture}
\end{document}

This leads the same output as above. (When writing this, I found it is not at all trivial to grasp all cases, and there is no guarantee that I did. That is, I am really deeply impressed by Alain Matthes' achievements. In other words, even though I do think that there might be an issue with tkz-euclide, one should also highlight that it overall works great.)

  • 2
    +1, I also think there are bugs in tkz-euclide. This package was very useful for me to start with Tikz since the documentation is in French and I almost exclusively build elementary geometric figures in my work. Starting with LaTeX, I thought I was doing the wrong thing at first. But over time and with the problems, I turned to Tikz which is more complex in approach, but with fewer bugs. – AndréC Nov 17 '18 at 20:23
  • @AndréC I guess it is very hard to come up with a bug-free package, but as you say at a given point it is probably a good idea to switch to calc, also because in the end it is IMHO more powerful even though it does not seem so at first sight simply because there are less examples in the manual. –  Nov 17 '18 at 20:26
  • I find TikZ very difficult to learn because the manual is very long and has a strange structure. In France, the manuals are exhaustive and methodically constructed. So when you talk about a subject, you explore it in depth. With TikZ, the documentation is scattered throughout the pages. For example, the notion of bounding box explained in several page 1032; 1033; 766; 173; 175; 562; but none of these pages really explain what it is or how it is calculated! You have to read page 969 to understand it. The TikZ manual is a real maquis, the tkz-euclide manual is much better built. – AndréC Nov 17 '18 at 20:47
  • @AndréC Except that those who do not speak French are excluded from that. (And there are different opinions on how one may explain things in the most efficient way. The pgfmanual has a very basic tutorial and then it explains everything in greater depth. I guess the main problem is that most users think they can skip the intro, and then get lost. I am one of those who has tried that. ;-) –  Nov 17 '18 at 20:51
  • We're not skipping the introduction by chance, but because of lack of time! – AndréC Nov 17 '18 at 20:57
  • @AndréC Yes, it is well known that just 5 hours of trial and error can spare you 5 minutes of reading the manual. ;-) –  Nov 17 '18 at 21:08
  • Reading the introduction takes 5 hours for someone who speaks good English. As for myself, it takes me a lot of time because at the same time I have to learn the specific vocabulary of LaTeX, TikZ and understand what is being said. Thus, I spend my time using automatic translators who clear the translation (but with many errors). For you 5 hours. For me: 5 weeks minimum! :( – AndréC Nov 17 '18 at 21:13
  • @AndréC The above was a joke. I also did not read the manual. And I learn most from the older answers of this site (but try of course to "cite" them when I use them). –  Nov 17 '18 at 21:18
  • @AndréC It would be nice if you were not overly critical to the pgfmanual without specifying a clear reason. Imagine Till Tantau had decided to write it in German, how would you feel about it? I cannot understand your comments on the bounding box either, IMHO it is not true that the information is scattered over many places, rather the bbox has relations to many other things which get explained whenever the other things are discussed. From my perspective you should only complain when you created a better manual. ;-) –  Nov 18 '18 at 16:18
  • I'm not complaining, I see. And the reason is clear: the information is scattered and scattered throughout the manual. Whether it is the notion of bounding box, path, node, decoration, we read a little bit here, a complement there, another elsewhere. The manual is designed to be read like a novel. This does not take anything away from TikZ's wonderful quality, it complicates his learning. And it also complicates the maintenance of the manual for those who wrote it. – AndréC Nov 18 '18 at 16:34
  • @marmot How to draw a square with four vertices lies on the circle? – minhthien_2016 Dec 05 '18 at 12:02
  • @minhthien_2016 Please ask a separate question on this. –  Dec 05 '18 at 13:16
  • @marmot My question is, with your code (contain a triangle and a circle) how to draw a circle with that code? – minhthien_2016 Dec 05 '18 at 13:31
  • @minhthien_2016 Please ask a real question instead adding a comment. By real question I mean press the "ask question" button. This question will be seen by all users, and you can get a real answer. Asking questions is free. –  Dec 05 '18 at 13:35
  • In my confident opinion, pgfmanual is the best documentation in many senses. If Pgfmanual is an occean, tkz-euclide is a small pond with some bugs. I don’t mean to be rude (tkz-euclide is helpful for beginers who don’t need to draw figures outside Euclidean geometry) – Black Mild Jul 30 '19 at 10:54
  • Yes there are some bugs in tkz-euclide. I hope the new version will be fine but it is very hard to come up with a bug-free package. I give an answer about the problem and about numerical instability in the computations done by the macros of the tkz-euclide package but there are also numerical instability with pgf. The only correct way is to use lua. – Alain Matthes Nov 22 '19 at 14:33
  • @AlainMatthes you could also (for non luatex use) look at using the l3fp code which implements iso double precision decimal arithmetic see the l3fp chapter in texdoc interface3 , unrelated, the code in the archive you link to has reverted the change that removed the etex package? – David Carlisle Nov 25 '19 at 13:51
  • @DavidCarlisle I use xfp in my last version to redefine veclen. I removed the êtes package. – Alain Matthes Dec 02 '19 at 18:57
  • @AlainMatthes sounds good:-) – David Carlisle Dec 02 '19 at 19:00
4

You found a bug ... The macro \tkzDefCircle[in] is incorrect. THe problem disappears with the new version of tkz-base and tkz-euclide.

Now the correct code with tkz-euclide is

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}

\begin{document} \begin{tikzpicture} \tkzDefPoint(0,0){A} \tkzDefPoint(4,0){B} \tkzDefTriangleequilateral \tkzGetPoint{D} \tkzDrawPolygon(B,A,D)

% circumcircle
\tkzCircumCenter(A,B,D)\tkzGetPoint{O}
\tkzDrawCircle(O,A)

% incircle
\tkzDefCircle[in](A,B,D)\tkzGetPoint{I}\tkzGetLength{rIN}
\tkzDrawPoint(I)
\tkzDrawCircle[R](I,\rIN pt)

\end{tikzpicture} \end{document}

Now you can avoid \usetkzobj{all}. The main modules are charged only very specific modules can be called with this macro.

Now how, why, where it's necessary or not to use calc , tikz etc.

It's preferable to use \tkzDefPoint instead of \coordinate when you use tkz-euclide because if you need some computations to get a point, tkz-euclide uses the package fp. Tikz and TeX are very fine tools but they are limited to calculate and quickly errors occur if one connects calculations. For example, you can look in the manual how to draw the heights of a triangle. The intersection of heights is defined very badly.

The best way to do calculations is to use lua. Perhaps the next version of euclide will be in two parts. The first one will use lua to define the points and then the second part will use tikz to draw the different objects.

Now you can use the calc syntax to get the coordinates but you have other ways

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tkz-euclide}
\begin{document}

\begin{tikzpicture} \tkzDefPoint(0,0){A} \tkzDefPoint(4,0){B} \coordinate (C) at ($(A) + ({60}:4)$); % Better is \tkzDefTriangleequilateral % or \tkzDefShiftPointA{C} % possible % \begin{scope}[shift={(A)}] % \tkzDefPoint(-60:4){C} % \end{scope} \tkzDrawPolygon(B,A,C)

% circumcircle
\tkzCircumCenter(A,B,C)\tkzGetPoint{O}
\tkzDrawPoint(O)
\tkzDrawCircle(O,A)

% incircle
 \tkzDefCircle[in](A,B,C)\tkzGetPoint{I}\tkzGetLength{rIN}
 \tkzDrawCircle[R,color=red](I,\rIN pt)

\end{tikzpicture}
\end{document}

Alain Matthes
  • 95,075