How do I find the inverse tangent of a line?
Asked Answered
F

4

15

I've got a line (x1,y1) and (x2,y2). I'd like to use tan inverse to find the angle of that line, how would I do so in java?

I'd like to see what angle the line makes in relation to x1,y1

Freddafreddi answered 10/8, 2010 at 14:7 Comment(9)
A line by itself doesn't have an angle unless it's in reference to something. What's your reference point? 0,0?Shrubby
Edited my question to make it clearerFreddafreddi
@Meow, there is no such thing as an angle between a line and a point on that line; did you mean what was outlined in @stacker's answer?Evslin
I'd say it's pretty clear he is talking about arctan((y2 - y1) / (x2 - x1))Clad
@JeremyP: I'd say it isn't clear. I'm amazed at the number of answers and their upvotes when we still haven't ascertained what the question is yet.Leeann
@Troubadour: Because not everybody is as anal as the people here saying "you haven't defined the question yet". Most people have read the bit about (x1, y1) and (x2, y2) and made the assumption that the questioner is talking the angle being the one between the line and the x axis. It's pretty obvious really.Clad
@JeremyP: I disagree. Look at this question which was only asked yesterday. When this was first asked it sounded as if they wanted to know the angle between two vectors and in fact that was the original title of the question but there were not-so subtle pointers in the text that contradicted that. Many answers were given where people "made the assumption" that turned out to be irrelevant.Leeann
@Troubadour: That question seemed much more confusing to me. For a start it talked about two vectors but then had a diagram with only one. Sure, there's the possibility that the questioner wants a different angle to the obvious possibility here, but I'll bet 100 rep he doesn't.Clad
Troubadour is right. There is no such thing as "angle of a line", it is called the slope of a line... see en.wikipedia.org/wiki/Slope ... And "inverse tangent of a line" is very confusing. In geometry the tangent of a curve at a point is a line, moreover tangent of a line is the line itself. See en.wikipedia.org/wiki/Tangent ... I would change the title to "What is the slope of a line?", at leastNarrows
C
30

You need

Math.toDegrees(Math.atan((y2-y1)/(x2-x1)))

Do notice exception when x1=x2.

Capitate answered 10/8, 2010 at 14:11 Comment(3)
Use atan2 and you can avoid that.Polarity
Why would this be returning a negative angle?Freddafreddi
Because atan returns value between (-Pi/2, Pi/2). Use Math.atan2(). Seems it handles problems of Math.atan usage. If you need positive value, just add 2*Pi if negative value is received.Capitate
D
25

Use the Math.atan2 function. It is like arctan but knows about x and y coordinates, so it can handles lines that are horizontal, vertical, or pointing in other directions -- arctan's range of -pi/2 through pi/2 will not give the correct answer for some lines.

Downturn answered 10/8, 2010 at 14:12 Comment(0)
B
20

The atan2 function helps solve this problem while avoiding boundary conditions such as division by zero.

Math.atan2(y2-y1, x2-x1)
Bergin answered 10/8, 2010 at 14:13 Comment(0)
D
3

This post Angle between 2 points has an example using atan().

Dormie answered 10/8, 2010 at 14:12 Comment(1)
Link is broken nowMab

© 2022 - 2024 — McMap. All rights reserved.