Take a 2-dimensional grid and draw a number of line segments on it to represent mirrors. Now pick a point to place a theoretical laser and an angle to define the direction it's pointing. The question is: if your follow the laser beam path for some specified distance, what coordinate point are you at?
In this image,
L is the location of the laser,
t is it's angle (measured from the positive X axis),
M3 are all line segment mirrors, and
E is the point on the laser beam path after
D = d1 + d2 + d3 + d4 units, starting from
Write the shortest program (in bytes) that outputs
D, and a list of mirrors.
(Use http://mothereff.in/byte-counter to count bytes.)
Input will come from stdin in the format:
Lx Ly t D M1x1 M1y1 M1x2 M1y2 M2x1 M2y1 M2x2 M2y2 ...
- All the values will be floating points matching this regex:
- There is always exactly one space between each number.
- Requiring quotes around the input is allowed.
tis in degrees, but not necessarily in the
[0, 360)range. (If you prefer you may use radians instead, just say so in your answer.)
Dmay be negative, effectively rotating the laser 180 degrees.
Dmay also be 0.
- There may be arbitrarily many mirrors (including none at all).
- The order of the mirrors should not matter.
- You may assume the input will come in multiples of 4 numbers. e.g.
Lx Ly tor
Lx Ly t D M1x1are invalid and will not be tested. No input at all is also invalid.
The layout above might be input as:
1 1 430 17 4.8 6.3 6.2 5.3 1.5 4.8 3.5 6 6.3 1.8 7.1 3
(Note that the image was drawn freehand and these values are only approximations. Martin Büttner's input values of
1 1 430 17 4.8 5.3 6.2 4.3 1.5 4.8 3.5 6 6.3 1.8 7.1 3
will give more collisions though they do not match the sketch.)
Output should go to stdout in the format:
These are also floats and may be in exponential form.
- Mirrors may intersect each other.
- Both sides of mirrors are reflective.
- The beam may hit the same mirror many times.
- The beam goes on forever.
You may assume that the cases where
- the laser starts on a mirror line segment
- the laser beam hits the endpoint of a mirror
- the laser beam hits the intersection between two mirrors
are undefined and will not be tested. Your program may do anything if these occur, including throw an error.
Just for fun, I will award 200 bounty points to the highest voted submission that outputs a graphical representation of the problem (you could even write an interactive script). These bonus submission do not need to be golfed and can be lenient with how input and output are handled. They are distinct from the actual golfed submissions but both should be submitted in the same answer.
Note: Only submitting a bonus answer is fine, you just wont be the accepted answer. To be accepted you must exactly follow the input/output spec (e.g. output only involves
Ex Ey, not images), and be the shortest.