Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from Radians to Degrees

I'm building a small Physics engine and I'm having trouble converting my Radian value to Degrees using atan, as I need an angle to output in Degrees only.

Firstly, I have an x and y value, and I need to find an angle using atan, so I divide y by x like so:

angleDivide = yN / xN;

Then, before putting this value into tan, I attempt to convert it to Degrees like this:

angleToDegrees = angleDivide * (3.14 / 180);

Then I place angleToDegrees into atan:

angle = atan(angleToDegrees);

But when I'm displaying angle, I'm, still getting radian values.

Please could you tell me what is wrong with my code and how to fix this?

like image 974
Lucy Loo Avatar asked Nov 24 '16 01:11

Lucy Loo


1 Answers

You want to calculate radians=tan(y/x) first.

Then you can convert it to degrees:

radians = atan(y/x)
degrees = radians * (180.0/3.141592653589793238463)

See the reference here for atan:

On a side note, you also have to take into account what quadrant you are in to get the correct answer (since -y/x is the same number as y/-x)

like image 176
anthonybell Avatar answered Nov 15 '22 16:11

anthonybell