1

我有一个函数应该找到两个弧度的中间

function mrad(rb,ra){return (rb+ra)/2;}

但有时当我用 Math.sin 和 Math.cos 绘制 x 和 y 时,这两个指定长度的新坐标会出现在与我想要的截然相反的地方。

例如; 如果我预计新点会向下和向右,有时它们会出现在向上和向左。除此之外,坐标是正确的!

这是我绘制新 x 和 y 的方法

xnew=xold-(100)*Math.cos(radian);
xnew=yold+(100)*Math.sin(radian);

我猜如果弧度 B (rb) 大于 ra 可能很重要。我认为正在发生的事情是,在这种情况下,我会绕一圈,而有时我应该做类似的事情

 function mrad(rb,ra){return (rb-ra)/2;}

我的问题是

  1. 我的假设正确吗?

  2. 条件是什么,如何判断何时执行rb-rarb+ra,或者说得更好,您如何判断一个弧度是指向另一个弧度还是指向另一个弧度?

它应该看起来像这样

function mrad(rb,ra){return ((/*condition*/)?(rb-ra):(rb-ra))/2;}

编辑

为了找到范围,我试图表达不同的值以找到弧度范围,但除了对角线之外找不到任何东西

http://jsfiddle.net/roLLqfs6/

此外,定义的长度并不总是 100,如示例中所写

4

1 回答 1

0

It's really a maths question. You need to make sure that your angles are in the same range (either -pi ... pi or 0 ... 2.0 * pi), but even then, taking the mean of them will not necessarily find the angle you are looking for - you might expect that (in degrees, for simplicity) the answer you want for 10º & 20º would be 15º, but what would you expect for 175º and -175º? I suspect you would want 180º (as this is the minor arc between them), but you will get 0º.

So, you need to test the difference, to see that they are less than 180º (pi radians) apart, and modify the answer accordingly if they are not.

于 2014-09-24T16:06:09.623 回答