Solution (source code)

= Solution

At <periapsis>, the particle's <mean longitude> equals its <longitude of periapsis>: $\lambda=\varpi$. Hence the <pericentre geometry of an exterior resonance> gives
$$
\phi=p(\varpi-\lambda_p),\qquad
\boxed{\Delta\theta_{\rm peri}=\frac{\phi+2\pi j}{p},\quad j\in\mathbb Z.}
$$
The <resonant argument> therefore fixes the angular separation from the planet whenever the particle reaches <periapsis>. With $r_{\rm peri}=a(1-e)$ and planetary radius $a_p$, the actual separation is
$$
\boxed{d_{\rm peri}^2=a_p^2+r_{\rm peri}^2-2a_pr_{\rm peri}\cos\Delta\theta_{\rm peri}.}
$$
Thus a restricted libration phase can prevent close encounters even when the radial ranges of the two orbits overlap: this is <resonance protection>.