1

我一直在努力实现一个简单的开普勒轨道,以证明我正在使用 JavaScript 进行概念验证。我面临的问题是我必须使用的数学会导致生成的点存在一定程度的不准确性。我正在使用以下等式来确定我的坐标。

y = sqrt(r-x^2)

我使用线性增加的 X 值来确定我的 y 坐标(当它接近 的上限时X)导致 Y 值迅速变大。当我尝试沿着我收到的坐标为任何东西设置动画时,它会加速到我方程的极限。

目前我只在我的笛卡尔平面上计算象限 II,我确信有更好的方法可以做到这一点,但是我对 JavaScript 数学方法相当陌生,并且不完全确定如何实现我想要做的事情。

我想知道极坐标是否可能是计算的方法,然后如果可能的话,使用r = sqrt(x^2 + y^2)已知x,y坐标的函数转换为笛卡尔坐标。

就像我说的那样,我不完全确定如何做到这一点,或者是否有可能。以下是我到目前为止的来源。提前感谢您的帮助!

    var x = 0;
    var y;
    var r = 10;
    var check = setInterval(function(){topHalf()}, 20);

    function topHalf(){
    if (Math.pow(x,2) <= 10){

            y = Math.sqrt(r-(Math.pow(x,2)));   
            x = x + 0.01;
            $('#orb').offset({top:y*100, left:x*100});



    }else{
        console.log('done');
        clearInterval(check);
    }


    }
4

1 回答 1

0

This instruction is not precise in floating point (since 0.01 cannot be represented as an exact number). The error becomes bigger at every iteration.

x = x + 0.01;

replace with

i = i + 1
x = i * 0.01

The error is further amplified by the square root, and possibly by the Pow. Replace all Math.pow(x,2) with x * x

于 2014-01-31T22:39:37.787 回答