A best approximation of a real number $r$ is a rational fraction $a/b$ with $b>0$ such that for every rational fraction $c/d$ with $d \le b$ and $c/d \ne a/b$,
$$\left| r - \frac{a}{b}\right| < \left|r - \frac{c}{d}\right|$$
Theorem: Every best approximation of a number $r$ is either a convergent or an intermediate fraction of the continued fraction representing $r$ (if you include a "$-1$'th order convergent $1/0$).
For example: if $r = \pi$, the continued fraction representation starts
$3 + 1/(7 + 1/(15 + 1/(1 + ...)))$. The first few convergents are
$$\frac{1}{0}, \frac{3}{1}, \frac{22}{7}, \frac{333}{106}, \frac{355}{113}$$
The intermediate fractions between $1/0$ and $22/7$ are $$\frac{4}{1}, \frac{7}{2}, \frac{10}{3}, \frac{13}{4}, \frac{16}{5}, \frac{19}{6}$$ The intermediate
fractions between $3/1$ and $333/106$ are
$$ \frac{25}{8}, \frac{47}{15}, \frac{69}{22}, \frac{91}{29}, \frac{113}{36}, \ldots,
\frac{311}{99}$$
The first few best approximations of $\pi$ are
$$\frac{3}{1}, \frac{13}{4}, \frac{16}{5}, \frac{19}{6}, \frac{22}{7}, \frac{179}{57},
\frac{201}{64}, \frac{223}{71}, \frac{245}{78}, \frac{267}{85}, \frac{289}{92}, \frac{311}{99}, \frac{333}{106}, \frac{355}{113}$$
Note: Here we want a line segment $(s_k, e_k)$ to be oriented such that the point $c$ is to the right of the colinear line through $s_k$ to $e_k$.
(BTW, "colinear line" is redundant.) This is a key property. As long as you choose $s_k$ and $e_k$ this way (so $(s_{ky}-c_y)(e_{kx}-c_x) \ge (s_{kx}-c_x)(e_{ky}-c_y)$), the problem has a simple solution. If travelling from $s_k$ to $e_k$ has the circle center on your right, then you are travelling clockwise, and the angle should be decreasing. That is, the argument for $e_k$ should always be less than or equal to the argument for $s_k$.
If this isn't true, there are two ways we can correct it: either add $2\pi$ to the argument of $s_k$ or subtract $2\pi$ from the argument of $e_k$. Which technique makes sense depends on whether we are looking for the rightmost or leftmost segment. And just to be clear, I assume that these mean:
- "Rightmost segment" is, among all segments with at least one end in the sector, the one with the most clockwise argument of all ends (include one outside the sector, provided the other end is in).
- "Leftmost segment" is, among all segments with at least one end in the sector, the one with the most counter-clockwise argument of all ends (include one outside the sector, provided the other end is in).
Note that if the sector happens to open downward (instead of upward as in your examples), the "Rightmost segment" will be on the left and the "Leftmost segment" will be on the right (this is why "clockwise" and "counter-clockwise" are the better terminology for rotational phenomena).
Initialize variables
MinIndex = -1
MaxIndex = -1
MinAngle = 13 # > 4 * pi
MaxAngle = -7 # < -2 * pi
Pass through your segments, skipping over any segment without at least one endpoint in the sector. For the rest,
- If $\arg s_k \ge \arg e_k$, then
- if $\arg s_k >\text{ MaxAngle}$, then $\text{MaxAngle } = \arg s_k, \text{ MaxIndex } = k$.
- if $\arg e_k <\text{ MinAngle}$, then $\text{MinAngle } = \arg e_k, \text{ MinIndex } = k$.
- If $\arg s_k < \arg e_k$, then
- if $\arg s_k + 2\pi >\text{ MaxAngle}$, then $\text{MaxAngle } = \arg s_k + 2\pi, \text{ MaxIndex } = k$.
- if $\arg e_k - 2\pi <\text{ MinAngle}$, then $\text{MinAngle } = \arg e_k - 2\pi, \text{ MinIndex } = k$.
When you are finished, MinIndex will point to the Leftmost segment and MaxIndex will point to the Rightmost.
Best Answer
Hint: What can we do if we know that the lowest digit is $0$?
What if the lowest digit is not $0$?
How does it scale?
Example: