-2

I have a line (x1, y1) (x2, y2) and a ray at an angle “a”, I need to find the angle of reflection of the ray from this line, as in the picture below. Thanks enter image description here

Lesti001
  • 19
  • 2

1 Answers1

0

Beam starting direction is vector Dir (dir.x, dir.y)
Unit normal N (n.x, n.y) for given line is

l = sqrt((y1 - y2)^2 + (x2 - x1)^2)
n.x = (y1 - y2) / l
n.y = (x2 - x1) / l

After reflection tangential component of Dir is inverted and normal component remains the same, so we can write equations for new direction using dot product:

 dot = dir.x * n.x + dir.y * n.y

 //after reflection
 newdir.x = dir.x - 2 * dot * n.x
 newdir.y = dir.y - 2 * dot * n.y

If you really need angle, use atan2 function

angle = atan2(newdir.y, newdir.x)

but in the most of cases reflection/hitting stuff might be solved with vector components without direct using of angles

MBo
  • 77,366
  • 5
  • 53
  • 86