如何计算旋转线反射光线与水平轴的角度?

问题描述 投票:0回答:1

我有一个 C++ 程序,我想计算光线的反射角。在示例中,我有一条与 X 坐标成 135 度角的黄线(光线),它击中了应该反射的红线。绿线是反射线。我已经尝试计算红线和黄线之间的角度,但我不知道如何处理旋转的线。所以这是我的问题:如何计算绿线与水平轴的角度?代码会很好。

此代码应计算黄线和红线之间的角度:

float a = atan(abs((HitPointWithRedLine.x - YellowLineStartPoint.x)) / abs(HitPointWithRedLine.y - YellowLineStartPoint.y)) * 180 / 3.14; 

但是因为我不知道如何处理线的角度,所以我尝试了这个(显然不起作用):

float resultAngle = a - angleLine
c++ math game-physics
1个回答
0
投票

您的代码没有计算两条线或向量之间的角度。为此,请使用点积。我不太确定你的变量意味着什么,但如果你有两个向量 redLine 和 YellowLine 你可以使用:

float lengthRedLine = sqrt(redLine.x * redLine.x + redLine.y * redLine.y) 
float lengthYellowLine = sqrt(yellowLine.x * yellowLine.x + yellowLine.y * yellowLine.y) 
float a = acos((redLine.x * yellowLine.x + redLine.y * yellowLine.y) / lengthRedLine / lengthYellowLine)
© www.soinside.com 2019 - 2024. All rights reserved.