如何找到光束从线面的反射角。

huangapple go评论65阅读模式
英文:

how to find the angle of reflection of the beam from the line

问题

我有一条线 (x1, y1) (x2, y2),以及一个角度为 "a" 的射线,我需要找到射线从这条线上反射的角度,就像下面的图片一样。谢谢。

英文:

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 如何找到光束从线面的反射角。

答案1

得分: 0

束束开始方向是向量Dir (dir.x, dir.y)
给定线条的单位法线N (n.x, n.y)

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

反射后,Dir的切向分量被反转,法向分量保持不变,因此我们可以使用点积为新方向编写方程:

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

// 反射后
newdir.x = dir.x - 2 * dot * n.x
newdir.y = dir.y - 2 * dot * n.y

如果确实需要角度,请使用atan2函数

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

但在大多数情况下,反射/碰撞问题可以通过矢量分量来解决,而无需直接使用角度。

英文:

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

huangapple
  • 本文由 发表于 2020年4月7日 04:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定