# TikZ: How to put in calculated length, if axes where changed (space rectangle)?

I have |AC| = sqrt(a²+b²) for the diagonal of a space rectangle.

\pgfmathsetmacro{\AC}{sqrt(\a^2+\b^2)}


Why does
\draw[red] (A) -- ($(A)!\AC cm!(C)$) node[midway, above, sloped]{too short};
not draw the correct line?

I want to have a coordinate system as shown:

x={(1cm,0cm)},
y={({cos(45)*1cm}, {sin(45)*1cm})},
z={(0cm,1cm)},


\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\pgfmathsetmacro{\a}{4} %
\pgfmathsetmacro{\b}{2} %
\pgfmathsetmacro{\AC}{sqrt(\a^2+\b^2)} %

\begin{tikzpicture}[
font=\footnotesize,
x={(1cm,0cm)},
y={({cos(45)*1cm}, {sin(45)*1cm})},
z={(0cm,1cm)},
]

% Space rectangle
\coordinate[label=below:{$A(0,0,0)$}] (A) at (0,0,0);
\coordinate[label=below:{$B(a,0,0)$}] (B) at (\a,0,0);
\coordinate[label={$C(a,b,0)$}] (C) at (\a,\b,0);
\coordinate[label={$D(0,b,0)$}] (D) at (0,\b,0);
\draw[] (A) -- (B) node[midway, below]{a} -- (C) node[midway, below]{b} -- (D) --cycle;

% DIAGONAL
\draw[red] (A) -- ($(A)!\AC cm!(C)$) node[midway, above, sloped]{too short};

\begin{scope}[-latex, shift={(1.5*\a,-2*\b)}]
\foreach \P/\s/\Pos in {(1,0,0)/x/below, (0,1,0)/y/left, (0,0,1)/z/right}
\draw[] (0,0,0) -- \P node[\Pos, pos=0.9,inner sep=2pt]{$\s$};
\end{scope}

\node[yshift=-1cm, anchor=north west, align=left] at (A) {
a = \a cm \\
b = \b cm \\
AC = $\sqrt{a^2+b^2}$  \\ ~~= \AC cm \\
};

\end{tikzpicture}

\end{document}

• isn't it drawing a line of the stated length, but sqrt(a^2+b^2) isn't the length of the diagonal once you skew the coordinate system – David Carlisle Jul 21 at 10:46
• @DavidCarlisle How can I fix that to work with usual formulas? – cis Jul 21 at 10:49
• Do you really need a formula? Since a and c are known coordinates, draw it directly, \draw (A)--(C). – ferahfeza Jul 21 at 10:57
• @ferahfeza Sure, drawing the line is simple. But this is only a short cutout. I think, I will try to read out the correct length with calc. – cis Jul 21 at 11:05
• Why you use 3-D coordinate system if your shape is only 2_D? For calculation distance between A and C you can perform with macro veclen from TikZ library calc. – Zarko Jul 21 at 17:54

Your |AC| is depending on your coordinate system. The distance |AC| is calculated as follows:

AC= sqrt((a + b * cos(teta))^2 + (b * sin(teta))^2)


Here is the full code for a=5, b=3 and the angle is 60 degrees:

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\pgfmathsetmacro{\a}{5} %
\pgfmathsetmacro{\b}{3} %
\pgfmathsetmacro{\teta}{60}
%\pgfmathsetmacro{\AC}{sqrt((\a+sqrt(\b))^2+sqrt(\b)^2))} %

\begin{tikzpicture}[
font=\footnotesize,
x={(1cm,0cm)},
y={({cos(\teta)*1cm}, {sin(\teta)*1cm})},
z={(0cm,1cm)},
]
\pgfmathsetmacro{\AC}{sqrt((\a+\b*cos(\teta))^2+(\b*sin(\teta))^2)} %
% Space rectangle
\coordinate[label=below:{$A(0,0,0)$}] (A) at (0,0,0);
\coordinate[label=below:{$B(a,0,0)$}] (B) at (\a,0,0);
\coordinate[label={$C(a,b,0)$}] (C) at (\a,\b,0);
\coordinate[label={$D(0,b,0)$}] (D) at (0,\b,0);
\draw[] (A) -- (B) node[midway, below]{a} -- (C) node[midway, below]{b} -- (D) --cycle;

% DIAGONAL
\draw[red] (A) -- ($(A)!\AC cm!(C)$) node[midway, above, sloped]{now exact };

\begin{scope}[-latex, shift={(\a+2,-1*\b)}]
\foreach \P/\s/\Pos in {(1,0,0)/x/below, (0,1,0)/y/left, (0,0,1)/z/right}
\draw[] (0,0,0) -- \P node[\Pos, pos=0.9,inner sep=2pt]{$\s$};
\end{scope}

\node[yshift=-1cm, anchor=north west, align=left] at (A) {
a = \a cm \\
b = \b cm \\
AC = $\sqrt{(a+(b\cos\theta))^2+(b\sin\theta)^2}$  \\
where $\theta$ is angle of $|\mathrm{BC}|$\\
~~= \AC cm \\
};
\end{tikzpicture}

\end{document}