3

The following code draws a circle with given center X and a point Z on its circumference using tikz.

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

How to draw with tikz (or tkz-euclide) an ellipse when the foci are X and Y and a point Z on its circumference are given? In other words how to draw an ellipse with given focal points X and Y which passes through the point Z?

4

New answer

The original post never mentioned anything about scaling. If you do not mind loading xintexpr (which provides high-precision calculations), then here is an alternative that meets your additional scaling request.

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

ellipses


Old answer

We can always do the math ourselves. :)

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

ellipse

This will not work if the foci are vertically aligned. But I am sure that you can manage to change the \majoraxisangle calculation to a \minoraxisangle calculation.

  • Why when one scales your tikz-picture, the ellipse does not pass through Z? I mean adding [scale=.5] just after \begin{tikzpicture}. – Name Oct 17 '18 at 7:17
  • @Name Happy now? ;-) – Ruixi Zhang Oct 19 '18 at 21:55
  • Thank you for your detailed answer and fixing the issue with scaling. – Name Oct 20 '18 at 4:46
2

This is similar in spirit to Ruixi Zhang's answer, computes a bit less auxiliary quantities (all it uses is the fact that the sum of distances of a give point from the foci is a constant) and perhaps more TikZy, i.e. all you need to do is to say

\draw[ellipse through=X and Y and Z];

to draw an ellipse through these points or

\node[elliptical node through=X and Y and Z,draw]{hello};

to draw an elliptical node, where X and Y are the foci and Z is the additional point. However, if you try to pass some, well, let's call them unusual coordinates, there will be a dimension too large error.

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

enter image description here

  • Unfortunately, your solution does not work for the choices X=(0,0), Y=(1,0) and Z=(2,3) given in my original question. The ellipse given by your solution does not pass through Z. – Name Oct 17 '18 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.