Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.65 KB | None | 0 0
  1. close all;
  2. clear all;
  3. clc;
  4.  
  5. width = 10;
  6. height = width;
  7.  
  8. % Set up figure window, plot the grid
  9. figure
  10. hold on;
  11. xlim([-width/2 width/2])
  12. ylim([-height/2 height/2])
  13. xticks([-width/2:1:width/2])
  14. yticks([-height/2:1:height/2])
  15. axis square
  16. grid on;
  17.  
  18. turningPoints = [];
  19. turningPoints(:,:,1) = [0.5, 0.1];
  20. direction_rad = pi;
  21.  
  22. m = 0;
  23. b = 0.1;
  24.  
  25. V = 0;
  26. L = 0;
  27.  
  28. plot(turningPoints(1,1,1), turningPoints(1,2,1), 'or');
  29. % Plot the "mirrors" (i.e. plot circles)
  30. for x = [-width/2:1:width/2]
  31. for y = [-height/2:1:height/2]
  32. c = viscircles([x y],1/3,'Color','K');
  33. fill(c.Children(1).XData(1:end-1), c.Children(1).YData(1:end-1), [0.7294 0.7294 0.7294]);
  34. end
  35. end
  36.  
  37. intersection = cl_intersection(V, L, m, b);
  38.  
  39. line([turningPoints(1,1,1), intersection(1,1,1)],[turningPoints(1,2,1), intersection(1,2,1)], 'Color', [61/255 165/255 244/255], 'LineStyle', '--', 'LineWidth', 2);
  40.  
  41. distanceTravelled = 0;
  42.  
  43. angle=pi;
  44. globalAngle = angle;
  45.  
  46. while (distanceTravelled < 10)
  47. sinAngle = sin(globalAngle)
  48. cosAngle = cos(globalAngle)
  49. if (sinAngle < 2e-16 && sinAngle > -2e-16)
  50. sinAngle = 0;
  51. end
  52. if (cosAngle < 2e-16 && cosAngle > -2e-16)
  53. cosAngle = 0;
  54. end
  55.  
  56.  
  57.  
  58. % from the previous turning point, determine which mirror is the next one to be hit by our laser
  59. if (cosAngle < 0) %if we are moving left
  60. if (sinAngle < 0) % if we are moving left AND down
  61.  
  62. elseif (sinAngle > 0) % if we are moving left AND up
  63. % start looking for first intersection
  64.  
  65. %check the mirror directly above
  66. intersection = cl_intersection(V + 1, L, m, b);
  67.  
  68. % if there was no intersection, check the mirror to the left
  69. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  70. intersection = cl_intersection(V, L - 1, m, b);
  71. end
  72.  
  73. %check the remaining possible mirrors
  74. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  75. for i = 1:1:20
  76. intersection = cl_intersection(V + 1, L - i, m, b);
  77. if (~isreal(intersection(:,:,1)))
  78. if (~isreal(intersection(:,:,2)))
  79. intersection = cl_intersection(V + 1 + i, L - 1, m, b);
  80. continue;
  81. end
  82. end
  83.  
  84. d1 = sqrt((turningPoints(1,1,end) - intersection(1,1,end-1))^2 + (turningPoints(1,2,end) - intersection(1,2,end-1))^2);
  85. d2 = sqrt((turningPoints(1,1,end) - intersection(1,1,end))^2 + (turningPoints(1,2,end) - intersection(1,2,end))^2);
  86.  
  87. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  88. turningPoints = [turningPoints; intersection(:,:,end-1)];
  89. else % d2 is closer than d1
  90. turningPoints = [turningPoints; intersection(:,:,end)];
  91. end
  92. break;
  93. end
  94. end
  95.  
  96.  
  97.  
  98. else % we are not moving up or down (purely leftwards)
  99. cx = floor(turningPoints(1,1,end));
  100. cy = floor(turningPoints(1,2,end));
  101. intersection = cl_intersection(cx, cy, m, b);
  102.  
  103. d1 = sqrt((turningPoints(1,1,end) - intersection(1,1,end-1))^2 + (turningPoints(1,2,end) - intersection(1,2,end-1))^2);
  104. d2 = sqrt((turningPoints(1,1,end) - intersection(1,1,end))^2 + (turningPoints(1,2,end) - intersection(1,2,end))^2);
  105.  
  106. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  107. turningPoints = [turningPoints; intersection(:,:,end-1)];
  108. else % d2 is closer than d1
  109. turningPoints = [turningPoints; intersection(:,:,end)];
  110. end
  111.  
  112. end
  113. elseif (cosAngle > 0)
  114. if (sinAngle < 0) % if we are moving right AND down
  115.  
  116.  
  117.  
  118. % start looking for first intersection
  119.  
  120. %check the mirror directly below
  121. intersection = cl_intersection(V - 1, L, m, b);
  122.  
  123. % if there was no intersection, check directly to the right
  124. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  125. intersection = cl_intersection(V, L + 1, m, b);
  126. end
  127.  
  128. %check the remaining possible mirrors
  129. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  130. for i = 1:1:20
  131. intersection = cl_intersection(V - 1, L + i, m, b);
  132. if (~isreal(intersection(:,:,1)))
  133. if (~isreal(intersection(:,:,2)))
  134. intersection = cl_intersection(V - i, L + 1, m, b);
  135. continue;
  136. end
  137. end
  138.  
  139. d1 = sqrt((turningPoints(1,1,end) - intersection(1,1,end-1))^2 + (turningPoints(1,2,end) - intersection(1,2,end-1))^2);
  140. d2 = sqrt((turningPoints(1,1,end) - intersection(1,1,end))^2 + (turningPoints(1,2,end) - intersection(1,2,end))^2);
  141.  
  142. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  143. turningPoints = [turningPoints; intersection(:,:,end-1)];
  144. else % d2 is closer than d1
  145. turningPoints = [turningPoints; intersection(:,:,end)];
  146. end
  147. break;
  148. end
  149. else
  150. d1 = sqrt((turningPoints(end, 1, :) - intersection(1,1,end-1))^2 + (turningPoints(end, 2, :) - intersection(1,2,end-1))^2);
  151. d2 = sqrt((turningPoints(end, 1, :) - intersection(1,1,end))^2 + (turningPoints(end, 2, :) - intersection(1,2,end))^2);
  152.  
  153. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  154. turningPoints = [turningPoints; intersection(:,:,end-1)];
  155. else % d2 is closer than d1
  156. turningPoints = [turningPoints; intersection(:,:,end)];
  157. end
  158.  
  159. end
  160.  
  161.  
  162.  
  163. elseif (sinAngle > 0) % if we are moving right AND up
  164. % start looking for first intersection
  165.  
  166. %check the mirror directly above
  167. intersection = cl_intersection(V + 1, L, m, b);
  168.  
  169. % if there was no intersection
  170. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  171. intersection = cl_intersection(V, L + 1, m, b);
  172. end
  173.  
  174. %check the remaining possible mirrors
  175. if (~isreal(intersection(:,:,1)) && ~isreal(intersection(:,:,2)))
  176. for i = 1:1:20
  177. intersection = cl_intersection(V + 1, L + 1 + i, m, b);
  178. if (~isreal(intersection(:,:,1)))
  179. if (~isreal(intersection(:,:,2)))
  180. intersection = cl_intersection(V + 1 + i, L + 1, m, b);
  181. continue;
  182. end
  183. end
  184.  
  185. d1 = sqrt((turningPoints(1,1,end) - intersection(1,1,end-1))^2 + (turningPoints(1,2,end) - intersection(1,2,end-1))^2);
  186. d2 = sqrt((turningPoints(1,1,end) - intersection(1,1,end))^2 + (turningPoints(1,2,end) - intersection(1,2,end))^2);
  187.  
  188. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  189. turningPoints = [turningPoints; intersection(:,:,end-1)];
  190. else % d2 is closer than d1
  191. turningPoints = [turningPoints; intersection(:,:,end)];
  192. end
  193. break;
  194. end
  195. else
  196. d1 = sqrt((turningPoints(1,1,end) - intersection(1,1,end-1))^2 + (turningPoints(1,2,end) - intersection(1,2,end-1))^2);
  197. d2 = sqrt((turningPoints(1,1,end) - intersection(1,1,end))^2 + (turningPoints(1,2,end) - intersection(1,2,end))^2);
  198.  
  199. if (d1 < d2) % if d1 is closer than d2, d1 is our intersection
  200. turningPoints = [turningPoints; intersection(:,:,end-1)];
  201. else % d2 is closer than d1
  202. turningPoints = [turningPoints; intersection(:,:,end)];
  203. end
  204.  
  205. end
  206.  
  207. else % we are not moving up or down (purely rightwards)
  208.  
  209. end
  210.  
  211. else
  212. if (sinAngle < 0) % if we are moving purely downwards
  213.  
  214. elseif (sinAngle > 0) % if we are moving purely upwards
  215.  
  216. end
  217.  
  218. end
  219.  
  220. % calculate the derivative at the point the laser hit the mirror
  221. mirrorX = turningPoints(end,1,:);
  222. mirrorY = turningPoints(end,2,:);
  223.  
  224. if (mirrorY > round(mirrorY)) %if we hit one of the top quadrants
  225. if(mirrorX > round(mirrorX)) %top right quadrant
  226. derivative = -(mirrorX - round(mirrorX))/sqrt((1/3)^2 - (mirrorX - round(mirrorX))^2);
  227. b = mirrorY - derivative * mirrorX % rearranging y = mx + b
  228.  
  229. % choose x values either side of the x-value where our laser
  230. % hits the mirror (this is for plotting the tangent line)
  231. x1 = mirrorX + 0.05;
  232. x2 = mirrorX - 0.05;
  233.  
  234. % calculate the corresponding y values for plotting the tangent
  235. % line
  236. y1 = derivative*x1 + b;
  237. y2 = derivative*x2 + b;
  238.  
  239. %plot the tangent line
  240. line([x1, x2],[y1, y2], 'Color', [61/255 165/255 244/255], 'LineStyle', '--', 'LineWidth', 1);
  241.  
  242. % 90 degree rotation
  243. transformationMatrix = [0 1;-1 0];
  244.  
  245. % vector that is rotated 90 degrees (i.e. orthogonal to) the
  246. % tangent line (subtract mirrorX and mirrorY so we rotate around
  247. % origin, add them back for plotting in the next line)
  248. % orthogonalVector = transformationMatrix*[x2-mirrorX ; y2-mirrorY];
  249. % line([mirrorX, orthogonalVector(1)+mirrorX],[mirrorY, orthogonalVector(2)+mirrorY], 'Color', [61/255 165/255 244/255], 'LineStyle', '-', 'LineWidth', 2);
  250.  
  251.  
  252.  
  253. nx1 = mirrorX + 0.35;
  254. nx2 = mirrorX - 0.35;
  255.  
  256. nm = -1/derivative;
  257. nb = mirrorY - nm * mirrorX % rearranging y = mx + b - this b value is the b value for plotting our tangent line
  258. ny1 = nm*nx1 + nb;
  259. ny2 = nm*nx2 + nb;
  260.  
  261. line([nx1, nx2],[ny1, ny2], 'Color', [61/255 165/255 244/255], 'LineWidth', 0.5);
  262.  
  263.  
  264. V = round(mirrorY);
  265. L = round(mirrorX);
  266.  
  267.  
  268. % the magnitude (i.e. positive value) of the angle between the incident ray, and the vector orthogonal to the tangent line
  269. % (the blue theta in my book)
  270. angleMag = pi/2 - abs(atan(derivative));
  271.  
  272. % the new angle (i.e. the angle of the reflected ray)
  273. angle = atan(derivative) + pi/2 + (pi/2 - abs(atan(derivative)))
  274.  
  275. angleMag = abs(atan(nm) - atan(m));
  276.  
  277. angle = atan(nm) + angleMag;
  278. globalAngle = angle;
  279. m = tan(angle); % our m value in y = mx + b
  280. % transformationMatrix = [cos(angle) -sin(angle); sin(angle) cos(angle)]
  281. b = mirrorY - m*mirrorX; % rearrange y = mx + b
  282. % transformationMatrix = -1*[cos(atan(1/derivative)) sin(atan(1/derivative));-sin(atan(1/derivative)) cos(atan(1/derivative))];
  283. % newVector = transformationMatrix*[orthogonalVector(1)-mirrorX ; orthogonalVector(2)-mirrorY];
  284. line([0, mirrorX+3],[b,m*(mirrorX+3) + b], 'Color', [244/255 165/255 244/255], 'LineWidth', 0.5);
  285. xlim([-1 3])
  286. ylim([-1 3])
  287.  
  288.  
  289. else % we hit the top left quadrant
  290.  
  291. derivative = -(mirrorX - round(mirrorX))/sqrt((1/3)^2 - (mirrorX - round(mirrorX))^2); % slope of the line that runs tangent to the point we hit
  292. b = mirrorY - derivative * mirrorX % rearranging y = mx + b - this b value is the b value for plotting our tangent line
  293.  
  294. % choose x values either side of the x-value where our laser
  295. % hits the mirror (this is for plotting the tangent line)
  296. x1 = mirrorX + 0.05;
  297. x2 = mirrorX - 0.05;
  298. V = round(mirrorY);
  299. L = round(mirrorX);
  300.  
  301. % calculate the corresponding y values for plotting the tangent
  302. % line
  303. y1 = derivative*x1 + b;
  304. y2 = derivative*x2 + b;
  305.  
  306. %plot the tangent line
  307. line([x1, x2],[y1, y2], 'Color', [61/255 165/255 244/255],'LineWidth', 0.5);
  308.  
  309. % 90 degree rotation
  310. transformationMatrix = [0 1;-1 0];
  311.  
  312. % vector that is rotated 90 degrees (i.e. orthogonal to) the
  313. % tangent line (subtract mirrorX and mirrorY so we rotate around
  314. % origin, add them back for plotting in the next line)
  315. orthogonalVector = transformationMatrix*[x2-mirrorX ; y2-mirrorY];
  316. line([mirrorX, orthogonalVector(1)+mirrorX],[mirrorY, orthogonalVector(2)+mirrorY], 'Color', [255/255 0/255 0/255], 'LineWidth', 0.5);
  317.  
  318.  
  319.  
  320.  
  321. nx1 = mirrorX + 0.35;
  322. nx2 = mirrorX - 0.35;
  323.  
  324. nm = -1/derivative;
  325. nb = mirrorY - nm * mirrorX % rearranging y = mx + b - this b value is the b value for plotting our tangent line
  326. ny1 = nm*nx1 + nb;
  327. ny2 = nm*nx2 + nb;
  328.  
  329. line([nx1, nx2],[ny1, ny2], 'Color', [61/255 165/255 244/255], 'LineWidth', 0.5);
  330.  
  331.  
  332. V = round(mirrorY);
  333. L = round(mirrorX);
  334. % angleMag = abs((atan(m) + pi/2 - atan(derivative)));
  335.  
  336. angle = (atan(m) + pi/2 - atan(derivative));
  337. if (angle > 0)
  338. globalAngle = pi - atan(m);
  339. end
  340. % angle = (pi/2 + 2*abs(atan(derivative)))
  341. m = tan(atan(nm) + pi - angle);
  342. % transformationMatrix = [cos(angle) -sin(angle); sin(angle) cos(angle)]
  343. b = mirrorY - m*mirrorX; % rearrange y = mx + b
  344. % transformationMatrix = -1*[cos(atan(1/derivative)) sin(atan(1/derivative));-sin(atan(1/derivative)) cos(atan(1/derivative))];
  345. % newVector = transformationMatrix*[orthogonalVector(1)-mirrorX ; orthogonalVector(2)-mirrorY];
  346. line([0, mirrorX+3],[b,m*(mirrorX+3) + b], 'Color', [244/255 165/255 244/255], 'LineWidth', 0.5);
  347.  
  348.  
  349.  
  350.  
  351. % m = 1/tan (angle);
  352. % transformationMatrix = [cos(angle) -sin(angle); sin(angle) cos(angle)]
  353. % b = mirrorY - m*mirrorX; % rearrange y = mx + b
  354. % transformationMatrix = -1*[cos(atan(1/derivative)) sin(atan(1/derivative));-sin(atan(1/derivative)) cos(atan(1/derivative))];
  355. % newVector = transformationMatrix*[orthogonalVector(1)-mirrorX ; orthogonalVector(2)-mirrorY];
  356. % line([0, mirrorX+3],[b,m*(mirrorX+3) + b], 'Color', [244/255 165/255 244/255], 'LineWidth', 0.5);
  357. end
  358.  
  359.  
  360.  
  361. else % we hit one of the bottom quadrants
  362. if (mirrorX > round(mirrorX)) % we hit the bottom right quadrant
  363. derivative = (mirrorX - round(mirrorX))/sqrt((1/3)^2 - (mirrorX - round(mirrorX))^2);
  364. b = mirrorY - derivative * mirrorX % rearranging y = mx + b
  365.  
  366. % choose x values either side of the x-value where our laser
  367. % hits the mirror (this is for plotting the tangent line)
  368. x1 = mirrorX + 0.35;
  369. x2 = mirrorX - 0.35;
  370.  
  371. % calculate the corresponding y values for plotting the tangent
  372. % line
  373. y1 = derivative*x1 + b;
  374. y2 = derivative*x2 + b;
  375.  
  376. %plot the tangent line
  377. line([x1, x2],[y1, y2], 'Color', [61/255 165/255 244/255], 'LineWidth', 0.5);
  378.  
  379. % 90 degree rotation
  380. transformationMatrix = [0 1;-1 0];
  381.  
  382. % vector that is rotated 90 degrees (i.e. orthogonal to) the
  383. % tangent line (subtract mirrorX and mirrorY so we rotate around
  384. % origin, add them back for plotting in the next line)
  385. % orthogonalVector = -1*transformationMatrix*[x2-mirrorX ; y2-mirrorY];
  386. % line([mirrorX, orthogonalVector(1)+mirrorX],[mirrorY, orthogonalVector(2)+mirrorY], 'Color', [61/255 165/255 244/255], 'LineStyle', '-', 'LineWidth', 2);
  387. V = round(mirrorY);
  388. L = round(mirrorX);
  389.  
  390. nx1 = mirrorX + 0.35;
  391. nx2 = mirrorX - 0.35;
  392.  
  393. nm = -1/derivative;
  394. nb = mirrorY - nm * mirrorX % rearranging y = mx + b - this b value is the b value for plotting our tangent line
  395.  
  396. ny1 = nm*nx1 + nb;
  397. ny2 = nm*nx2 + nb;
  398.  
  399. line([nx1, nx2],[ny1, ny2], 'Color', [61/255 165/255 244/255], 'LineWidth', 0.5);
  400.  
  401. % the magnitude (i.e. positive value) of the angle between the incident ray, and the vector orthogonal to the tangent line
  402. % (the blue theta in my book)
  403. angleMag = abs(atan(nm) - atan(m));
  404.  
  405. % the new angle (i.e. the angle of the reflected ray)
  406. % angle = -(atan(m) + pi/2 - atan(derivative));
  407. angle = atan(nm) - angleMag;
  408. if (angle < 0)
  409. globalAngle = atan(m);
  410. end
  411. % angle = (pi/2 + 2*abs(atan(derivative)))
  412. m = tan(angle);
  413. % transformationMatrix = [cos(angle) -sin(angle); sin(angle) cos(angle)]
  414. b = mirrorY - m*mirrorX; % rearrange y = mx + b
  415. globalAngle = angle;
  416.  
  417. % transformationMatrix = [cos(angle) -sin(angle); sin(angle) cos(angle)]
  418. % transformationMatrix = -1*[cos(atan(1/derivative)) sin(atan(1/derivative));-sin(atan(1/derivative)) cos(atan(1/derivative))];
  419. % newVector = transformationMatrix*[orthogonalVector(1)-mirrorX ; orthogonalVector(2)-mirrorY];
  420. line([0, mirrorX+3],[b,m*(mirrorX+3) + b], 'Color', [244/255 165/255 244/255], 'LineWidth', 0.5);
  421. xlim([-1 3])
  422. end
  423.  
  424. end
  425. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement