Project Euler 144 - Laser Beam Reflections
Official link: https://projecteuler.net/problem=144
Thought Process
This was a fun problem, at first I tried rotating the line along the normal using the angle of incidence, but it felt messy and I didn't like it, in the end I used a combination of vector and line intersections and my solution was potentially even more messy but it worked!
I took each line as an incoming vector, and then found the outgoing vector by reflecting it, the method for this is not difficult if you've seen it before, I link a StackExchange post where David K explains it very well. Then, with this outgoing vector, I turn into back into the line we are used to (y = mx + b), which again is simple if you've seen it before, but again I link a StackExchange post where bubba explains it nicely for those who are new to all of this!
Now that I have the outgoing line, I simply calculate where it will intersect the ellipse.
Note that this last step is not as straightforward for a computer as it is for a human. Firstly, the line intersects the ellipse twice (where we started, and our next intersection point which is the one we want) so we need to pick the correct x point where our lines intersect, next when I substitute x back into the ellipse equation, I again get 2 possible values for y. I use the outgoing line to double check that I pick the correct y, then I set the outgoing line as my next incoming line and repeat the process till I'm out of the ellipse.
I also had some fun with matplotlib in graphing the lines, which was also helpful for debugging as I made a few variable naming errors alogn the way.
The end picture looks pretty cool!
Interactive Code
Input an integer (yourinput)
Code will output the yourinput-th point the light beam hits, for example if you input 1 it will return (1.4, -9.6)