英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论