# How can I get correct the point A and B automatically in this picture?

I am trying to find a general method to find the points A and B automatically of this picture. I can not get it. I must calculate by hand. \documentclass[tikz,border=3.14mm]{standalone}
\usepackage{fouriernc}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\begin{document}
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}
[scale=1,tdplot_main_coords]
\path
coordinate (O) at (0,0,0)
coordinate (T) at  (0,0,3)
coordinate (B) at  (3,{-sqrt(7)},0)
coordinate (A) at  (0,4,0);
\draw[thick, dashed] (O) -- (A) node[midway,sloped,below] {$r$};
\draw[thick, dashed] (T) -- (A) node[midway,right] {$R$};
\draw[thick, dashed] (T) -- (O) node[midway,left] {$d$};
\foreach \v/\position in {T/above,O/below,A/below,B/below} {
\draw[draw =black, fill=black] (\v) circle (1.2pt) node [\position=0.2mm] {$\v$};
}
\begin{scope}[tdplot_screen_coords, on background layer]
\pgfmathsetmacro{\R}{5}%
\pgfmathsetmacro{\r}{4}%
\fill[ball color=cyan!50, opacity=1.0] (T) circle (\R);
\end{scope}
\tkzMarkRightAngle[size = 0.3](T,O,A);
\draw [thick] (B) arc (318.5:450:4);
\draw [thick, dashed] (A) arc (90:318.5:4);
\end{tikzpicture}
\end{document}


Is there a way to find the point A and B automatically?

• The range for the visible angle has been worked out in tex.stackexchange.com/questions/46850/… and similar posts. This allows you to determine the range for the solid arc, and thus the coordinates A and B. Alternatively you can load pgfplots and its library fillbetween and just draw the "correct" intersection segment solid.
– user121799
Aug 11, 2019 at 9:48
• Thank you very much. I shall try. Aug 11, 2019 at 11:42

AFAIK the first post working out the critical values that distinguish the stretches on the foreside from those on the backside was this. (To all who are interested: this is what I mean with "giving credit". Even though the following will use different formulae and macros, it was nice for me to see a post in which the derivation is explained and which I can use to compare my results. I personally feel one should always add these "references" because then Alain Matthes answer has many links and users have simple way of looking for related posts. Personally I just feel that Alain deserves credit for that.) These things were done in different coordinates than those tikz-3dplot, but the analogous expressions in the tikz-3dplot conventions were put down in this answer. With those, you can switch to an xy plane and all you need to say is

 \draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:4)
coordinate (A)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:4) coordinate(B);


Here, alpha1 and alpha2 are the critical phi values that determine where the path makes transitions from the fore- to the backside and thus fix the locations of A and B analytically.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{fouriernc}
\usepackage{tikz-3dplot}
\usetikzlibrary{backgrounds}
\begin{document}
\tdplotsetmaincoords{60}{110}
\begin{tikzpicture}[scale=1,tdplot_main_coords,declare function={R=5;
alpha1(\th,\ph,\b)=\ph-asin(cot(\th)*tan(\b));%
alpha2(\th,\ph,\b)=-180+\ph+asin(cot(\th)*tan(\b));%
beta1(\th,\ph,\a)=90+atan(cot(\th)/sin(\a-\ph));%
beta2(\th,\ph,\a)=270+atan(cot(\th)/sin(\a-\ph));%
}]
\path
coordinate (O) at (0,0,0)
coordinate (T) at  (0,0,3);
\begin{scope}[tdplot_screen_coords, on background layer]
\fill[ball color=cyan!50, opacity=1.0] (T) circle (R);
\end{scope}
\begin{scope}[canvas is xy plane at z={3-sqrt(5^2-4^2)}]
\draw[thick] ({alpha1(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:4)
coordinate (A)
arc({alpha1(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:
{alpha2(\tdplotmaintheta,\tdplotmainphi,{atan(4/5)})}:4) coordinate(B);
\end{scope}
\begin{scope}[on background layer]
\draw[thick, dashed] (O) -- (A) node[midway,sloped,below] {$r$};
\draw[thick, dashed] (T) -- (A) node[midway,right] {$R$};
\draw[thick, dashed] (T) -- (O) node[midway,left] {$d$};
\foreach \v/\position in {T/above,O/below,A/below,B/below} {
\draw[draw =black, fill=black] (\v) circle (1.2pt) node [\position=0.2mm] {$\v$};
}
\end{scope}
\end{tikzpicture}
\end{document} If you compare this very carefully to your output, then you see that there is a tiny discrepancy in the location of A. This could be a numerical issue, or one of use made a mistake.

• Yes. Your answer is correct the point A. In my code, I put the point A by my hand. Aug 12, 2019 at 2:16
• Please for me ask some options in your code: 1) How about the numbers \a and \b; 2) what is {atan(4/5)}. I can change into {atan(\r/\R)}? Aug 12, 2019 at 2:47
• @minhthien_2016 Instead of \R this code uses R, which is set in declare function. I do not know what \a and \b are in this code. You could just set \pgfmathsetmacro{\r}{sqrt(R^2-3^2)} and then replace 4/5 by \r/R or something like this. The value 3 is the z coordinate of T, the center of the ball, and \begin{scope}[canvas is xy plane at z={3-sqrt(5^2-4^2)}] is just to say that I draw in the plane at z=0 but 0.
– user121799
Aug 12, 2019 at 2:59
• Thank you very much. Aug 12, 2019 at 3:03
• If the center of circle not at O(0,0), e.g at C(1,2) How can I use your code? Aug 12, 2019 at 3:49