12

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?

Lucy Loo
  • 171
  • 1
  • 1
  • 7
  • 3
    That is a horrifically bad approximation to pi. **First**, use `M_PI` if it's defined, please, or else define it to `LDBL_DIG_10` digits precision. **Second**, you should be converting the _output_ of `atan()` from rads to degrees, not the input (which is just a slope). **Third**, you should use `atan2(y,x)` and not `atan(y/x)`. – Iwillnotexist Idonotexist Nov 24 '16 at 01:25
  • 1
    Um... Firstly, your text says you want to put the value into `tan`, but the code then uses it in `atan`. So, is it for `tan` or for `atan`? Make up your mind. Secondly, if this is for `atan`, then why are you converting it? The argument of `atan` is not an angle. It is tangent value. Tangent is neither "radians" nor "degrees". Tangent is a unitless value. Converting "radians to degrees" for something that is not an angle makes no sense whatsoever. It is the *result* of `atan` that you might want to convert to degrees. The result, not the argument. – AnT stands with Russia Nov 24 '16 at 01:35

1 Answers1

14

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)

fivef
  • 2,397
  • 21
  • 21
anthonybell
  • 5,790
  • 7
  • 42
  • 60