0

Lets say I want to draw a line on a grid given two points A,B:

\documentclass[tikz]{standalone}
%mixing defs and coordinates is a bad idea  
\usetikzlibrary{fit,shapes,calc,matrix,math,positioning,arrows.meta}
\begin{document}
\begin{tikzpicture}
\def\xmax{7}
\draw [help lines] (0,0) grid (\xmax,\xmax);
\tikzmath{
coordinate \A, \B; 
\A =(1,2);
\B =(4,4);
}
\draw[fill=black] (\A) circle [radius=1pt] node[above,xshift=-1pt] {$A$};
\draw[fill=black] (\B) circle [radius=1pt] node[above,xshift=-1pt] {$B$};
\pgfmathsetmacro{\gradient}{(\By-\Ay)/(\Bx-\Ax)};
\draw (0,\Ay-\gradient*\Ax) -- (\xmax,\Ay-\gradient*\Ax+\gradient*\xmax);
\draw[fill=green] (0,\Ay-\gradient*\Ax) circle [radius=1pt] ;
\draw[fill=red] (\xmax,\Ay-\gradient*\Ax+\gradient*\xmax) circle [radius=1pt] ;
\end{tikzpicture}
\end{document}

This generates,

![enter image description here

which produces a correct left boundary (green dot) but an incorrect right boundary (red dot). It turns out that \Ax and \Ay are not equal to 1cm and 2cm but instead are expressed in pts and are equal to 28.45274 and 56.90549. Because \gradient is calculated using these units and the expression for the green dot also uses these units I get the correct result. This is not the case for the red dot since \xmax is definitely not in pts. The result is a y coordinate equal to 42.60365 pt or 1.497348 cm.

Is there a way to avoid this unit mess by forcing the coordinates defined in tikzmath to use the default unit of cm?

Ted Black
  • 453
  • 2
  • 8
  • 3
    Basically the same problem as Q687376. No, once input, coordinates are always in the canvas coordinate system. The xyz coordinate system is only used for a one-way transformation into the canvas coordinate system. The values are then lost. (There might be TikZ addons or a future update of TikZ that changes this. I believe one 3d TikZ expansion might do this internally.) What are you trying to do? There are easier ways to find points on a line through two points or parallel to it with TikZ that don't need you do to math. – Qrrbrbirlbel Nov 20 '23 at 17:58
  • 1
    Of course, you can always save \Ax to be 1, \Ay to be 2 and then use (\Ax,\Ay) instead of \A (or even define \A to be \Ax, \Ay) which would allow you to use the \Ax and \Ay macros as you wish. – Qrrbrbirlbel Nov 20 '23 at 18:00
  • 1
    The green dot can be found by (intersection of 0,0--0,1 and A--B) which the red one can be found by (intersection of 5,0--5,1 and A--B) (where x,0--x,1 just describes a vertical line with x value x), this basically does the same math for you. – Qrrbrbirlbel Nov 20 '23 at 18:06
  • 1
    In this case, you could simply replace \Ay-\gradient*\Ax+\gradient*\xmax by \Ay-\gradient*\Ax+\gradient*\xmax*1cm and this way clarify that you mean 7cm and not 7pt. – Jasper Habicht Nov 20 '23 at 18:11
  • Thanks @Qrrbrbirlbel I was aware of the intersection method. Its good that I have confirmed that it is the default behavior and that there is no way to change the default units of the canvas coordinate system. – Ted Black Nov 20 '23 at 18:40

0 Answers0