質問

So I am working on a 3-D scene where I need to convert to and from polar coordinates at will, and have written a function to help me convert. The problem is that it is resulting in a NaN for one of the angles pretty regularly. The function I am using is as follows:

toPolar: function(x,y,z){
    var sqrd = (x*x)+(y*y)+(z*z)
    var radius = Math.pow(sqrd,.5)
    var theta = Math.acos(z/radius)
    var phi = Math.asin(y/x)
    var toReturn={
        r:radius,
        t:theta,
        p:phi
    }
    return toReturn
}

Phi is what is returning NaN, and Although I though I figured out where it resulted in NaN it seems as though it is happening in certain points all throughout the scene

You can see the problem here: http://cabbibo.com/sketches/audioSketch3/

Where in the upper left hand corner you see the polar and cartesian coordinates of the camera, and the last section of the polar coordinates (phi) will show up as NaN from time to time.

I assume its some sort of problem with my math, because I am not that good at it, but it seems like it might be a different issue, such as me not understanding how to use Math.asin ...

Thanks for your time, and let me know if there is any other information anybody needs!

Isaac

役に立ちましたか?

解決

If you're expecting phi to be like Azimuth (e.g. in the horizontal plane over, -pi -> pi) you should use atan2.

var phi = Math.atan2(y,x);

他のヒント

You don't want polar coordinates; you appear to want spherical coordinates.

Your equation for phi doesn't look right. Check this out:

http://en.wikipedia.org/wiki/Spherical_coordinate_system

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top