12

I need to know angle of rotation in polar coordinates using X and Y from cartesian coordinates.

How to do it in JS without a lot of IF statements? I know that I can do it using this system,

but I think that it will be bad for performance, because it is in animation cycle.

levshkatov
  • 477
  • 2
  • 5
  • 16
  • 1
    That will be a fair enough effort for anyone to brainstorm a solution until you have something to show like if you have kick off with something. So as a start up, just take a paper and recall everything you need and create a pseudo code/algorithm without any programming logic and then try to mold it into JS code. It could be dirty at first but improvements can be done later. If possible, you can dump the relevant section of code here to get a much better response. :) – Rohit416 Aug 26 '15 at 06:09
  • @Rohit416 If only I could do it. I said that I know how to do it only using if constructions. I think it's obvious and I don't write here this algorithm. I ask for any other ideas how to solve this problem, maybe math library in JS or something like that. – levshkatov Aug 26 '15 at 06:18
  • 1
    Yes, `Math` object and its methods will definitely help. [Have you look at this](http://stackoverflow.com/questions/8898720/cartesian-to-polar-coordinates) ? – Rohit416 Aug 26 '15 at 06:23
  • 1
    @Rohit416 Thanks a lot! `atan2()` is definitely what I was looking for. – levshkatov Aug 26 '15 at 06:26
  • it's meaningless to optimise if statements because `atan` will be way more costly –  Aug 26 '15 at 09:46
  • possible duplicate of [How to calculate the angle between a line and the horizontal axis?](http://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-a-line-and-the-horizontal-axis) – MvG Aug 27 '15 at 16:05
  • @willywonka_dailyblah: But getting all the code in all the if statements right would mean a lot more testing code to cover all the code paths. Reading it would be harder, too. A solution based on `atan2` is far easier to maintain. – MvG Aug 27 '15 at 16:09
  • @MvG as a VB.NET user, javascript _itself_ is hard enough to read and maintain :) –  Aug 28 '15 at 09:08

1 Answers1

22

Javascript comes with a built in function that does what is represented in the image: Math.atan2()

Math.atan2() takes y, x as arguments and returns the angle in radians.

For example:

x = 3
y = 4    
Math.atan2(y, x) //Notice that y is first!

//returns 0.92729521... radians, which is 53.1301... degrees

I wrote this function to convert from Cartesian Coordinates to Polar Coordinates, returning the distance and the angle (in radians):

function cartesian2Polar(x, y){
    distance = Math.sqrt(x*x + y*y)
    radians = Math.atan2(y,x) //This takes y first
    polarCoor = { distance:distance, radians:radians }
    return polarCoor
}

You can use it like this to get the angle in radians:

cartesian2Polar(5,5).radians

Lastly, if you need degrees, you can convert radians to degrees like this

degrees = radians * (180/Math.PI)
CoderMuffin
  • 519
  • 1
  • 10
  • 21
Jake
  • 625
  • 6
  • 16
  • 1
    Here is my 1 line version of the same function: const c2p = pt => [Math.sqrt(pt[0] ** 2 + pt[1] ** 2), Math.atan2(pt[1], pt[0])]; However, it also takes the argument as an array [x,y] and returns the array [distance,radian]. Conversely, here is also my 1 line function for polar to cartesian: const p2c = pt => [pt[0] * Math.cos(pt[1]), pt[0] * Math.sin(pt[1])]; – Adam Sassano Apr 01 '21 at 18:56
  • the hypotenuse function `hypot(x, y)` can be used instead of `sqrt(x*x+y*y)` – Alex Brown May 01 '21 at 17:44