3

Suppose we programmatically calculate two coordinates P and Q in TikZ and then choose a positive number r. This data defines a unique ellipse:

enter image description here

The center of this ellipse is the midpoint between P and Q and the value of R is half the distance between P and Q.

My questions are:

Question 1. Is there a simple way to draw this ellipse in TikZ?

Question 2. More generally, is there a simple way to draw arbitrary arcs on this ellipse?

1

2 Answers 2

5

While you are waiting for the TikZ helpers, here is a Metapost version. This is almost a one-liner in MP.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}

vardef elliptic_path(expr a, b, minor_radius) =
    fullcircle xscaled abs(a-b) yscaled 2 minor_radius
               rotated angle (a-b) shifted 1/2[a,b]
enddef;

beginfig(1);
    pair P, Q; numeric r;
    P = origin; Q = 89 dir 42;
    r = 34;

    path e; 
    e = elliptic_path(P, Q, r);

    draw e withpen pencircle scaled 1.414 withcolor 3/4 white;
    drawarrow subpath (1, 5) of e withcolor red;

    dotlabel.lft("$P$", P);
    dotlabel.rt("$Q$", Q);
endfig;
\end{mplibcode}
\end{document}

This is wrapped up in luamplib so compile with the lualatex engine.

2
  • @Sebastiano is that better?
    – Thruston
    Apr 16, 2020 at 22:09
  • Yes...I remembered the coronavirus :-)
    – Sebastiano
    Apr 16, 2020 at 22:11
2

I think this is a bit of an xy question. What you probably want to have on the long run is something that transforms you in a coordinate system the center of which is the center between two points, the x axis goes from the center to one of the two points, and the y axis is orthogonal and has length r. Then a circle in this coordinate system becomes your ellipse, ordinary arcs, elliptical arcs, and so on. Obviously, TikZ does that out of the box. To make things a bit more convenient I added a style, elli cs, that installs this coordinate system from these data. With this style, the graph becomes as simple as

 \begin{scope}[elli cs={A={(P)},B={(Q)},r=9mm}]
  \draw circle[radius=1];
  \draw[dashed] (0,1) -- node[midway,fill=white]{$r$} (0,0) node[dot] {}
  -- node[midway,fill=white]{$R$} (1,0);
  \draw[blue,-{Stealth[bend]}] (-30:1) arc[start angle=-30,end angle=120,radius=1];
 \end{scope}

As you see, for the arc there is no guess work required, the angles have a very intuitive interpretation in this coordinate system. You can do many further operations in this frame very conveniently, and also transform shapes according to it.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,calc}
\begin{document}
\begin{tikzpicture}[dot/.style={circle,fill,inner sep=1pt},thick,
    elli cs/.code={
    \tikzset{ellipse through/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/ellipse through/##1}}%
    \edef\temp{\noexpand\tikzset{shift={($0.5*\pv{A}+0.5*\pv{B}$)},
        x={($0.5*\pv{B}-0.5*\pv{A}$)},
        y={($($\pv{A}!\pv{r}!90:\pv{B}$)-\pv{A}$)}
        }}%
    \temp   
    },
    ellipse through/.cd,r/.initial=5mm,A/.initial={(-1,0)},
        B/.initial={(1,0)}]
   \path[nodes=dot] (0,0) node[label=below left:$P$] (P){}
     (4,1.5) node[label=below right:$Q$] (Q){};         
   \begin{scope}[elli cs={A={(P)},B={(Q)},r=9mm}]
    \draw circle[radius=1];
    \draw[dashed] (0,1) -- node[midway,fill=white]{$r$} (0,0) node[dot] {}
    -- node[midway,fill=white]{$R$} (1,0);
    \draw[blue,-{Stealth[bend]}] (-30:1) arc[start angle=-30,end angle=120,radius=1];
   \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

2
  • For my humble opinion if you put the point Q in the front is best hidden under the blue curve.
    – Sebastiano
    Apr 16, 2020 at 22:13
  • 2
    @Sebastiano This is on purpose. We define P and Q first. They are the input of the "elliptical" coordinate system in which one can draw all the things the OP mentions (and more) very easily with an intuitive interpretation. So the arc comes last, and hence is on top of the other objects. What is important here is that the start and end points of the arc is from an intuitive parametrization.
    – user194703
    Apr 16, 2020 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .