# Model How Light Reflects Off a Mirror With Python

Ever wonder how a mirror works? If you want to find the path that light takes when reflecting off a surface, you could use Fermat’s Principle. This states something like this:

The path that light takes is the path that takes the least time. When going from point A to point B, light will travel along the path that takes less time than any other options.

I actually used this idea in a question for my students learning about the variational principle with a question using Fermat’s Principle. It goes like this:

Below are two points (A and B). Light travels from A, reflects off the bottom surface and then travels to point B. Show that for the path of least time, the angle of incidence is equal to the angle of reflection.

The ideal solution (for physics majors) is to set up a function for the time it takes light to travel from A to B. This function should depend on the location (and thus the angle of incidence and reflection) where the light hits the surface. Once you have the time as a function of this position, you can use calculus to minimize the function.

But I’m not going to exactly do it that way—you know I like to mix things up. How about a numerical solution for this problem? By “numerical,” I mean that I’ll create a python program that models the motion of light from A to B. I will then change the reflection point on the surface until I get the smallest travel time. It’s going to be fun.

Let’s start with a basic program. This program starts with two points (A and B) and a beam of light that travels at a very low speed (so you can see it). The front of the light will aim towards some particular spot on the surface below—which you can change. Once the front of the light reaches this point, it changes direction to head towards point B. When the light reaches point B, the whole thing stops and it reports the total time. That’s it. Here’s the program (press “play” to run).

If you scroll down in the output window above, you can see the angles and times. You can manually change the location of the point on the surface where the light hits. But if you leave it as it is, the light takes 2.37 seconds with an angle of incidence of 16.699° and reflection of 67.380°.

Of course, I don’t want to manually change the reflection point on the surface—I’m too lazy for that. Instead, I will change the program so that it shoots the light at some point and then moves that point a little bit forward. I will do this until the point is just below the ending point so that I can plot the total time vs. incident angle.

Here’s what I get. Yes, this one is a little bit more complicated—but I put some comments in there for you (again, press “play” to run it).

Although I printed out the time for the light and the incident angle and the reflected angle, it’s a little hard to see. Here is a plot of time vs. angle for this same program (I also made the change in position smaller).

From that graph, it should be clear that the angle with the least time is when angle of incidence is equal to the angle of reflection (I suspect the curve looks rough because of the way I calculate the angles). Boom—there’s your Law of Reflection. Go ahead and try changing the location of point B and see what happens. It should still work.

Now for your homework. See if you can get this to reproduce Snell’s Law. This says that when light goes from one medium to another (like from air to water), the path of light will change based on the apparent speed of light in those mediums. The angle of incidence and reflection will then be related by the following equation.

In this expression, n_{1} and n_{2} are the respective indexes of refraction—this is the ratio of the speed of light in a vacuum (which is a constant) to the apparent speed of light in that material. So if you want to make this a numerical calculation, you would still have a light beam moving. However, once it gets into the second material the speed of the light will decrease some. Since the speed of light is not constant, the shortest time path is not a complete straight line. Trust me, this will be fun.