Graphics, Figures & Tablesdrawing the incircle, problems with units

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

drawing the incircle, problems with units

Post by kent »

I am using the PGF mathematical engine to calculate the incenter I for a given triangle ABC with given corner coordinates A, B and C.
The corners are defined by \coordinate (A) at (0,0) etc..
I have obtained the x, y coordinates for A, B and C as \xA, \yA etc. (by say \pgfgetlastxy) and the side lengths of the triangle as \la etc..
All this given in pt. I have also obtained the angles of the triangle such as \aA etc.
All these data have been checked to be correct for the triangle I am using.
Continuing with

Code: Select all

\pgfmathsetmacro{\S}{\la+\lb+\lc};
\pgfmathsetmacro{\xI}{(\la*\xA+\lb*\xB+\lc*\xC)/\S};
\pgfmathsetmacro{\yI}{(\la*\yA+\lb*\yB+\lc*\yC)/\S};
\pgfmathsetmacro{\r}{\lb*\lc*sin(\aA)/\S};
To draw the incircle I need \xI, \r (radius of incircle) etc. in cm, so

Code: Select all

\pgfmathsetmacro{\cmpt}{28.45274};%convert from cm to pt; 1 cm is 28.45274 pt
\pgfmathsetmacro{\ptcm}{1/\cmpt};%convert from pt to cm
% PGF uses pt, we need cm
\pgfmathsetmacro{\xI}{\xI*\ptcm};
\pgfmathsetmacro{\yI}{\yI*\ptcm};
\pgfmathsetmacro{\r}{\r*\ptcm};
I then thought that \draw[] (\xI,\yI) circle (\r); would do the trick.
The problem is that the drawn circle has to scaled up by a factor 10 to get the correct incircle.
I don't know why.
Anybody who can explain what's wrong?
Kent
Last edited by Stefan Kottwitz on Wed Nov 02, 2016 3:12 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Re: drawing the incircle, problems with units

Post by Stefan Kottwitz »

Hi Kent,

it's a bit theoretical... do you have some real code (compilable) to post for us to work that?

Stefan
LaTeX.org admin
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

Re: drawing the incircle, problems with units

Post by kent »

The code take some space. Can I add an attachment (file) to emails sent from here?
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

drawing the incircle, problems with units

Post by kent »

Here is the code:

Code: Select all

