2-D Ray Tracing

조회 수: 36 (최근 30일)
Maureen Mendez
Maureen Mendez 2018년 2월 19일
이동: Walter Roberson 2023년 7월 9일
Hi everyone, I have been tackling a problem in regards to ray tracing in 2-dimensions and havent been able to find anything helpful to help me start the program I want. My goal is to be able to plot a ray in a confined space that reflects the ray, but each time it hits a wall the ray decays a little until it vanishes eventually. I've been able to plot a ray and a "wall", but making the actual reflecting line is what's troubling me. I have found the point of intersection of 2 lines, and the angle I want the reflection to come out at (supw1). Any help to this problem is greatly appreciated, I essentially want to be able to make the ray "laser" bounce around like the old fashioned DVD logo from back in the day. Attached is the Matlab code I have so far. Thanks in advanced for the help, feel free if you have any questions for clarification.
%line1
x1 = [2 3];
y1 = [1 1];
%line2
x2 = [2 10];
y2 = [0 10];
%fit linear polynomial
p1 = polyfit(x1,y1,1);
p2 = polyfit(x2,y2,1);
% p3 = polyfit(x3,y3,1);
% calculate intersection
x_intersect = fzero(@(x) polyval(p1-p2,x),3);
y_intersect = polyval(p1,x_intersect);
% function drawLine(point,slope)
% % point - vector [x,y]
% % slope - slope of the line
%
% x = x_intersect;
% y = y_intersect;
%
% lengthLine = 5;
%
% xLine = x-lengthLine:x+lengthLine;
% yLine = slope*(xLine-x) + y;
%
% plot(x,y,'ro')
% plot(xLine,yLine,'b')
%
% end
intlines = 1;
% if intlines == 1
% plot(
line(x1,y1);
hold on;
line(x2,y2);
plot(x_intersect,y_intersect, 'ro', 'Markersize', 18)
rectangle('Position',[1 1 11 11])
diff = (atan((y1(2)-y1(1))/(x1(2)-x1(1))) - atan((y2(2)-y2(1))/(x2(2)-x2(1)))) * 180/pi;
supw1 = abs(diff / 2);
  댓글 수: 2
Gilcimar Florindo de Souza
Gilcimar Florindo de Souza 2020년 5월 24일
Hello, I need an algorithm that executes the commands below.
You will apply the ray tracing technique to this variable (which contains the scene object). For
this, you will write the algorithm that performs ray tracing.
Thus, the only input parameter of your algorithm must be the variable X containing the object
of the scene (which you’ll upload from the ‘.mat’ file).
The only output parameter must be the two-dimensional image produced, corresponding to the
orthographic parallel projection of the object on the plane using the ray tracing technique.
Sunaina
Sunaina 2023년 7월 8일
이동: Walter Roberson 2023년 7월 9일
how to ray-trace already plotted graph having power density vs distance of base station antenna?

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2018년 2월 20일
Let P1=[x1,y1] and P2=[x2,y2] be the two endpoints of a line segment of the reflecting "wall", and let P3 = [x3,y3] be a point on a ray with vector v = [v1,v2] pointing from P3 along the ray. Assume that the ray will intersect the given line segment. The point of intersection, P4, of the ray with the wall segment will be:
t = ((y2-y3)*v1-(x2-x3)*v2)/((x1-x2)*v2-(y1-y2)*v1);
P4 = t*P1+(1-t)*P2;
[Note #1: If t lies outside the interval [0,1], then the ray doesn't actually intersect the segment.]
The point, P5, at the mirror image of P3 with respect to the line perpendicular to the segment at P4 will be:
P5 = P3+2*dot(P4-P3,P2-P1)/dot(P2-P1,P2-P1)*(P2-P1);
Thus, the reflected ray proceeds from P4 and goes through P5.
[Note #2: This just gives the direction of the reflected ray from P4 toward P5. It doesn't matter if P5 lies outside the other parts of your "wall".]
  댓글 수: 3
Sahil Kalra
Sahil Kalra 2019년 3월 26일
For 3D we can use surfnorm command and get the normal; thereafter the same process.
Aknur
Aknur 2023년 2월 22일
@Sahil Kalra Hello! Thank you for your suggestion for 3d. Could you please to write more detail about to find intersection point of line and plain. Thank you in advance. BR, Aknur

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by