#MATL, 35 34 characters
MATL, 35 34 characters
Z"42Gi:qi!+lGqE!/tYo-KM!*|QYoZ}X]( ###Explanation
Explanation
This takes care of the reflections using the formulas
nx = 2*(Nx-1) px = abs(t/nx-round(t/nx))*nx where t is a time variable that increases in steps of 1, Nx is the maximum size along the x direction and px is the computed x position. Analogous formulas are used for the y coordinate.
A char matrix of spaces is initially created, and then it is filled with character * at the computed x and y positions.
Z" % Implicitly take [width height]. Push char matrix of spaces with that size 42 % Push 42. ASCII for character `*` G % Push first input again, that is, [width height] i % Take number of movements, M :q % Range [0 1 ... M-1] i! % Take initial position [x y]. Transpose into column vector + % Add element-wise with broadcast. Gives 2-row vector lG % Push first input, [width height] qE! % Transform into column vector [2*width-2; 2*height-2] / % Divide element-wise with broadcast tYo % Duplicate. Round to closest integer - % Subtract KM! % Push [2*width-2; 2*height-2] again * % Multiply element-wise with broadcast | % Absolute value Q % Add 1 Yo % Round to closest integer Z} % Separate 2-row array into two row arrays. These contain row and column % indices, respectively, of the ball positions X] % Convert row and colummn indices to linear index ( % Assign 42 to those positions of the matrix of spaces