Given the diameter, draw in pgfplots the circle and its intersection with x-axis

Given two points, A and B, I want to draw the circle of diameter AB together with its intersection with the X axis.

Intended result is the figure

I got this with the code

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\rd}{(sqrt(2))}
\pgfmathsetmacro{\fs}{(1+sqrt(2))}
\pgfmathsetmacro{\ss}{(1-sqrt(2))}
\begin{axis}[
x=1cm,
y=1cm,
axis lines=middle,
xmin=-1,
xmax=3,
ymin=-2,
ymax=2
]
\node[right] (A) at (0,1) {A} ;
\node[right] (B) at (2,-1) {B} ;
\node (V) at (1,-1.2) {label} ;
\node[left] (C) at (\ss,0.2) {C} ;
\node[right] (D) at (\fs,0.2) {D} ;
\end{axis}
\end{tikzpicture}
\end{document}


While this works, I am not satisfied with it.

What I would like to achieve is that I should provide just the coordinates of A and B, the diameter of the given circle, then everything else should be computed from this.

In particular, I shouldn't compute the center of the circle, its radius and the coordinates of C,D. Moreover, the dots should be drawn in the node and the letters should be the labels. Also, the label of the circle should be defined within the circle.

• Providing just A and B won't be enough, as you can draw an infinite number of circles throguh A and B. You need a third point or a third condition, like a symmetry requirement. Which one is it, or should it be? Commented Aug 18, 2022 at 14:12
• @MS-SPO: I said [A,B] has to be the diameter of the circle: there is only one such circle, to my knowledge.
Commented Aug 18, 2022 at 14:21
• Ah, now I see. // Why not making it more obvious by stating e.g. "diameter AB as the third condition" ? Commented Aug 18, 2022 at 14:44
• @MS-SPO Yes, maybe it's better. I've added it.
Commented Aug 18, 2022 at 15:00
• This is something relatively easy using tkz-euclide package, but as I understand you need a solution using pgfplots is't it? Commented Aug 18, 2022 at 15:33

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{calc, intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x=1cm, y=1cm,
axis lines=middle,
xmin=-1,  xmax=3,
ymin=-2,  ymax=2,
x axis line style={name path=xaxis},
]
\coordinate (A) at (0,1);
\coordinate (B) at (2,-1);
\draw[name path=circle] let \p1=($0.5*(B)+0.5*(A)$), \p2=($0.5*(B)-0.5*(A)$), \n1={veclen(\x2,\y2)} in (\p1) circle[radius={\n1}];
\fill[fill=yellow!80!orange, name intersections={of=xaxis and circle}]
\end{axis}
\end{tikzpicture}
\end{document}


• This is very close to what I asked. A few points missed: 1) you deleted axis on top : I need it; 2) do I need to put the "label" manually? If I use node {label} when drawing the circle, it goes close to the center; 3) what if the circle is tangent to the x axis (for example, B=(4,4))? In this case, the (unique) intersection is not found
Commented Aug 18, 2022 at 23:00
• Do you mean the axis on top option? It is not in your code, -I do not see anything preventing you to add it. For the label, you can use e.g. ...node[below=\n1-0.5cm]{label};. For the special cases where the circle intersect in no points, or one point, you need to handle them separately with e.g. an if statement. If you need it, you can ask a question about that. Commented Aug 19, 2022 at 12:53
• You are right the option axis on top was not in my code: it looks like I forgot to copy it from my original figure. My fault. Sorry. In any case, I tried adding it as soon as I saw your code, but I get an error which I can't understand.
Commented Aug 19, 2022 at 15:35
• Yes - I see now. When the axis is drawn lastly, we need an other way to name it. You can use e.g. \path[name path=xaxis] (current axis.left of origin) -- (current axis.right of origin); as the first thing inside axis and delete the x axis line style={name path=xaxis}, option. Commented Aug 19, 2022 at 16:12

Using tkz-euclide package we have:

\documentclass{standalone}
\usepackage{tkz-euclide}
\usetikzlibrary{calc}
\tkzSetUpPoint[size=5]

\begin{document}
\begin{tikzpicture}[domain=-2:1.2]
\tkzInit[xmin=-1,xmax=3,ymin=-2,ymax=2,xstep=1,ystep=1] % limits the size of the axes
\tkzDrawX[>=stealth] % arrow of x-axis
\tkzDrawY[>=stealth] % arrow of y-axis
\foreach \x in {-1,1,2,3} \draw (\x,0.1) -- (\x,-0.1) node[below] {\x};% ticks on x-axix
\foreach \y in {-2,-1,1,2} \draw (0.1,\y) -- (-0.1,\y) node[left] {\y};% tikc on y-axis
\tkzDefPoints{0/1/A,2/-1/B,-1/0/X,0/0/O} %given points A,B,X(-1,0) and O(0,0)
\tkzDefMidPoint(A,B) %AB diameter-> midpoint K=center
\tkzGetPoint{K} %save coordinates of midpoint in name K
\tkzDrawCircle[thick,black](K,A) %draw circle with center K and R=KA
\tkzInterLC(O,X)(K,B) %find coordinates of intersection line OX (x-axis) and circle
\tkzGetPoints{D}{C} %save these coordinates with names D and C, points of intersection
\tkzDrawPoints[fill=yellow](C,D) %draw points C and D
\tkzDrawPoints[fill=blue](A,B) %fill points A and B with blue color
\tkzLabelPoints[right](A,B) % label points A and B with letters A and B
\tkzLabelPoint[above left](C){C}% labels point C
\tkzLabelPoint[above right](D){D}%labels point D
\node at (1,-1.2) {label};%labels the circle
\draw[color=blue,thick]   plot (\x,{(\x)^2+\x})    node[right] {$f(x) = x^2+x$};% draw parabola, page 344 tikz manual.
\end{tikzpicture}
\end{document}


• This is very nice and I will save it for future reference but, actually, I realize it is a bit different from what I was looking for in this case. Anyway, thank you for your time and this interesting piece of code.
Commented Aug 18, 2022 at 23:04

With pstricks it requires a comparatively short code:

    \documentclass[svgnames]{standalone}
\usepackage{pst-eucl}

\begin{document}

\begin{pspicture}(-1.25,-2.25)(3.25,2.25)
\psset{linewidth=0.5pt}
\psaxes[arrowinset=0.1, ticksize=2pt -2pt, showorigin = false]{->}(0,0)(-1,-2)(3.,2)
\pnodes(-1,0){X1}(2,0){X2}(1,0){I}(0,1){A}(2,-1){B}
\uput[r](A){$A$}\uput[r](B){$B$}
\pstCircleAB{A}{B}
\psdots[linecolor=RoyalBlue](A)(B)
\pstInterLC[PointName=none]{X1}{X2}{I}{A}{C}{D}
\psdots[linecolor=Yellow](C)(D)
\uput[ul](C){$C$}\uput[ur](D){$D$}
\end{pspicture}

\end{document}


• Thank you but this has to go in a work which already uses an awful amount of packages, so I am not keen on adding another one.