Answer the question
In order to leave comments, you need to log in
How to build a beam simulation?
I'm solving problem #202 on Project Euler. I'm looking for a way to implement ray reflection simulation. Found how knowing the coordinates to get the cosine of the angle. If I want to build a ray inside a triangle so that it bounces off its walls, how do I build that ray? How to set the direction of movement?
Below I output the angles of the triangle: {'a': 0.8944271909999159, 'b': 0.0, 'c': 0.4472135954999579}
from math import sqrt, fabs
def get_angle(a, b, c):
ab = fabs(sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2))
ac = fabs(sqrt((a[0] - c[0]) ** 2 + (a[1] - c[1]) ** 2))
bc = fabs(sqrt((b[0] - c[0]) ** 2 + (b[1] - c[1]) ** 2))
cos_a = (ab ** 2 + ac ** 2 - bc ** 2) / (2 * ab * ac)
cos_b = (ab ** 2 + bc ** 2 - ac ** 2) / (2 * ab * bc)
cos_c = (ac ** 2 + bc ** 2 - ab ** 2) / (2 * ac * bc)
return {'a' : cos_a, 'b' : cos_b, 'c' : cos_c}
a = [0, 0]
b = [0, 25]
c = [12.5, 25]
print(get_angle(a, b, c))
Answer the question
In order to leave comments, you need to log in
As is often the case in light reflection problems, there is no need to reflect the beam. Imagine that the mirror is a portal to the Looking Glass. The beam goes straight and, passing through the mirror, enters the adjacent triangular room.
In this problem, you need to imagine that the entire plane is tiled with triangles, a straight ray goes from one point to another. How many sides of the triangles he crosses - so many rejections are done.
Reflections, if you so desire, can be done through vectors. You have a vector along the ray, and a vector along the mirror and a normal inward. Through the scalar product, you can expand the ray vector into a perpendicular and parallel to the mirror. Then you need to expand the vector perpendicular and add them.
But in this task, this will not help you in any way - because there will not be enough time to simulate these 100500 reflections and quantillions of possible initial angles.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question