0


I am having troubles trying to run the following code. It is meant to calculate a series of rays reflections inside a closed room (2D).
Collision[] is a function that returns the intersection point (ray hit) with the closest object/wall.
p = {10,0} is the source location.
line is a list contains {a,b} where y=ax+b is the ray's line equation.

Code:

path[line_, k_] :=  ( 
  p = {10, 0};
   l = {p};
  line1 = line;
  For[i = 1, i <= k, i = i + 1, 
    p = collision[line1, p];
    AppendTo[l, p];
    If[9.5 <= p[[1]] <= 10.5 && p[[2]] == 15 , Break[], 
     line1 = {-1/line1[[1]], p[[2]] - -1/line1[[1]]*p[[1]]}]
     ]
   l
  )


Calling this function with the needed parameters:
path[{1, -10}, 2]
Returns the output:
{{10 Null, 0}, {20 Null, 10 Null}, {0, 30 Null}}
While the expected output is supposed to be:
{{10, 0}, {20, 10}, {0, 30}}
Where did I go wrong?
Any help will be appreciated!

kobibo
  • 215
  • 1
  • 4

1 Answers1

1

rhermans has it right: You forgot a semicolon. What's happening is that the For[...] evaluates to Null. Since you left the ; out before the last l, the result you're getting is the product of Null and l.