# Calculating an angle using two points / coordinates

Daniel Devlin

Greenhorn

Posts: 15

posted 3 years ago

Hi All,

I'm trying to calculate the angle between two co ordinates. I am not sure where to start. If necessary I can provide side lengths as with a triangle but if I use the law of cosine I dont know what to use in Java to represent the inverse of cosine. Any help would be greatly appreciated.

thanks.

I'm trying to calculate the angle between two co ordinates. I am not sure where to start. If necessary I can provide side lengths as with a triangle but if I use the law of cosine I dont know what to use in Java to represent the inverse of cosine. Any help would be greatly appreciated.

thanks.

posted 3 years ago

It seems to me you could only get a line between two coordinates, but the class you're looking for is java.lang.Math. There you'll find a method called acos(). Does that do it for you?

posted 3 years ago

It is normally better to use atan2() when getting the angle of a line since it automatically resolves the quadrant ambiguity and deals better acos() for small angles ( or angles near 90 degrees when one uses asin()). One still has to manually handle the condition where the two points are co-incident.

Greg Charles wrote:There you'll find a method called acos().

It is normally better to use atan2() when getting the angle of a line since it automatically resolves the quadrant ambiguity and deals better acos() for small angles ( or angles near 90 degrees when one uses asin()). One still has to manually handle the condition where the two points are co-incident.

Campbell Ritchie

Sheriff

Posts: 49367

62

posted 3 years ago

I think we are talking at cross-purposes. I was assuming that given two points P1 and P2 the OP wanted the angle atan2(P2.x-P1.x, P2.y-P1.y) but I suspect you think he wants (atan2(P2.x,P2.y) - atan2(P1.x,P1.y) ) . The OP needs to clarify.

Campbell Ritchie wrote:I presume you mean the angle at the origin of the coordinate system between two lines to different coordinates. As long as the coordinates are both not (0, 0), you can simply return 0° if they refer to the same point.

I think we are talking at cross-purposes. I was assuming that given two points P1 and P2 the OP wanted the angle atan2(P2.x-P1.x, P2.y-P1.y) but I suspect you think he wants (atan2(P2.x,P2.y) - atan2(P1.x,P1.y) ) . The OP needs to clarify.

Campbell Ritchie

Sheriff

Posts: 49367

62

Daniel Devlin

Greenhorn

Posts: 15

posted 3 years ago

Thanks for the replies. I'll try to clarify my post as I haven't done so (apologies). I have an image on which I can select two points e.g. 23,45 and 45,88. These would be the coordinates of the two selections. I know need to be able to move along the line between the two coordinates. My intention was to create a triangle where the origin would be the intersection between the coordinates of the row of the first click and the column of the last one. I could use this to then calculate the lengths of hypotenuse , adjacent, and opposite. I know the triangle is a right angle triangle and so now I can calculate the angle between the points. To find the angle in trigonometry would be a2 + b2 -c2/2ab and then inverse the cos of this value. I may be doing this the completely wrong way but this is what I'm trying to do. Then if I know the angle between the points I can say if angle is between 0 and 45degrees go up one pixel then recalculate if its greater than 45 and less than 120 go right 1 pixel etc.

Thanks again for the help.

Thanks again for the help.

posted 3 years ago

In my last post in this thread I gave the equation you require to get the angle (the one that uses the single atan2()). BUT, you don't really say what you mean by 'move along the line' and depending on what you are doing as you move there are fast algorithms that do not require you to actually calculate the angle. See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm .

Daniel Devlin

Greenhorn

Posts: 15

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 3 years ago

I'm no GUI expert, but that strikes me as very involved way of going about things, and is likely to result in transformations that only move the component in one direction, which might result in odd visual effects if you're using it for animation.

If you are, my suspicion is that you'd need to keep t (your time) independently of your movement, and only move when the result is "close enough" to your axis of movement; but, as I say, I'm no expert, and there may well be libraries out there for doing exactly what you want.

Winston

Daniel Devlin wrote:By travelling along the line I mean moving pixel by pixel to the target co ordinate using the angle to determine which pixel is chosen to move to next.

I'm no GUI expert, but that strikes me as very involved way of going about things, and is likely to result in transformations that only move the component in one direction, which might result in odd visual effects if you're using it for animation.

If you are, my suspicion is that you'd need to keep t (your time) independently of your movement, and only move when the result is "close enough" to your axis of movement; but, as I say, I'm no expert, and there may well be libraries out there for doing exactly what you want.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here