I get a different formula. Let me show you how I derived it.
Using the following diagram:
We can write the following equations by looking at triangles:
$$\begin{align}\frac{x}{L} &= \sin(\theta_1-\theta_2)\\&=\sin\theta_1 \cos\theta_2 - \cos\theta_1\sin\theta_2\\
\frac{d}{L}&=\cos\theta_2\end{align}$$
Assuming that the air has a refractive index of 1, we can further write
$$\frac{\sin \theta_1}{\sin\theta_2}=n$$
From basic geometry we know that for angles in the first quadrant,
$$\cos\theta = \sqrt{1-\sin^2\theta}$$
Combining these gives
$$\begin{align}x &= \frac{d}{\cos\theta_2}\left(\sin\theta_1 \cos\theta_2 - \cos\theta_1 \sin\theta_2 \right)\\
&= d\left(\sin\theta_1 - \frac{\sin\theta_1\cos\theta_1}{n\cos\theta_2}\right)\\
&=d\sin\theta_1\left(1-\frac{\sqrt{1-\sin^2\theta_1}}{n\sqrt{1-\frac{\sin^2\theta_1}{n^2}}}\right)\\
&=d\sin\theta_1\left(1-\frac{\sqrt{1-\sin^2\theta_1}}{\sqrt{n^2-\sin^2\theta_1}}\right)\end{align}$$
Note that with this expression, the distance $x$ will approach $d$ when $\theta_1$ approaches $\pi/2$ since the second term will vanish.
You might want to compare my approach with yours. I'm not claiming mine is right...
Firstly, your point about total-internal-reflection (look into the tag wiki for further info about the phenomenon) resulting in 100% transmission of light energy and hence 100% intensity of incident light being preserved during reflection is conventionally considered true, but is not entirely correct.
Let me explain:
Reflection of any type, even if it is total internal reflection, cannot reflect 100% of the incident light energy. Total Internal Reflection nearly 100% (99.9999% maybe, but not 100%) efficiency compared to conventional reflection from a surface.
An interface between 2 optical media has the property of a "critical angle", the angle of incidence beyond which if light is incident on the interface from the optically denser medium into the optically rarer medium, it will be reflected back entirely into the denser medium, with no refraction into the rarer medium. Normally, at an interface between 2 transparent media of different optical densities, the light wave will be partially reflected into the medium from which it was incident and partially refracted into the medium into which it was incident, until the angle of incidence goes beyond this critical angle. This phenomenon of total internal reflection is usually observed for light, but is also applicable for other types of waves, like sound and waves on a string. This phenomenon is explained by Huygen's Wave Theory of Light in a classical sense. It occurs at the interface between 2 media of different densities in which the wave travels.
Hence, the intensity of incident light is preserved nearly entirely in total internal reflection, except for that minor fraction which may be absorbed by the denser medium itself, which is low due to the medium being transparent. There is also some energy loss due to photon tunneling across the interface, but again, it is not conventionally significant (total about $10^{-3} \%$). There are also evanescent-waves across the interface, but they do not result in net energy transfer across the interface. Refer Wikipedia here.
In conventional reflection, 2 surfaces are involved:
1) A transparent unsilvered surface
2) An opaque silvered surface
For light transmission through the unsilvered surface, similar energy losses are applicable as in case of total internal reflection.
However, for light reflection at the silvered surface, the surface being opaque, will absorb a significant portion (say about 1%-2%) of the incident light energy, while it will reflect most of the light energy due to it being silvered and reflective.
This 2nd significant energy loss of incident light at the silvered surface is not applicable for total internal reflection, hence we commonly say that total internal reflection forms images at 100% the brightness (intensity) of the incident light.
Best Answer
When light is propagating in glass or other medium, it isn't really true, pure light. It is what (you'll learn about this later) we call a quantum superposition of excited matter states and pure photons, and the latter always move at the speed of light $c$.
You can think, for a rough mind picture, of light propagating through a medium as somewhat like a game of Chinese Whispers. A photon is absorbed by one of the dielectric molecules, so, for a fantastically fleeting moment, it is gone. The absorbing molecule lingers for of the order of $10^{-15}{\rm s}$ in its excited state, then emits a new photon. The new photon travels a short distance before being absorbed and re-emitted again, and so the cycle repeats. Each cycle is lossless: the emitted photon has precisely the same energy, momentum and phase as the absorbed one. Unless the material is birefringent, angular momentum is perfectly conserved too. For birefringent mediums, the photon stream exerts a small torque on the medium.
Free photons always travel at $c$, never at any other speed. It is the fact that the energy spends a short time each cycle absorbed, and thus effectively still, that makes the process have a net velocity less than $c$.
So the photon, on leaving the medium, isn't so much being accelerated but replaced.
Answer to a Comment Question:
A very good question. This happens by conservation of momentum. The interaction is so short that the absorber interacts with nothing else, so the emitted photon must bear the same momentum as the incident one. Also take heed that we're NOT a full absorption in the sense of forcing a transition between bound states of the atom (which gives the sharp spectral notches typical of the phenomenon), which is what David Richerby is talking about. It is a transition between virtual states - the kind of thing that enables two-photon absorption, for example - and these can be essentially anywhere, not at the strict, bound state levels. As I said, this is a rough analogy: it originated with Richard Feynman and is the best I can do for a high school student who likely has not dealt with quantum superposition before. The absorption and free propagation happen in quantum superposition, not strictly in sequence, so information is not being lost and when you write down the superposition of free photon states and excited matter states, you get something equivalent to Maxwell's equations (in the sense I describe in my answer here or here) and the phase and group velocities naturally drop out of these.
Another way of qualitatively saying my last sentence is that the absorber can indeed emit in any direction, but because the whole lot is in superposition, the amplitude for this to happen in superposition with free photons is very small unless the emission direction closely matches the free photon direction, because the phases of amplitudes the two processes only interfere constructively when they are near to in-phase, i.e. the emission is in the same direction as the incoming light.
All this is to be contrasted with fluorescence, where the absorption lasts much longer, and both momentum and energy is transferred to the medium, so there is a distribution of propagation directions and the wavelength is shifted.
Another comment:
If you are careful, the book's comment may have some validity. We're talking about a superposition of photon and excited matter states when the light is propagating in the slab, and this superposition can indeed be construed to have a nonzero rest mass, because it propagates at less than $c$. The free photons themselves always propagate at $c$ and always have zero rest mass. You actually touch on something quite controversial: these ideas lead into the unresolved Abraham-Minkowsky Controversy.