# How do I bound a circle on the surface of a sphere using coordinates?

Suppose I have a sphere of radius $$R$$ with a longitude/latitude system on it (i.e. great circle lines for longitude, constant $$z$$ lines for latitude). Then I place a circle on the surface of the sphere with radius $$r < \frac{\pi}{4}R$$.

What set of coordinate lines encloses this circle with tangent lines that have the smallest area? I'm thinking that the number of bounding lines should be limited to $$1$$$$4$$, to cover cases where the circle should be bounded by a near square, a triangle, a lune, and a single line of latitude (e.g. if the circle has $$r=\frac{\pi}{4}R$$ and is centered on a pole, its bounding coordinate is just latitude$$=0$$).

The answer is to take and find all of the coordinate tangents, then handle the special cases where the circle contains one or both of the coordinate poles. The answer can be summarized as finding the supremum and infimium latitude ($$\phi_s$$ and $$\phi_i$$) and longitude ($$\lambda_s$$ and $$\lambda_i$$) for the circle centered at $$(\lambda_c,\phi_c)$$. It just comes down to defining how those quantities are found in a coordinate system with discontinuities and edges.
First, the easy case. $$\phi_{s} = \operatorname{min}(\phi_c + r/R, \pi/2)$$ and $$\phi_{i} = \operatorname{max}(\phi_c - r/R, -\pi/2)$$. Because $$\phi$$ is limited to $$[-\pi/2,\pi/2]$$, whenever $$\phi_s=\pi/2$$ the top bounding latitude actually does not exist in a meaningful way (the boundary excludes a set of measure zero). Similarly for when $$\phi_i=-\pi/2$$.
Longitude is a bit trickier due to its periodicity. If we allow for the map from $$(\lambda,\phi)$$ to physical space to be many to one (i.e. we allow $$\lambda$$ to take any value and rely on trig functions to get the physical vector position right) it simplifies things. Given that relaxed definition of $$\lambda$$, then the value for $$\lambda_i$$ is bounded between $$[\lambda_c + r/R, \lambda_c+\pi/2]$$ (when $$r/R > \pi/2$$, you need to flip the sense of inside/outside the circle to get something meaningful). This fact is proved by inspection - start with $$\phi_c$$ on the sphere's equator. Then $$\lambda_s = \lambda_c + r/R$$ because the circle is perpendicular to the equator, just like the lines of longitude. As the circle moves North, the tangent Longitudes move toward the top of the circle, opening up until the edge of the circle is the North pole, then $$\lambda_s = \lambda_c + \pi/2$$ because then the great circle defined by $$\lambda=\lambda_c\pm\pi/2$$ is tangent to the circle. If the center moves any further North, then there are no lines of longitude tangent to the circle because all of them cross the circle's boundary to pass through the North pole. $$\lambda_i$$ behaves analogously.
Now, the use case for this idea is to use a small number of coordinate lines to divide the surface of the sphere into points that are definitely outside of a circle from those that might be inside; for example, when a search for points on the sphere can be done quickly using a coordinate bounded region, then perform the search for circle bounded points on that subset. Given that goal, the restriction to 4 lines, at most, is a bit too restrictive, since when $$r$$ is an appreciable fraction of $$R\pi/4$$, the "maybe inside" area can still pretty large when the edge of the circle nears a pole (precise definitions of "appreciable" depends on the situation the algorithm is being applied to). In the case where $$\phi_c > \pi/2-r/R$$, adding a middle latitude $$\phi_m$$ between $$\phi_c$$ and $$r/R - \pi/2 + \phi_c$$ to cut the circle into two parts, and then finding the bounding lines for the two new parts can be worthwhile. Finding those bounds, and which value of $$\phi_m$$ minimizes the area enclosed, is left as an exercise.