Bearing from one coordinate to another

后端 未结 4 1253
北恋
北恋 2020-12-08 21:33

I implemented the \"bearing\" formula from http://www.movable-type.co.uk/scripts/latlong.html. But it seems highly inaccurate - I suspect some mistakes in my implementation.

4条回答
  •  Happy的楠姐
    2020-12-08 22:01

    You just have your parentheses () in the wrong place.

    You are adding degrees to a value in radians, which won't work. toDegrees() will do the conversion from radians to degrees for you, then you do the normalisation once you have a value in degrees.

    You have:

     Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;
    

    But you need:

    ( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;
    

    Remember also that all inputs to Math.sin(), Math.cos() and all the other trigonometric functions must be in radians. If your inputs are degrees you'll need to convert them using Math.toRadians() first.

提交回复
热议问题