tags:

views:

58

answers:

4

What is the opposite of the Math.tan(double x) function of java?

I know that Tan(X) = oppositeSideLength/AdjacentSideLength

but I have the opposite and adjacent sides so I want to do the opposite operation.

ie: x = Tan^-1(oppositeSideLenght/AdjacentSideLength) (that is how I would enter it in a calculator.

I just looked in the Math class and I know that there is:

  • Math.atan(
  • Math.atan2

but I don't think that either of these is what I am looking for.

+1  A: 
John Kugelman
+1  A: 

Math.atan is indeed the opposite of tangent. It's called arctangent.

Basically x = arctan(tan(x)). Well, it's a tad more complex than that seeing that tangent is a repetitive function (it needs some adjustments by adding k*pi). You should check out the wikipedia article about the details.

Anyway, you can indeed compute x (the angle) by doing Math.atan(opposite/adjacent). Take note though that the angle will be in radians, so make sure you convert to other units if that's not what your using.

Andrei Fierbinteanu
+1  A: 

Math.atan and Math.atan2 should work just fine.

angle = Math.atan(Math.Tan(radians));
angle = Math.atan(oppositeSideLength/adjacentSideLength)
angle = Math.atan2(oppositeSideLength, adjacentSideLength)
Chris Taylor
I don't think atan2 is what he needs. According to wikipedia: For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it. It's a different angle from the one in the triangle.
Andrei Fierbinteanu
atan2 WILL be correct. However, it is not necessary. As I said in my response, atan will be sufficient.
woodchips
+4  A: 

Yes, you do indeed want atan, or sometimes, atan2. The difference between the two is that atan will fail under some circumstances when one of the side lengths are zero. While that may be unlikely for triangles, it is a possibility for some other, more general uses of atan. In addition, the atan function gives you an angle limited to the interval [-pi/2,pi/2]. So if you think about the atan function as a function of two inputs, (x,y), atan(y/x) will yield the same result as atan((-y)/(-x)). This is a serious flaw in some circumstances.

To solve these problems, the atan2 is defined such that it yields the correct result for all values of x and y, in any quadrant. One would use it as

atan2(oppositesidelength,adjacentsidelength)

to yield a consistent result.

Of course, for use in a non-degenerate triangle, the simple call to atan(opposite/adjacent) should be entirely adequate for your purposes.

woodchips