ASCII reflections in a box
You probably all know the Law of Reflection, in this challenge you'll visualize the trajectory of a ball in a box.
Related: ASCII Ball in Box Animation and ASCII Doodling: Laser in a Box
Task
You're given three integer pairs W,H
, x,y
and dx,dy
- the first represents the size of the box, the second the starting position and the third pair is the direction in which the ball starts moving.
The task is to visualize the movement of the ball until it stops rolling, this happens as soon as the ball is at a position that it was before or it hits a corner.
The character *
shall visualize the trajectory of the ball and +
marks its final position, the rest of the box must consist of (whitespace).
Examples
To lay it out a bit clearer, in these examples _
will represent a whitespace. Also the intermediate stages are only here for clarification, you'll only need to output the last stage, these examples are 1
-indexed.
Given W = 3, H = 5
, x = 3, y = 2
and dx = -1, dy = 1
:
___ ___ ___ ___
__* __* __* __*
___ -> _*_ -> _*_ -> _+_
___ *__ *__ *_*
___ ___ _*_ _*_
- Ball starts at point
(3,2)
and - moves in direction
(-1,1)
, hits the wall at(1,4)
and - gets reflected, new direction is
(1,1)
. It hits the wall again at(2,5)
- where it gets gets reflected. The new direction is
(1,-1)
and it hits the wall immediately at(3,4)
, - again it gets reflected into the direction
(-1,-1)
. It would now travel through points(2,3),(1,2)
, reflected etc. but since it already visited the position(2,3)
it stops there.
This example demonstrates, what happens if a ball hits a corner. For this let W = 7, H = 3
, x = 1, y = 3
and dx = 1, dy = -1
:
_______ __*____ __*____ __*___+
_______ -> _*_____ -> _*_*___ -> _*_*_*_
*______ *______ *___*__ *___*__
- Start position is
(1,3)
, - the ball now travels in direction
(1,-1)
until it hits the wall at(3,1)
- where it gets reflected into the new direction
(1,1)
. - At
(5,3)
it gets reflected and travels into the new direction(1,-1)
. It comes to an abrupt stop at(7,1)
because that's a corner.
Given W = 10, H = 6
, x = 6, y = 6
and dx = 1, dy = 1
:
__________ __________ ________*_ ________*_ ________*_ __*_____*_ __*_____*_
__________ _________* _________* _______*_* _______*_* _*_____*_* _*_*___*_*
__________ -> ________*_ -> ________*_ -> ______*_*_ -> *_____*_*_ -> *_____*_*_ -> *___*_*_*_
__________ _______*__ _______*__ _____*_*__ _*___*_*__ _*___*_*__ _*___+_*__
__________ ______*___ ______*___ ____*_*___ __*_*_*___ __*_*_*___ __*_*_*___
_____*____ _____*____ _____*____ ___*_*____ ___*_*____ ___*_*____ ___*_*____
Input specification
The input consists of the three integer pairs W,H
, x,y
and dx,dy
, you may take input in any format that makes most sense for your programming language and the order doesn't matter. However the accepted input must not encode more information than these pairs contain (see this answer for an example).
W,H >= 1
x,y
are either1
-indexed (1 <= x <= W
and1 <= y <= H
) or0
-indexed (0 <= x < W
and0 <= y < H
), please specify what indexing you chosedx,dy
are always either-1
or1
Invalid input can be ignored.
Output specification
- No leading whitespaces are allowed
- Trailing whitespaces may be omitted
- Trailing whitespaces are not allowed if they don't fit the box
- Trailing newlines (after all output related lines) are allowed
Let's take the first example:
(good by 2)
__*
_+ (good by 2)
*_*_ (bad by 3)
(bad by 4)
_*_
(good by 4)
Test cases
Assuming the input has the format (W,H,x,y,dx,dy)
and 1
-indexing was chosen, here are some test cases (again _
is here to represent whitespaces!):
Input: 1,1,1,1,1,1
Output:
+
Input: 3,3,3,3,1,1
Output:
___
___
__+
Input: 3,3,3,3,-1,-1
Output:
+__
_*_
__*
Input: 7,3,1,3,1,-1
Output:
__*___+
_*_*_*_
*___*__
Input: 10,6,6,6,1,1
Output:
__*_____*_
_*_*___*_*
*___*_*_*_
_*___+_*__
__*_*_*___
___*_*____
Input: 21,7,6,4,-1,-1
Output:
__*_______*_______*__
_*_*_____*_*_____*_*_
*___*___*___*___*___*
_*___*_*_____*_*___*_
__*___*_______+___*__
___*_*_________*_*___
____*___________*____
This is code-golf, so the shortest program/function wins, but any effort is appreciated.
1
-indexed constraint if you like(?) About that first question, not sure what you mean but it sounds ok. I guess anything that is a bijection ({-1,1}x{-1,1} ≡ "Your space"
) would be good. \$\endgroup\$