Thin Lens Ray TracingAn instructional physics program by Donald E. Simanek.Introductory physics textbooks introduce the subject of lenses and mirrors by use of a geometric process called "ray tracing". This method is an exact geometric interpretation of the simple-lens formula 1/p + 1/q = 1/f, where p and q are the object and image distance (from the lens) and f is the focal length of the lens. Therefore geometric ray-tracing has all of the limitations of that formula.
Specifically, the simple-lens formula applies only to paraxial rays, those rays which make only small angles with the lens axis. In fact, the angles must be so small that the sines of the angles are nearly equal to the angles' radian measure, and the cosines of the angles are nearly equal to 1. Lens aberrations are by definition any deviation from the simple-lens formula, and therefore the formula does not reveal their existence. So this method cannot say anything about curvature of field, chromatic or spherical aberration, astigmatism, coma, thick lenses, etc. So what is the simple-lens formula good for? It gives object-image-focal length relations correctly, as well as object and image character (real or virtual) and image magnification.The ray-tracing process and this phenomena are difficult for students at first, particularly when dealing with virtual objects or images. This computer program was written to help students visualize such ray diagrams and form a stronger understanding of images of all character. This computer program may be used by the instructor, with a graphics projector, or by the student as an interactive tutorial for concept development. This document has been prepared with monochrome graphics, to save you toner and ink costs if you print it out. If you wish to see what this program's typical computer screen display looks like (black background) click here to view lens0.gif. Color is only provided in the compiled version for VGA screen. This program does not simulate multiple lenses, thick lenses, or aspheric lenses. It does not simulate aberrations of any kind, for the simple-lens formula applies only to paraxial rays and is limited to values of p, q, and f which are large compared to the lens thickness. The method assumes an infinitesimally thin lens. This is not a lens-design program. Many commercial software programs do that. It's purpose is primarily instructional, for concept-strengthening, not for computation. But the results of the simple lens formula computation are displayed. The monochrome version 13.38 LENS1338.BAS is posted here 3/5/01. Some bugs have been fixed that were in previous versions. This is an older version. It can be run in interpreted BASIC or may be compiled, and will support VGA screens only if your interpreter or compiler can. It has the same functionality as the VGA color version below. It is intended to be compatible with the following computers:
IBM PC, XT, AT with CGA or EGA graphics Tandy 3000 with CGA graphics Xerox 6064 or ATT 6300 high res screen, 640 x 400 Tandy 2000 high res mono, 640 x 400 Sanyo 555, original MS-DOS, 640 x 200 Sanyo 555, with video card, DS-DOS, CGA graphics. Persyst BoB graphics card, 640 x 350 mono.This program has evolved recently, and since I no longer own some of these computers, I cannot test the most recent versions on them. Enough "fossil" code remains in the program that interested users may be able to modify it for their own special computers. Expect the program to run a bit slower on older computers. Color version 14.56 LENS1456.EXE supports only VGA and EGA graphics, and has color enhancements. The code was rewritten with better subroutine structure, but the user won't notice that. See the last section of this document for notes on its special features. The color version of this program does not seem to work with Windows Vista. It generates an error message "this computer does not support full screen mode." The message seems to apply to the command prompt window in which a DOS program runs. If anyone knows a way around this, please let me know. Another reason to curse Microsoft. These new versions have cleaner graphics in the "rayburst" mode, and a cleaner menu layout. They implement for the first time the option of toggling the lens equation on/off the screen, with the equation correctly displayed for either sign convention. If you experience problems, crashes, or odd behavior, check this site to see if a newer version has been posted. If the newer version still has problems, send me e-mail with as much detail as you can. During dull moments I tinker with this thing, and may, without warning, post a newer version. The program first attempts to determine the display adapter type from two peeks at hardware codes. If this fails, the user is manually prompted to select from a menu. The user is also given a chance to override the selection from the menu. A prompt gives several choices for entry to the program after the computer has selected a screen mode. Press "y" to accept the choice and begin the program, "n" to seclect a different screen, "g" to go directly to the program, bypassing the help screen, or "i" to enter an instructor's "demo" mode.
The instructor's demo opens showing only a source of light with many rays coming from it in all directions. This illustrates an isotropic source of light. I call this the "rayburst" effect, and it may be toggled on/off at any time by pressing "*". Pressing any key after this opening screen introduces a converging lens into the picture, and shows how the rays which reach the lens are refracted to form an image. I like to leave the rayburst in place for a while, to remind students that the source gives off many rays which are not affected by the lens. The rayburst effect "goes along" with any changes you make in the size or position of the object. If the object is moved to the right of the lens (virtual object), the rayburst switches off, but returns when the image arrow is placed where it represents a real image. The opening screen.![]() Lens, object, image, and magnification data is displayed at the top of the screen. The type and character of the lens is shown beneath the initial position of the lens. At lower left the character of the object and image is displayed in words. Data displays are refreshed at every change of value of a variable. The opening screen shows the lens shape, and a modest vertical magnification. The lens shape is drawn for a lens of 1.5 index of refraction. No attempt is made to show the rays refracting at the lens surfaces, however. Consider this as an icon representing the lens. The present version does not correct error in the last displayed digit.
Vertical expand.These diagrams seen in textbooks are frequently exaggerated vertically to faciliate manual drawing. Pressing the key "v" toggles a larger vertical magnification. In this mode the lens shape is not drawn; the lens is represented only as a thin line.
![]() The key "h" toggles a smaller horizontal magnification, showing a greater portion of the lens axis. This is useful to see an object or image which is off-screen.
Moving things around.The display may be scrolled left or right with the "," and "." keys. They are easy to remember, for these keys also have the symbols "<" and ">".The object arrow may be moved left/right with the cursor keys. The default movement is 1 axis unit, but movement of .1, .5, 1, and 5 may be selected by pressing 1, 2, 3, and 4 on the keyboard. The selected value is shown one time only at the top right of the screen. If you've scrolled a long way, you can return the lens to default position at the center of the screen by pressing "z" (for "zero").
Object size.The up/down arrows on the keyboard change the size of the object. This illustrates that for a given object position, the image size is proportional to the object size, a consequence of the linearity of the lens equation. The default length is equal to the lens diamter, which is most useful for most purposes, for it ensures that two "special rays" are always displayed.At any time the object size may be set to zero (source on the axis) by pressing "a". The default size is then restored by pressing "b". It may be possible that repeated changes of size by cursor could accumulate round-off error, though I can't seem to make that happen when I deliberately try. But if it happens to you, pressing "a" or "b" should set things right, or press "zero" or "/" to reset defaults.
Direct entry of values.Pressing the following keys gives you a prompt to enter values of these variables:
p or o for the object distance
q or i for the image distance
f for the focal length.
Exponential form may be used for input values (3E5 for example).
The units used are those shown as "hash-marks" on the lens axis.
The position of the focal points are also shown on the axis
with longer hash-marks (blue in the VGA version).
Sign convention and the lens equation.Pressing "s" toggles the sign convention being used for data entry and data display. The opening screen defaults to the "freshman" sign convention. Pressing "s" changes this to the "Cartesian" convention in which everything to the left of the lens is negative and everything to the right of the lens is positive. In this convention the lens equation is 1/p + 1/f = 1/q. The Cartesian convention is often used in upper-level courses. (Does the "freshman" convention have a proper name?)
The Cartesian convention takes everything to the left of the lens as negative, everything to the right as positive. This has the fortunate result that the lens equation, as written above, is more intuitive. 1/p and 1/q represent the convergence of the rays entering and leaving the lens. Negative convergence means the rays are divergent. 1/p represents the power of the lens to change the rays' convergence. Why this sensible convention is not more widely used in introductory physics courses is a mystery to me.Pressing "w" toggles the display of the lens equation at the top right of the screen. The equation is correctly shown for the particular sign convention you have chosen. This helps to remind everyone that this program simulated that equation, the ideal(ized) lens equation, and is not a complete simulation of lenses themselves.
Command menu and graphics menu on/off.The command menu is at the bottom. This menu may be toggled on/off with "c". The data and menu displays may be toggled off/on by pressing "g" (for graphics-only display, useful when doing screen captures for reproduction or when projecting this program on a screen as a classroom aid. For class use, it's useful to be able to suppress the display of object and image characteristics, to ask students to charactrize them as real or virtual.
![]()
It's even possible to suppress all text, for a
display which shows only the drawing. Just press the key
"d". The text is restored by pressing
Rather than allow (or require) entry of f, p and q each
time you make a change, I have adopted a more natural
"single entry" logic.
Here's how it works:
Anything larger than 1E6 (or just E6) is considered infinite. Much care
has been taken in writing this program so that variables
will never have values too large for the basic interpreter or
compiler to deal with without crashing the program. The
program also correctly deals with the case where p=0 and
therefore q=0.
Infinite focal lengths, object distance, or image distances and focal
length may be entered by first pressing the number "8". Think
of it as the infinity symbol seen sidewise. Infinite values of
p, q or f may also be entered as 1E6. An infinite value of
f may be
forced by entering a value of q equal to p.
The "-" key toggles the sign of the lens, positive for
converging, negative for diverging. The "m" key toggles
lens/mirror. The curvature of the mirror is not shown. The
mirror is represented by a straight line.
The left/right scroll feature is especially useful when
simulating mirrors.
The rayburst option may be toggled on/off
at any time with "*". It shows the fact that rays
radiate from the object in many directions, but we only
need and use those which pass through the lens to do the
calculations of objects and images. In fact we need only
two of the three "special" rays and the positions of the
focal points to geometrically locate objects and
images. The rayburst effect is only displayed for real objects.
At any time an "interrupted" mode may be invoked by
pressing "`". Rays are then drawn one segment at a time, waiting
for a key press to continue. When the final image is drawn,
any normal command may be issued. To enter a command at any
other time in this mode, press "e". To exit the interrupted
mode, press "`" again. The interruped ray mode may be useful
for demonstration in an interactive
class setting, but is not yet as user-friendly as I'd like.
Pressing "'" (right single quote) displays dotted lines
which are extensions of the emergent rays. This
is useful for showing how incident convergent rays are aimed towared
the object arrow on the right.
Pressing ";" displays dotted lines which are
extensions of the incident rays. This is useful for showing how
emergent rays seem to radiate from the virtual image to the left.
These rays to the right and left of the lens can be shown simultaneously,
but you don't usually need to. Construction rays are not shown
if the lens is scrolled off-screen. I can't think why you'd want them if
the lens were not shown. The construction rays may also be shown in
the mirror simulation.
This is primarily an option for instructor's demonstrations. It should
be used sparingly, to avoid too much confusing detail on screen.
This mode seems
to me to be of limited use except to show that very distant objects
will produce images nearly in the focal plane. It's an
"almost infinity" simulation. This version works
(I hope) just like all previous versions in all other
respects. Let me know if that is not true. I have not
blocked extreme keyboard entries in this "very distant
object" mode. Some work, some don't, and some may generqate
an "overflow" message and the "Redo from start" message.
The rayburst feature is disabled in "distant" mode.
If you are working in distant mode, where the object length
is huge, and enter a smaller value of p where that size
object would
generate a screen write error, the program puts the object
there but reduces it to the default size. This does not,
however, exit from distant mode. The only way to exit the
distant mode is to press "/".
The up/down arrow keys change the object size by fixed
increments, which is obviously of little use for distant
objects. The [ and ] keys change the object size by a
fixed fraction of p, the object distance, and are primarily
useful for very distant objects, though they are active at
all times, except when p is infinity.
There's an error handler in the code to print a yellow "Inappropriate
input" message in the middle of the screen and prompt for a
keypress, but I haven't seen it appear in a very long while.
Help! This version implements a help screen which shows all of
the commands. It may be brought up at any time by entering
"?", or, in the compiled .EXE version, by pressing F5.
Raytrace mode. Many instructors emphasize manual
construction of ray diagrams using the three 'special' rays:
At long last I have implemented this conceptual mode as an
optional way to display the diagram.
By simply pressing <r> the "raytrace" mode is toggled
on or off. In this mode the command keys *, [, ], =, and \
are disabled. The screen shows just the three special rays,
and you can't change the number of displayed rays.
When in this mode, the lens plane is shown as a vertical dashed
line. As you change the object height, you are magnifying
the paraxial ray situation in the vertical direction, and
rays "refract" at the lens plane. The lens icon
is still shown to remind you what kind of lens you are
using. When a construction ray goes off the top or bottom
of the screen it is not shown. Restore it by making the
object smaller.
Of course the interrupted mode still works in raytrace
mode, which is very valuable as an instructor demo. The ray
through the center is drawn first, then the incident
parallel ray, and finally the incident ray which (if
extended) passes through the appropriate focal point. If
the object distance is smaller than the focal length, the
incident ray direction is extended as a construction
(dotted) line to the focal point.
The raytrace mode is useful for showing what a true
paraxial situation looks line. Just reduce the object size
to the smallest finite value allowed.
In this version I've given users the option of whether the
defaults are reset when toggling to/from raytrace mode. Two
commands do the raytrace toggle. The
command "r" resets the defaults, the command "u" does not.
I prefer "u" most of the time. For technical reasons, I've
disabled the "u" toggle when you are in "distant object"
mode.
The only way to exit the raytrace mode is to press "r" or
"u". The dashed-line lens plane will disappear as confirmation.
Is it worth this modest cost? If you have spent many class periods
at the blackboard juggling two-meter sticks and chalk trying to
construct accurate ray diagrams you needn't even ask that question.
Feedback from registered users encourages
me to spend the time to improve the program and add functionality.
In particular, I would like to:
Since 8/30/01
Revised document, April 23, 2001.
Return to lens tutorial.
|