我试着用三角形的长度来编写计算角度的代码。公式为cos(a)=b^2+c^2-a^2/2bc。(三角形在这里)
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / 2 * length2 * length3)* 180 / 3.14153;
angle2 = acosf((powf(length1,2) + powf(length3,2) - powf(length2,2)) / 2 * length1 * length3)* 180 / 3.14153;
angle3 = 180 - (angle2 + angle1);一切都是浮动的。当输入5-4-3输入时,结果如下。
angle one is 90.0018
angle two is nan
angle three is nan改变顺序并不重要,只给出5的输出。
发布于 2022-08-17 11:48:25
你所做的是:
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / 2 * length2 * length3)* 180 / 3.14153;你应该做的是:
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / (2 * length2 * length3))* 180 / 3.14153;解释:问题是由以下公式引起的,这个公式实际上写得很糟糕:
cos(a)=b^2+c^2-a^2/2bc
// This, obviously, is wrong because
// you need to group the firt three terms together.
// Next to that, everybody understands that the last "b" and "c" are divisors,
// yet it would be better to write it as:
cos(a)=(b^2+c^2-a^2)/(2bc)我在代码中添加的方括号类似于用/2bc替换/(2bc)。
https://stackoverflow.com/questions/73387598
复制相似问题