Snell's law states that the ratio of the sines of the angles of incidence and refraction is equivalent to the reciprocal of the ratio of the indices of refraction of given materials:
I wanted to implement a simple program to visualize the law. Since ,
and
are known, here's how I calculate
:
theta2 = asin((sin(theta1) * n1) / n2);
The problem is that for certain values of and
(for example 1.52 and 1.0 for glass and air respectively), the result of
(sin(theta1) * n1) / n2
can be more than 1.0 for bigger angles, which makes asin
return NaN. The way I cope with this is to check if (sin(theta1) * n1) / n2
is greater than 1.0 and if that's the case first subtract 1 from it, compute with the new value, then add
0.5 * M_PI
(or 90.0 degrees) to it. Is there a better way?