### Author Topic: Ray tracing for laser light  (Read 24934 times)

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Ray tracing for laser light
« on: March 08, 2010, 01:23:16 pm »
The following simulation is created due to request from Re: Ray tracing (polygon: range from 3-32 faces)

The slider at the top can be used to adjust minimum intensity ray to be drawn/calculated in the simulation.

Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!

The range of the X-Y coordinate system is between -100 and 100.
number of points =Please enter correct number of points
The data points should be enter in clockwise direction!
X= enter , separated x coordinate
Y= enter , separated x coordinate
Laser Pointer x=,y=

How to calculate the reflected light from incoming ray?
Support the vector for the incoming ray is \$vec{a}\$,
We need to know either the component of incoming ray in the normal direction \$vec{b}\$ or the parallel component along the interface \$vec{b}'\$ to find the reflected ray vector \$vec{c}\$ with \$vec{c}=vec{a}-2vec{b}=2vec{b}'-vec{a}\$

If you know the unit vector \$hat{b}\$ , then \$vec{b}=(vec{a}cdothat{b}) hat{b}\$.
If you know the unit vector \$hat{b}'\$, then \$vec{b}'=(vec{a}cdotvec{b})hat{b}'\$.

The refracted angle can be calculated with \$n_1 sin   heta_1=n_2sin   heta_2\$,
where \$sin   heta_1=frac{vec{a}cdothat{b}'}{|vec{a}|}\$ or \$cos   heta_1=frac{vec{a}cdothat{b}}{|vec{a}|}\$

The normal vector \$vec{b}=vec{a}-vec{b}'\$.
The refracted ray can be calculated with \$hat{b},cos   heta_2+hat{b}',sin   heta_2\$

You can study many different cases with this applet. Please check out attached files for examples.

#### liesenbergk

• Newbie
• Posts: 16
##### Re: Ray tracing for laser light
« Reply #1 on: March 11, 2010, 06:17:40 pm »
Dear Professor,

Thanks for your programming efforts. I have been testing the new simulation. What I am missing is the possibility of defining the polygon with coordinates as well as the position of the laser beam. This feature is available in the previous simulation and it is very usefull. I wonder if this could be programmed as well.

Kind regards from Brazil!

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Ray tracing for laser light
« Reply #2 on: March 11, 2010, 09:48:06 pm »
O.K. It is done.

#### liesenbergk

• Newbie
• Posts: 16
##### Re: Ray tracing for laser light
« Reply #3 on: March 11, 2010, 10:50:40 pm »
Thanks Professor, that was more than fast!

I don't want to abuse, but I am missing the drag mode in this new version...

Best regards!

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Ray tracing for laser light
« Reply #4 on: March 12, 2010, 12:14:25 am »
Because the feature has been implemented before, what I did was copy code from previous page.
And I find some free time after I went home tonight.
The drag mode should be available now.

#### liesenbergk

• Newbie
• Posts: 16
##### Re: Ray tracing for laser light
« Reply #5 on: March 12, 2010, 06:43:00 pm »
Wow Professor! You're my idol!!! Thanks for your efforts and congratulations for you outstanding skills.

#### liesenbergk

• Newbie
• Posts: 16
##### Re: Ray tracing for laser light
« Reply #6 on: March 15, 2010, 07:26:24 pm »
Dear Professor,

The new simulation is very useful for me, especially with the possibility of entering the polygon's defining coordinates.

There are, however, some changes which could be very interesting.

The first one (please see attached "laser pen handling" picture) concerns on how to handle the laser pen. As it is now, you can drag the pen to coordinates defines by its basis, and then rotate the pen over the axis of the basis. If you want to follow a particular line you have to, in most cases, drag and rotate, in order to avoid the entering of the laser pen's output in the optical body. It would be very nice if the laser pen can be dragged and rotated based on the coordinates of its beam output. It would also be very nice if the laser pen's angle could be entered, as shown in the related picture.

The other wished enhancement is the possibility of exporting ray data (please see attached "desired ray data" picture). Here the defining coordinates of the way of the primary beam would be outputted, if possible with its final intensity too.

I will understand if you don't have time or if this is to complicated to do. But,... it would be nice!

With best regards!

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Ray tracing for laser light
« Reply #7 on: March 18, 2010, 11:02:35 pm »
I justed modified the code so that a new window will popup when you click the arrow of laser pointer or click the check box labeled "table".
You can change the angle with slider or text field.

I also add a table to show a list of (x,y,I) which represent all rays (include reflected ray and refracted ray).
You should be able to find which one is reflected ray, which one is refracted ray from intensity.

#### liesenbergk

• Newbie
• Posts: 16
##### Re: Ray tracing for laser light
« Reply #8 on: March 19, 2010, 06:10:57 pm »
That is stunning, Master! Thanks a lot. The data table is very useful.

Kind regards, Professor!

#### tmzoughi

• Newbie
• Posts: 9
##### Re: Ray tracing for laser light
« Reply #9 on: March 28, 2010, 11:42:40 pm »
This is wonderful simulation and it offers many possibilities. I tried to use it for a particular geometry and I think I have detected a small bug. Or it might be an opportunity for me to learn something new!
I am attaching an image of the setting I am using. This is the output of the lines generated:
j=0,I=1.0,c=1.7669502000928863,x=-75.51111260566398,y=-21.117714323462188
j=0,I=1.0,c=0.26179938779914935,x=-75.51111260566398,y=-21.117714323462188
j=1,I=0.057796105403213144,c=-1.8325957145940464,x=-15.075862213492771,y=-4.924137786507226
j=2,I=0.9422038945967869,c=0.44556125394332635,x=-15.075862213492771,y=-4.924137786507226
j=3,I=0.9422038945967869,c=-0.4455612539433263,x=58.049565975939494,y=30.0
j=4,I=0.0733637418622893,c=-2.6960313996464667,x=100.0,y=9.964797822930265
j=5,I=0.8688401527344976,c=-0.18820469334913387,x=100.0,y=9.964797822930265
j=6,I=0.0733637418622893,c=2.6960313996464667,x=16.320254742697305,y=-29.999999999999993
j=7,I=0.02264313020441028,c=2.016357580738223,x=4.221937396573228,y=-24.221937396573228
j=8,I=0.05072061165787902,c=2.1447458012139355,x=4.221937396573228,y=-24.221937396573228
j=9,I=0.00118179551253197,c=-2.016357580738223,x=-21.67403968794734,y=29.999999999999996
j=10,I=0.02146133469187831,c=2.1705020069039946,x=-21.67403968794734,y=29.999999999999996
j=11,I=0.006915452246752739,c=-2.1447458012139355,x=-30.83551657260938,y=30.0
j=12,I=0.04380515941112628,c=1.7669502000928863,x=-30.83551657260938,y=30.0

The first bug seems to be related to the ray corresponding to j=5. Since this represents a beam going from n=1.5 to n=1, the refracted beam should be larger than what what is shown. The same problem happens to beam j=8 when it refracts into beam j=12. The problem does not happen for beam j=7!
The other item I don't understand is the fact that we have two beams reflected at x=4.221937396573228,y=-24.221937396573228 (j=7 and j=8)
Thanks for your help clarifying this and for all of your amazing work.

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Ray tracing for laser light
« Reply #10 on: March 29, 2010, 12:18:07 am »
Could you give me the coordinates for those polygon points and light source and angle to test it out!

#### tmzoughi

• Newbie
• Posts: 9
##### Re: Ray tracing for laser light
« Reply #11 on: March 29, 2010, 01:07:33 am »
Thanks. I have attached the xml file that I have used.
Taha

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: Ray tracing for laser s
« Reply #12 on: March 29, 2010, 11:42:47 am »
Yes. It is a bug. Thank you for reporting it.
The last ray should be a total reflection ray. However, the ray was treated emmitted from air to glass, so the code did something wrong.
It is fixed now.

The above simulation was designed such that you can use javascript to change the shape of the polygon (px,py) and the position of laser point(xs,ys).

You do not need to re-compile the EJS. For you case :
n=4;
px=10,-50,100,100
py=-30,30,30,-30
xs=-90
ys=-25

Suppose the name for the EJS applet is "polygonsn". The following two javascript call will set up the above configuration.
document.polygonsn._setVariables("n=4;px=10,-50,100,100;py=-30,30,30,-30;xs=-90;ys=-25");//set up the above parameters
document.polygonsn._model.setit();// Some parameters need to re-calculate according to the above change,this is the method defined as Custom code
EJS variables are decleared as global variables, so you can change it with _setVariables (";" separated variable assignment , use "," to separate indicidual array (variable) values.

I just added another option so that user can edit coordinate for polygon in the popup window (when table is checked).
There is a second tabbed panel which display coordinate for the polygon points (it is editable).
However, only the fist N rows are useful (You can adjust N with slider).

#### tmzoughi

• Newbie
• Posts: 9
##### Re: Ray tracing for laser light
« Reply #13 on: March 29, 2010, 05:49:51 pm »
Thanks. I knew I could modify it with JavaScript but I needed it to pop up for students under that particular configuration. I prefer that they ran it as a JAR. They then can play around and do whatever they want. Thanks for your help with this and all of your great work.

Taha
« Last Edit: March 29, 2010, 05:52:08 pm by tmzoughi »

#### tmzoughi

• Newbie
• Posts: 9
##### Re: Ray tracing for laser light
« Reply #14 on: March 29, 2010, 06:28:13 pm »
I just noticed that there is still a problem with the refraction. It still behaves like from air to glass.

Taha