Simple Ray Tracing
- Matthew Joseph R. Banaag

- Jul 21, 2020
- 3 min read
This is a project I had to make for one of my majors where we had to create a simple ray tracing program and later on create a more generalized version of it. But here, I will just talk about the simple program.
The whole project can be found here: https://github.com/mbanaag/Programming-Projects.git
This project was written in Python.
So the goal here is: given a certain focal length, using the thin lens equation we can say where the object is located and find where the image will result units in term of cm.
So we will need to define a number of things, such as where to set an object in space, the lens, and some functions for plotting.
def si(f,xo,xl):
return (f*(xl-xo))/((xl - xo) - f)
#Function to set an object in space. Arguments are location in x and height of object
def object(x,y):
return plt.plot((x,x),(0,y), 'k')
#Function to set a lens in space. Arguments are location in x and height of object
def lens(x,y):
return plt.plot((x,x),(0,y*0.5), 'b'), plt.plot((x,x),(0,-y*0.5), 'b')
#Functions used to help plot the system
def trace(x1,y1,x2,y2):
return plt.plot((x1,x2),(y1,y2),'r')
def btrace(x1,y1,x2,y2):
return plt.plot((x1,x2),(y1,y2),'g--')
def ptslope(x1,y1,m,x):
return (m*(x - x1)) + y1
def xptslope(x1,y1,m,y):
return ((y - y1)/m) + x1
def oblq(x1,y1,x2,y2):
return plt.plot((x1,x2),(y1,y2),'r--')Now, using the rules of ray tracing (in this project we will be using simple converging lenses) we will be able to construct the needed functions to simulate the ray tracing. Note, that this will require no math if we were to draw this by hand, but in order to tell the computer to do this we'll need to do a little solving.
Here is a simplified version of the ray tracing using the three principal rays (but you can find the rest of it here: https://phys.libretexts.org/Courses/University_of_California_Davis/UCD%3A_Physics_7C_-_General_Physics/09%3A_Optics/9.3%3A_Lenses/9.3.2%3A_Lenses_and_Ray_Tracing)
1, The ray that is going into the lens parallel to the optical axis (the x-axis of where the optical system is setup); this ray gets bent to go through the focal point.
2. The ray that passes through the center; this ray does not bend.
3. The ray that passes through the focal point into the lens; this ray is refracted to be parallel with the optical axis
It is where these three lines intersect where the image will be formed.
Note: I also had to include backtracing, the event when the image formed is in front of the lens. The code posted is also for a two lens system.
#Function used to set the system in place and ray trace
def set(xo,yo,xl,yl,f):
if yo > yl/2:
return print("Choose a smaller object")
lens(xl,yl)
object(xo,yo)
xi = si(f,xo,xl)
mag = -abs(xi)/abs(xl - xo)
return trace(xo,yo,xl,yo),trace(xo,yo,xl,0),trace(xo,yo,xl-f,0),trace(xl,yo,xl + f,0),trace(xl + f,0,2*xl,-ptslope(xl + f,0,(yo/f),2*xl)),trace(xl,0,2*xl,-ptslope(xl,0,(yo/abs(xl-xo)),2*xl)),trace(xl - f,0,xl,-ptslope(xl - f,0,(yo/abs((xl - f)-xo)),xl)),trace(xl,-ptslope(xl - f,0,(yo/abs((xl - f)-xo)),xl),2*xl,-ptslope(xl - f,0,(yo/abs((xl - f)-xo)),xl)),object(xl + xi,yo*mag)
def backtrace(xim,yi,x2,y2,f1,f2):
lens(x2,y2)
object(xim,yi)
xi = si(f1,xim,x2)
mag = -abs(xi)/abs(x2 - xi)
return btrace(xim,yi,x2+f2,0),btrace(xim,yi,x2,yi),btrace(xim,yi,x2,0),trace(xim,yi,-x2,xptslope(xim,yi,(-yi/(x2-f2)),yi))
def set2(xo,yo,x1,y1,x2,y2,f1,f2):
if yo > y1/2:
return print("Choose a smaller object")
lens(x1,y1)
lens(x2,y2)
object(xo,yo)
xi = si(f1,xo,x1)
mag = -abs(xi)/abs(x1 - xo)
if abs(si(f1,xo,x1)) + x1 < abs(x2) and abs(si(f1,xo,x1)) + x1 < x2 - f2:
return set(xo,yo,x1,y1,f1),set(x1 + xi,yo*mag,x2,y2,f2)
else:
return set(xo,yo,x1,y1,f1),backtrace(x1 + xi,yo*mag,x2,y2,f1,f2)Another note, when setting the height of the lenses, it's half of what you input because half will be above the x-axis and the other half will be below. These are the arguments of the function in order:
Location of object in space
Height of object
Location of lenses in space
Height of lens
Focal length
So for example:
set2(0,1.5,1,4,2,5,.25,.7)Will give us a result that looks like this:

Where the image formed by backtracing can be seen in between the two lenses.


Comments