TikZ:如果轴发生变化(空间矩形),如何输入计算的长度?

TikZ:如果轴发生变化(空间矩形),如何输入计算的长度?

我有 |AC| = sqrt(a²+b²) 作为空间矩形的对角线。

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

为什么
\draw[red] (A) -- ($(A)!\AC cm!(C)$) node[midway, above, sloped]{too short};
画不出正确的线?

我想要一个如图所示的坐标系:

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}

答案1

您的|AC|距离取决于您的坐标系。距离|AC|计算如下:

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

以下是 a=5、b=3 且角度为 60 度的完整代码:

\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}

在此处输入图片描述

相关内容