开发者

Java: Tangent method

开发者 https://www.devze.com 2023-01-08 13:05 出处:网络
What is the opposite of the Math.tan(double x) function of java? I know that Tan(X) = oppositeSideLength/AdjacentSideLength

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 op开发者_如何转开发eration.

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.


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.


Yep, you want atan, the arc tangent. This is the inverse of tangent; same thing with sin and arc sine, cosine and arc cosine, etc. These are alternative mathematical terminology for these functions' inverses.

Notice how atan returns angles from -π/2 to π/2, by the way. That's a hint that it's an inverse function (tangent takes angles and spits out ratios, arc tangent takes ratios and spits out angles). It is also important to recognize the restricted range. You won't necessarily get back your original angle, since tangents repeat every π radians (every 180°) — tan(π) = 0, but atan(0) = 0, not π.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号