如何使用 LaTeX/Tikz 计算半径

问题描述 投票:0回答:1

我想在 tikz 中定义一个包含在矩形中的椭圆。

棘手的部分是我希望我的绘图自动从矩形的左上角和右下角坐标的变量中绘制。

我可以从矩形坐标计算椭圆的中心,但无法自动计算椭圆命令所需的预期半长轴和半短轴。

基本上,我想计算 (\xb-\xa)/2 和 (\yb-\ya)/2

你有什么想法吗?

非常感谢:)

BR

到目前为止我做了什么:

\begin{tikzpicture}

% Top-left point coordinates
\newcommand\xa{0.75}
\newcommand\ya{2.25}

% Bottom-right point coordinates
\newcommand\xb{3.25}
\newcommand\yb{0.75}


\coordinate (A) at (\xa, \ya);
\coordinate (B) at (\xb, \yb);

\coordinate (C) at ($ 0.5*(A)+0.5*(B) $); % Center of the ellipse

\draw[thick, dashed] (A) rectangle (B);

%\draw[ultra thick] (C) ellipse ( ??? and ??? );
\end{tikzpicture}
latex tikz
1个回答
0
投票

使用矩形的边计算半轴长度。

正如您已经注意到的,半轴的长度对应于矩形的二等分边。在下面您可以找到解决问题的代码。

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

% Top-left point coordinates
\newcommand\xa{0.75}
\newcommand\ya{2.25}

% Bottom-right point coordinates
\newcommand\xb{3.25}
\newcommand\yb{0.75}

% Compute the semiaxes
\def\a{abs(\xa-\xb)/2}
\def\b{abs(\ya-\yb)/2}

\coordinate (A) at (\xa, \ya);
\coordinate (B) at (\xb, \yb);

\coordinate (C) at ($ 0.5*(A)+0.5*(B) $); % Center of the ellipse

\draw[thick, dashed] (A) rectangle (B);

% Beware to put the length in curly brackets
\draw[ultra thick] (C) ellipse ( {\a} and {\b} );
\end{tikzpicture}
\end{document}
© www.soinside.com 2019 - 2024. All rights reserved.