以下代码使用 tikz 绘制一个以给定中心 X 和圆周上的点 Z 为圆心的圆。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{through}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left:$X$] (X) at (0,0);
\coordinate [label=left:$Y$] (Y) at (1,0);
\coordinate [label=left:$Z$] (Z) at (2,3);
\node[draw,circle through=(Z)] at (X) {};
\end{tikzpicture}
\end{document}
当焦点为 X 和 Y 且圆周上有一个点 Z 时,如何使用 tikz (或 tkz-euclide) 绘制椭圆?换句话说,如何绘制一个焦点为 X 和 Y 且通过点 Z 的椭圆?
答案1
新答案
原始帖子从未提及任何有关缩放的内容。如果您不介意加载xintexpr
(提供高精度计算),那么这里有一个替代方案可以满足您的额外缩放要求。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xintexpr}
\begin{document}
\begin{tikzpicture}
\def\Xx{0}\def\Xy{0}
\def\Yx{1}\def\Yy{0}
\def\Zx{2}\def\Zy{3}
\coordinate [label=left:$X$] (X) at (\Xx,\Xy);
\coordinate [label=left:$Y$] (Y) at (\Yx,\Yy);
\coordinate [label=left:$Z$] (Z) at (\Zx,\Zy);
\edef\fixedtotallength{%
\xintthefloatexpr\xintexpr
sqrt((\Zx-\Xx)^2+(\Zy-\Xy)^2)+sqrt((\Zx-\Yx)^2+(\Zy-\Yy)^2)
\relax\relax
}
\edef\majoraxisradius{%
\xintthefloatexpr\xintexpr
\fixedtotallength/2
\relax\relax
}
\edef\focidistance{%
\xintthefloatexpr\xintexpr
sqrt((\Yx-\Xx)^2+(\Yy-\Xy)^2)
\relax\relax
}
\edef\minoraxisradius{%
\xintthefloatexpr\xintexpr
sqrt((\fixedtotallength/2)^2-(\focidistance/2)^2)
\relax\relax
}
\pgfmathsetmacro\majoraxisangle{%
atan((\Yy-\Xy)/(\Yx-\Xx))
}
\draw[rotate=\majoraxisangle]
($(X)!0.5!(Y)$) ellipse ({\majoraxisradius} and {\minoraxisradius});
\filldraw[red] (X) circle (2pt) (Y) circle (2pt) (Z) circle (2pt);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.5]
\def\Xx{0}\def\Xy{0}
\def\Yx{3}\def\Yy{2}
\def\Zx{2}\def\Zy{3}
\coordinate [label=left:$X$] (X) at (\Xx,\Xy);
\coordinate [label=left:$Y$] (Y) at (\Yx,\Yy);
\coordinate [label=left:$Z$] (Z) at (\Zx,\Zy);
\edef\fixedtotallength{%
\xintthefloatexpr\xintexpr
sqrt((\Zx-\Xx)^2+(\Zy-\Xy)^2)+sqrt((\Zx-\Yx)^2+(\Zy-\Yy)^2)
\relax\relax
}
\edef\majoraxisradius{%
\xintthefloatexpr\xintexpr
\fixedtotallength/2
\relax\relax
}
\edef\focidistance{%
\xintthefloatexpr\xintexpr
sqrt((\Yx-\Xx)^2+(\Yy-\Xy)^2)
\relax\relax
}
\edef\minoraxisradius{%
\xintthefloatexpr\xintexpr
sqrt((\fixedtotallength/2)^2-(\focidistance/2)^2)
\relax\relax
}
\pgfmathsetmacro\majoraxisangle{%
atan((\Yy-\Xy)/(\Yx-\Xx))
}
\draw[rotate=\majoraxisangle]
($(X)!0.5!(Y)$) ellipse ({\majoraxisradius} and {\minoraxisradius});
\filldraw[red] (X) circle (2pt) (Y) circle (2pt) (Z) circle (2pt);
\end{tikzpicture}
\end{document}
旧答案
我们总是可以自己做计算。:)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left:$X$] (X) at (0,0);
\pgfgetlastxy{\Xx}{\Xy}
\coordinate [label=left:$Y$] (Y) at (3,2);% changed for testing
\pgfgetlastxy{\Yx}{\Yy}
\coordinate [label=left:$Z$] (Z) at (2,3);
\pgfgetlastxy{\Zx}{\Zy}
\pgfmathsetmacro{\fixedtotallength}{%
sqrt((\Zx-\Xx)^2+(\Zy-\Xy)^2)+sqrt((\Zx-\Yx)^2+(\Zy-\Yy)^2)
}
\pgfmathsetmacro{\majoraxisradius}{%
\fixedtotallength/2
}
\pgfmathsetmacro{\focidistance}{%
sqrt((\Yx-\Xx)^2+(\Yy-\Xy)^2)
}
\pgfmathsetmacro{\minoraxisradius}{%
sqrt((\fixedtotallength/2)^2-(\focidistance/2)^2)
}
\pgfmathsetmacro{\majoraxisangle}{%
atan((\Yy-\Xy)/(\Yx-\Xx))
}
\draw[rotate=\majoraxisangle]
($(X)!0.5!(Y)$) ellipse ({\majoraxisradius pt} and {\minoraxisradius pt});
\end{tikzpicture}
\end{document}
如果焦点垂直对齐,则此方法无效。但我确信您可以设法将\majoraxisangle
计算更改为\minoraxisangle
计算。
答案2
这在精神上与张瑞熙的回答类似,计算的辅助量少一些(它使用的只是给定点与焦点的距离之和是一个常数的事实),也许更多的 Ti钾Zy,也就是说,你需要做的就是说
\draw[ellipse through=X and Y and Z];
通过这些点画一个椭圆,或者
\node[elliptical node through=X and Y and Z,draw]{hello};
绘制椭圆节点,其中X
和Y
是焦点,Z
是附加点。但是,如果您尝试传递一些(好吧,我们称它们为不寻常的)坐标,则会出现错误dimension too large
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{through,calc,shapes.geometric}
\tikzset{ellipse through/.style args={#1 and #2 and #3}{
insert path={let \p1=(#1),\p2=(#2),\p3=(#3),
\n1={veclen(\x1-\x3,\y1-\y3)},
\n2={veclen(\x2-\x3,\y2-\y3)},
\n3={veclen(\x1-\x2,\y1-\y2)},
\n4={sqrt((\n1+\n2)^2-(\n3)^2)/2},
\n5={atan2(\y2-\y1,\x2-\x1)} in
%\pgfextra{\typeout{\n1,\n2,\n3,\n4,\n5}}
($(#1)!0.5!(#2)$)
[rotate around={\n5:($(#1)!0.5!(#2)$)}]circle({(\n1+\n2)/2} and {\n4})
}}}
\tikzset{/tikz/my ellipse a/.store in=\myella,
/tikz/my ellipse b/.store in=\myellb,
/tikz/my ellipse angle/.store in=\myellangle,
set ellipse pars/.code={
\tikzset{my ellipse a={\n6},
my ellipse b={\n4},my ellipse angle=\n5}
},
elliptical node through/.style args={#1 and #2 and #3}{
insert path={let \p1=(#1),\p2=(#2),\p3=(#3),
\n1={veclen(\x1-\x3,\y1-\y3)},
\n2={veclen(\x2-\x3,\y2-\y3)},
\n3={veclen(\x1-\x2,\y1-\y2)},
\n4={sqrt((\n1+\n2)^2-(\n3)^2)/2},
\n5={atan2(\y2-\y1,\x2-\x1)},
\n6={(\n1+\n2)/2} in [set ellipse pars]},
ellipse,
rotate=\myellangle,
minimum width=2*\myella,
minimum height=2*\myellb,
at={($(#1)!0.5!(#2)$)}}}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left:$X$] (X) at (0,0);
\coordinate [label=left:$Y$] (Y) at (2,1);
\coordinate [label=left:$Z$] (Z) at (2,2);
\node[draw,circle through=(Z)] at (X) {};
\draw[ellipse through=X and Y and Z];
\foreach \X in {X,Y,Z}
{\fill (\X) circle (1pt);}
\begin{scope}[xshift=7.5cm]
\coordinate [label=left:$X$] (X) at (0,0);
\coordinate [label=left:$Y$] (Y) at (3,1);
\coordinate [label=left:$Z$] (Z) at (3,0);
\node[draw,circle through=(Z)] at (X) {};
\node[elliptical node through=X and Y and Z,draw]{hello};
\foreach \X in {X,Y,Z}
{\fill (\X) circle (1pt);}
\end{scope}
\end{tikzpicture}
\end{document}