As requested, here is a very common derivation to the formula above, aka Snell's law. The core idea behind this derivation is that light always takes the shortest path (Fermat's principle). Here is how it goes:

The time light takes is given by:
$$t=\frac{\sqrt{a^2+x^2}}{v_1} + \frac{\sqrt{b^2+(d-x)^2}}{v_2}.$$
This time must be minimised, so $\frac{dt}{dx}=0$.
Differentiating the above equation gives us
$$\frac{dt}{dx}=0\longrightarrow\frac{x}{v_1\sqrt{a^2+x^2}} - \frac{d-x}{v_2\sqrt{b^2+(d-x)^2}}=0;$$
$$\frac{x}{v_1\sqrt{a^2+x^2}}= \frac{d-x}{v_2\sqrt{b^2+(d-x)^2}}.$$
Also,
$$\sin(i)= \frac{x}{\sqrt{a^2+x^2}}\;\mathrm{and}\; \sin(r) = \frac{d-x}{\sqrt{b^2+(d-x)^2}}.$$
Thus, $$\frac{\sin(i)}{v_1}=\frac{\sin(r)}{v_2}.$$
Voila! Snell's Law.