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

msw
  • 42,753
  • 9
  • 87
  • 112
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 2
    A line by itself doesn't have an angle unless it's in reference to something. What's your reference point? 0,0? – Noon Silk Aug 10 '10 at 14:10
  • Edited my question to make it clearer – Skizit Aug 10 '10 at 14:12
  • 2
    @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? – Pops Aug 10 '10 at 14:19
  • I'd say it's pretty clear he is talking about arctan((y2 - y1) / (x2 - x1)) – JeremyP Aug 10 '10 at 14:35
  • @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. – Troubadour Aug 10 '10 at 14:37
  • 1
    @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. – JeremyP Aug 10 '10 at 15:24
  • 2
    @JeremyP: I disagree. Look at [this question](http://stackoverflow.com/questions/3441782/how-to-calculate-the-angle-of-a-vector-from-the-vertical/3442109#3442109) 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. – Troubadour Aug 10 '10 at 15:30
  • @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. – JeremyP Aug 10 '10 at 15:49
  • Troubadour is right. There is no such thing as "angle of a line", it is called the slope of a line... see http://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 http://en.wikipedia.org/wiki/Tangent ... I would change the title to "What is the slope of a line?", at least – balint.miklos Aug 11 '10 at 01:45

4 Answers4

30

You need

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

Do notice exception when x1=x2.

Gedrox
  • 3,592
  • 1
  • 21
  • 29
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.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
20

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

Math.atan2(y2-y1, x2-x1)
ggg
  • 1,673
  • 1
  • 11
  • 5
3

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

stacker
  • 68,052
  • 28
  • 140
  • 210