\documentclass[10pt,titlepage,leqno]{article}
\usepackage{tikz}
\usetikzlibrary{calc,through,backgrounds,patterns}
% \deflen macro is used by the macro \mgtrianglesidelength below
\newcommand{\deflen}[2]{%
\expandafter\newlength\csname #1\endcsname
\expandafter\setlength\csname #1\endcsname{#2}%
}
% \getcoords macro get coordinates x and y from point A = (x,y) defined by \coordinate (A) at (x,y);
% the code for \getcoords was communicated to me through the latex forum online
% (credit to Stefan Kottwitz/Andrew Stacey here)
% need this since I am running PGF 2.0
\makeatletter
\newcommand{\getcoords}[3]{%
\tikz@scan@one@point\pgfutil@firstofone(#1)\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
% \mgtrianglesidelength macro calculates the lengths of the sides of a given triangle ABC
% from the coordinates of its corners 
% I don't have tikz-2d
%---------------------------------------------------------------------------------------
\def\mgtrianglesidelength[#1,#2,#3]{
\getcoords{#1}{\xf}{\yf};
\getcoords{#2}{\xs}{\ys};
\pgfmathparse{veclen(\xs-\xf,\ys-\yf)};
\deflen{#3}{\pgfmathresult pt}
}
%---------------------------------------------------------------------------------------
% PURPOSE OF THE FOLLOWING:
% GIVEN THE TRIANGLE ABC, FROM ITS CORNES DEFINED BY THEIR COORDINATES, DRAW THE
% INCIRCLE OF ABC
%---------------------------------------------------------------------------------------
\begin{document}
\newcommand{\scalefactor}{2.0}
\begin{figure} [htbp]
\centering
\begin{tikzpicture}[scale=\scalefactor]
% define the triangle to work with
% easy example, but the set up should work for a general non-degenerate triangle
\coordinate [label=below:$\bf A$] (A) at (0,0);
\coordinate [label=below:$\bf B$] (B) at (2,0);
\coordinate [label=above:$\bf C$] (C) at (2,3);
\fill[yellow] (A)--(B)--(C)--cycle;
\draw[red,ultra thick] (A)--(B)--(C)--cycle;
% calculate the side lengths of the given triangle ABC
\mgtrianglesidelength[B,C,la];
\mgtrianglesidelength[A,C,lb];
\mgtrianglesidelength[A,B,lc];
% the following debug output does not work, but the side lengths have been checked to be correct
%\path node at (1,1) {\la,\lb,\lc};%does not work properly!!?? ANY COMMENT WHY THE DEBUG GENERATES AN ERROR?
% finally, generate and draw the inscribed circle of the triangle ABC
% the coordinates of the incenter I = (xI,yI) and radius r of the inscribed circle are
% (1.30278,0.697224) and 0.697224 (in cm), resp., as calculated explicitly by Mathematica
% due to the special triangle ABC chosen (AB horizontal), we have r = yI
% calculate I = (xI,yI) and r, using PGF
\pgfmathsetmacro{\aA}{acos((\la/100*\la/100-\lb/100*\lb/100-\lc/100*\lc/100)/(-2*\lb/100*\lc/100))};
\pgfmathsetmacro{\aA}{180*(\aA<0)+\aA};%mod(\aA,180) etc. does not work if the angle is obstuse
% calculate I = (\xI,\yI) and r
\getcoords{A}{\xA}{\yA};
\getcoords{B}{\xB}{\yB};
\getcoords{C}{\xC}{\yC};
\pgfmathsetmacro{\S}{\la+\lb+\lc};
\pgfmathsetmacro{\xI}{(\la*\xA+\lb*\xB+\lc*\xC)/\S};
\pgfmathsetmacro{\yI}{(\la*\yA+\lb*\yB+\lc*\yC)/\S};
\pgfmathsetmacro{\r}{\lb*\lc*sin(\aA)/\S};
%-----------------------------------------------------------------------------------------
% draw circle(s)
%-----------------------------------------------------------------------------------------
% correct inscribed circle defined by I and r from mathematica above
\draw[red,very thick] (1.30278,0.697224) circle (0.697224);
%-----------------------------------------------------------------------------------------
% inscribed circle as defined by pgf/tikz above
%-----------------------------------------------------------------------------------------
\draw[blue,thick] (\xI,\yI) circle (\r); %NOT CORRECT!
\pgfmathsetmacro{\cmpt}{28.45274};%convert from cm to pt; 1 cm is 28.45274 pt
\pgfmathsetmacro{\ptcm}{1/\cmpt};%convert from pt to cm
\pgfmathsetmacro{\xI}{\xI*\ptcm};
\pgfmathsetmacro{\yI}{\yI*\ptcm};
\pgfmathsetmacro{\r}{\r*\ptcm};
\draw[blue,thick] (\xI,\yI) circle (\r); %NOT CORRECT EITHER!
\pgfmathsetmacro{\xI}{\xI*10};
\pgfmathsetmacro{\yI}{\yI*10};
\pgfmathsetmacro{\r}{\r*10};
\draw[blue,very thick,dashed] (\xI,\yI) circle (\r); %CORRECT!? What's going on?
%----------------------------------------------------------------------------------
\end{tikzpicture}
\end{figure} 
\end{document}
Last edited by Stefan Kottwitz on Thu Nov 03, 2016 1:58 pm, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

drawing the incircle, problems with units

Post by Stefan Kottwitz »

Well, it is correct - it is fine when I test it. This code:

Code: Select all

\documentclass[10pt,titlepage,leqno]{article}
\usepackage{tikz}
\usetikzlibrary{calc,through,backgrounds,patterns}
% \deflen macro is used by the macro \mgtrianglesidelength below
\newcommand{\deflen}[2]{%
\expandafter\newlength\csname #1\endcsname
\expandafter\setlength\csname #1\endcsname{#2}%
}
% \getcoords macro get coordinates x and y from point A = (x,y) defined by \coordinate (A) at (x,y);
% the code for \getcoords was communicated to me through the latex forum online
% (credit to Stefan Kottwitz/Andrew Stacey here)
% need this since I am running PGF 2.0
\makeatletter
\newcommand{\getcoords}[3]{%
\tikz@scan@one@point\pgfutil@firstofone(#1)\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
% \mgtrianglesidelength macro calculates the lengths of the sides of a given triangle ABC
% from the coordinates of its corners 
% I don't have tikz-2d
%---------------------------------------------------------------------------------------
\def\mgtrianglesidelength[#1,#2,#3]{
\getcoords{#1}{\xf}{\yf};
\getcoords{#2}{\xs}{\ys};
\pgfmathparse{veclen(\xs-\xf,\ys-\yf)};
\deflen{#3}{\pgfmathresult pt}
}
%---------------------------------------------------------------------------------------
% PURPOSE OF THE FOLLOWING:
% GIVEN THE TRIANGLE ABC, FROM ITS CORNES DEFINED BY THEIR COORDINATES, DRAW THE
% INCIRCLE OF ABC
%---------------------------------------------------------------------------------------
\begin{document}
\newcommand{\scalefactor}{2.0}
\begin{figure} [htbp]
\centering
\begin{tikzpicture}[scale=\scalefactor]
% define the triangle to work with
% easy example, but the set up should work for a general non-degenerate triangle
\coordinate [label=below:$\bf A$] (A) at (0,0);
\coordinate [label=below:$\bf B$] (B) at (2,0);
\coordinate [label=above:$\bf C$] (C) at (2,3);
\fill[yellow] (A)--(B)--(C)--cycle;
\draw[red,ultra thick] (A)--(B)--(C)--cycle;
% calculate the side lengths of the given triangle ABC
\mgtrianglesidelength[B,C,la];
\mgtrianglesidelength[A,C,lb];
\mgtrianglesidelength[A,B,lc];
% the following debug output does not work, but the side lengths have been checked to be correct
%\path node at (1,1) {\la,\lb,\lc};%does not work properly!!?? ANY COMMENT WHY THE DEBUG GENERATES AN ERROR?
% finally, generate and draw the inscribed circle of the triangle ABC
% the coordinates of the incenter I = (xI,yI) and radius r of the inscribed circle are
% (1.30278,0.697224) and 0.697224 (in cm), resp., as calculated explicitly by Mathematica
% due to the special triangle ABC chosen (AB horizontal), we have r = yI
% calculate I = (xI,yI) and r, using PGF
\pgfmathsetmacro{\aA}{acos((\la/100*\la/100-\lb/100*\lb/100-\lc/100*\lc/100)/(-2*\lb/100*\lc/100))};
\pgfmathsetmacro{\aA}{180*(\aA<0)+\aA};%mod(\aA,180) etc. does not work if the angle is obstuse
% calculate I = (\xI,\yI) and r
\getcoords{A}{\xA}{\yA};
\getcoords{B}{\xB}{\yB};
\getcoords{C}{\xC}{\yC};
\pgfmathsetmacro{\S}{\la+\lb+\lc};
\pgfmathsetmacro{\xI}{(\la*\xA+\lb*\xB+\lc*\xC)/\S};
\pgfmathsetmacro{\yI}{(\la*\yA+\lb*\yB+\lc*\yC)/\S};
\pgfmathsetmacro{\r}{\lb*\lc*sin(\aA)/\S};
%-----------------------------------------------------------------------------------------
% draw circle(s)
%-----------------------------------------------------------------------------------------
% correct inscribed circle defined by I and r from mathematica above
\draw[red,very thick] (1.30278,0.697224) circle (0.697224);
%-----------------------------------------------------------------------------------------
% inscribed circle as defined by pgf/tikz above
%-----------------------------------------------------------------------------------------
\pgfmathsetmacro{\cmpt}{28.45274};%convert from cm to pt; 1 cm is 28.45274 pt
\pgfmathsetmacro{\ptcm}{1/\cmpt};%convert from pt to cm
\pgfmathsetmacro{\xI}{\xI*\ptcm};
\pgfmathsetmacro{\yI}{\yI*\ptcm};
\pgfmathsetmacro{\r}{\r*\ptcm};
\draw[blue,thick] (\xI,\yI) circle (\r); %NOT CORRECT EITHER!
\pgfmathsetmacro{\xI}{\xI*10};
\pgfmathsetmacro{\yI}{\yI*10};
\pgfmathsetmacro{\r}{\r*10};
%\draw[blue,very thick,dashed] (\xI,\yI) circle (\r); %CORRECT!? What's going on?
%----------------------------------------------------------------------------------
\end{tikzpicture}
\end{figure} 
\end{document}
Gives me the incircle of the triangle. Do you have an older version of TikZ, older than 3.0? Perhaps there was a bug.

Stefan
LaTeX.org admin
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

drawing the incircle, problems with units

Post by rais »

I'd suspect some version trouble, too

Code: Select all

\listfiles
\documentclass[10pt,titlepage,leqno]{article}
\usepackage{tikz}
\usepackage{bm}% (rais) bold math
\usetikzlibrary{calc,through,backgrounds,patterns}
\newif\ifmydebug % (rais) a switch for debug info
\mydebugtrue% (rais) set to false to suppress debug info in the output
\newcommand*\dbginf[1]{% (rais) just a small macro to output debug info...or suppress it
  \ifmydebug
    (#1)%
  \fi
}
% \deflen macro is used by the macro \mgtrianglesidelength below
\newcommand{\deflen}[2]{%
  \expandafter\newlength\csname #1\endcsname
  \expandafter\setlength\csname #1\endcsname{#2}%
}
% \getcoords macro get coordinates x and y from point A = (x,y) defined by \coordinate (A) at (x,y);
% the code for \getcoords was communicated to me through the latex forum online
% (credit to Stefan Kottwitz/Andrew Stacey here)
% need this since I am running PGF 2.0
\makeatletter
\newcommand{\getcoords}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone(#1)\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother
% \mgtrianglesidelength macro calculates the lengths of the sides of a given triangle ABC
% from the coordinates of its corners 
% I don't have tikz-2d
%---------------------------------------------------------------------------------------
\def\mgtrianglesidelength[#1,#2,#3]{%
  \getcoords{#1}{\xf}{\yf};
  \getcoords{#2}{\xs}{\ys};
  \pgfmathparse{veclen(\xs-\xf,\ys-\yf)};
  \deflen{#3}{\pgfmathresult pt}
}
%---------------------------------------------------------------------------------------
% PURPOSE OF THE FOLLOWING:
% GIVEN THE TRIANGLE ABC, FROM ITS CORNES DEFINED BY THEIR COORDINATES, DRAW THE
% INCIRCLE OF ABC
%---------------------------------------------------------------------------------------
\begin{document}
\newcommand{\scalefactor}{2.0}
\begin{figure} [htbp]
  \centering
  \begin{tikzpicture}[scale=\scalefactor]
% define the triangle to work with
% easy example, but the set up should work for a general non-degenerate triangle
    \coordinate [label=below:$\bm{A}$] (A) at (0,0);% (rais) \bf is considered bad practice for about twenty years, now
    \coordinate [label=below:$\bm{B}$] (B) at (2,0);
    \coordinate [label=above:$\bm{C}$] (C) at (2,3);
    \fill[yellow] (A)--(B)--(C)--cycle;
%        \draw[red,ultra thick] (A)--(B)--(C)--cycle;
% calculate the side lengths of the given triangle ABC
    \mgtrianglesidelength[B,C,la];
    \mgtrianglesidelength[A,C,lb];
    \mgtrianglesidelength[A,B,lc];
% the following debug output does not work, but the side lengths have been checked to be correct
    \draw[red,ultra thick]
      (A)--(B) node[below, midway, sloped, black]{$c$ \dbginf{\the\lc}}
      --(C) node[below, midway, sloped, black]{$a$ \dbginf{\the\la}}
      --cycle node[above, midway, sloped, black]{$b$ \dbginf{\the\lb}};
%        \path node at (1,1) {\la,\lb,\lc};%does not work properly!!?? ANY COMMENT WHY THE DEBUG GENERATES AN ERROR?
% (rais) within \mgtrianglesidelength, you're using \deflen for defining the third parameter (\la, \lb, and \lc in the three calls above), which defines them as length registers. Use \the if you want to know what's in a length register (see my \dbginf above)
%
% finally, generate and draw the inscribed circle of the triangle ABC
% the coordinates of the incenter I = (xI,yI) and radius r of the inscribed circle are
% (1.30278,0.697224) and 0.697224 (in cm), resp., as calculated explicitly by Mathematica
% due to the special triangle ABC chosen (AB horizontal), we have r = yI
% calculate I = (xI,yI) and r, using PGF
    \pgfmathsetmacro{\aA}{acos((\la/100*\la/100-\lb/100*\lb/100-\lc/100*\lc/100)/(-2*\lb/100*\lc/100))};
    \pgfmathsetmacro{\aA}{180*(\aA<0)+\aA};%mod(\aA,180) etc. does not work if the angle is obstuse
% calculate I = (\xI,\yI) and r
    \getcoords{A}{\xA}{\yA};
    \getcoords{B}{\xB}{\yB};
    \getcoords{C}{\xC}{\yC};
    \pgfmathsetmacro{\S}{\la+\lb+\lc};
    \pgfmathsetmacro{\xI}{(\la*\xA+\lb*\xB+\lc*\xC)/\S};
    \pgfmathsetmacro{\yI}{(\la*\yA+\lb*\yB+\lc*\yC)/\S};
    \pgfmathsetmacro{\r}{\lb*\lc*sin(\aA)/\S};
%-----------------------------------------------------------------------------------------
% draw circle(s)
%-----------------------------------------------------------------------------------------
% correct inscribed circle defined by I and r from mathematica above
    \draw[red,very thick] (1.30278,0.697224) circle (0.697224);
%-----------------------------------------------------------------------------------------
% inscribed circle as defined by pgf/tikz above
%-----------------------------------------------------------------------------------------
%    \draw[blue,thick] (\xI,\yI) circle (\r); %NOT CORRECT!
% (rais) yes, well, \xI, \yI, and \r appear to be based on pt, but unitless; \draw (a.s.o.), however, use cm by default, their input is usually unitless, as well...but you can give them a unit:
    \draw[blue,thick] (\xI pt,\yI pt) circle (\r pt);% (rais) note the "pt"
% (rais) for me, the following isn't necessary
    \pgfmathsetmacro{\cmpt}{72.27/2.54};%convert from cm to pt; (rais) 2.54 cm equal 72.27 pt
    \pgfmathsetmacro{\ptcm}{1/\cmpt};%convert from pt to cm
    \pgfmathsetmacro{\xI}{\xI*\ptcm};
    \pgfmathsetmacro{\yI}{\yI*\ptcm};
    \pgfmathsetmacro{\r}{\r*\ptcm};
    \draw[cyan,thin] (\xI,\yI) circle (\r); %(rais) works for me
% (rais) skipped the multiplication by 10 because I really don't see a need for it
%----------------------------------------------------------------------------------
  \end{tikzpicture}
\end{figure} 
\end{document}
creates three incircles with the same dimensions (and points of origin, just different thicknesses/colors) for me.
My file list (compare with your log file, it's close to the end, if \listfiles was given)

Code: Select all

*File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
   leqno.clo    2015/03/31 v1.1i Standard LaTeX option (left equation numbers)
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
    tikz.sty    2015/08/07 v3.0.1a (rcs-revision 1.151)
     pgf.sty    2015/08/07 v3.0.1a (rcs-revision 1.15)
  pgfrcs.sty    2015/08/07 v3.0.1a (rcs-revision 1.31)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.1a (rcs-revision 1.7)
graphicx.sty    2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2016/07/10 v1.0t Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  pdftex.def    2016/07/10 v0.06j Graphics/color for pdfTeX
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2014/07/09 v3.0.1a (rcs-revision 1.48)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.1a (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.1a (rcs-revision 1.1)
  pgffor.sty    2013/12/13 v3.0.1a (rcs-revision 1.25)
 pgfkeys.sty    
 pgfkeys.code.tex
 pgfmath.sty    
 pgfmath.code.tex
  pgffor.code.tex
    tikz.code.tex
      bm.sty    2016/07/07 v1.2b Bold Symbol Support (DPC/FMi)
supp-pdf.mkii
pdftexcmds.sty    2016/05/21 v0.22 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
   ifpdf.sty    2016/05/14 v3.1 Provides the ifpdf switch
epstopdf-base.sty    2016/05/15 v2.6 Base part for package epstopdf
  grfext.sty    2016/05/16 v1.2 Manage graphics extensions (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
kvoptions.sty    2016/05/16 v3.12 Key value format for package options (HO)
kvsetkeys.sty    2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty    2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********
KR
Rainer
kent
Posts: 57
Joined: Thu Oct 20, 2016 3:41 pm

Re: drawing the incircle, problems with units

Post by kent »

Thanks for help!
I managed finally to get a code working properly without the explicit need to convert units.
My next project will be to upgrade my versions of PGF and TikZ. Great programs!
Kent
Post Reply