16

Possible Duplicate:
How to calculate the angle between two points relative to the horizontal axis?

I've been looking for this for ages and it's just really annoying me so I've decided to just ask...

Provided I have two points (namely x1, y1, and x2, y2), I would like to calculate the angle between these two points, presuming that when y1 == y2 and x1 > x2 the angle is 180 degrees...

I have the below code that I have been working with (using knowledge from high school) and I just can't seem to produce the desired result.

float xDiff = x1 - x2;
float yDiff = y1 - y2;
return (float)Math.Atan2(yDiff, xDiff) * (float)(180 / Math.PI);

Thanks in advance, I'm getting so frustrated...

Community
  • 1
  • 1
Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
  • 4
    I would suggest you ask this on http://math.stackexchange.com/ - although I'm sure there are plenty here capable of giving you a decent answer it's not strictly a programming question. – James Gaunt Oct 15 '12 at 08:07
  • 6
    you can't find a angle between two points. Two lines, yes. So presumably you mean to use the xaxis as the other line. – Mitch Wheat Oct 15 '12 at 08:07
  • What? "when x1 == x2 and x1 > x2" - when is that ever true? – Damien_The_Unbeliever Oct 15 '12 at 08:07
  • @Damien_The_Unbeliever I guess that should be an OR but it still doesn't make much sense. – Tobsey Oct 15 '12 at 08:09
  • 2
    Sorry, my mistake, the x1 == x2 is meant to be y1 == y2. I know this question doesn't particularly belong here but I honestly didn't know math.stackexchange.com existed until 6 seconds prior to be writing this... If anyone could help, I'd be forever grateful.. – Luke Joshua Park Oct 15 '12 at 08:10
  • arctan( y1 - y2 / x1 - x2 ) gives you the angle between PQ and X-axis, where P and Q are your points. If you want the "angle between these two points", which means the angle substended by these two points on the origin, you want arctan( y1 / x1 ) - arctan( y2 / x2 ) – Parakram Majumdar Oct 15 '12 at 08:18
  • Maybe you need to slow down and actually carefully describe the problem you're trying to solve - define what you mean by "the angle" (as @MitchWheat points out, two points just describe a line segment) – Damien_The_Unbeliever Oct 15 '12 at 08:21
  • 1
    In what direction do you want the angle increasing? Clockwise or anticlockwise? – phant0m Oct 15 '12 at 08:26
  • Well, provided we had a line the joined the two points, when the line is completely horizontal and the x1,y1 point is on the left and the x2,y2 point is on the right, the angle is 0 degrees. The angle will increase in a clockwise direction? – Luke Joshua Park Oct 15 '12 at 08:45
  • This link is related to java but i think this will help you http://stackoverflow.com/q/3365171/1577396 – Mr_Green Oct 15 '12 at 08:45

1 Answers1

30

From what I've gathered, you want the following to hold:

  • Horizontal line: P1 -------- P2 => 0°
  • Horizontal line: P2 -------- P1 => 180°

Rotating the horizontal line clockwise

You said, you want the angle to increase in clockwise direction.

Rotating this line P1 -------- P2 such that P1 is above P2, the angle must thus be 90°.

If, however, we rotated in the opposite direction, P1 would be below P2 and the angle is -90° or 270°.

Working with atan2

Basis: Considering P1 to be the origin and measuring the angle of P2 relative to the origin, then P1 -------- P2 will correctly yield 0.

float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;

However, atan2 let's the angle increase in CCW direction. Rotating in CCW direction around the origin, y goes through the following values:

  • y = 0
  • y > 0
  • y = 0
  • y < 0
  • y = 0

This means, that we can simply invert the sign of y to flip the direction. But because C#'s coordinates increase from top to bottom, the sign is already reversed when computing yDiff.

phant0m
  • 16,595
  • 5
  • 50
  • 82
  • Thanks for your reply, but when applied in C# it doesn't work at all. The current example I have is the first point being a fixed point (say 32, 32) and the second point being the x and y location of the mouse relative to the form. The code you provided returns either a 90 degree angle or -90 degree angle... – Luke Joshua Park Oct 15 '12 at 09:19
  • I have tested the code locally in VisualStudio now. Apart from forgetting that Coordinates in Windows go from top to bottom, the code is working correctly. – phant0m Oct 15 '12 at 09:46
  • @Aeaex, when you are testing new code like this, don't try it with the mouse first, try it with a couple of numbers. And if, by mistake, you try it with the mouse first and it doesn't work, don't go straight to the forum to complain, try it with a couple of numbers. – Beta Oct 15 '12 at 14:53
  • @Beta Your completely right , it was a mistake made on my part. The code works. Also, thank you for your post phant0m. Helped a lot. – Luke Joshua Park Oct 15 '12 at 20:14
  • @Aeaex Speaking about that code, I do wonder why it says `x2` twice in that subtraction, especially considering it doesn't in the JavaScript I used and nor does it in my VisualStudio code -.- – phant0m Oct 15 '12 at 20:15
  • @phant0m Which code excerpt are you referring to? – Luke Joshua Park Oct 15 '12 at 20:27
  • @Aeaex Look at the latest edit I made. Btw, you don't need to @ username when you want to notify the owner of a post ;) – phant0m Oct 15 '12 at 20:40