11,923,676 members (63,943 online)
Rate this:
See more:
Hello everyone,

I am currently attempting to get the degrees between a point and another.
The reason why is, because I need to rotate a sprite of mine, and I use a variable called "double Degrees".
So assuming that I want to calculate:

Central Point: 1,1
Degrees Point: 2,2
Image Example: http://i45.tinypic.com/2li910y.png[^]

I use the following method:

System.Windows.Forms.MessageBox.Show(GetAngleBetween(new Point(1, 1), new Point(2, 2)).ToString());

public double GetAngleBetween(Point p1, Point p2)
{
return Math.Atan(p1.X / p1.Y) - Math.Atan(p2.X / p2.Y);
}

Now obviously, looking from the picture, I should rotate my sprite with the angle: ~45*
But, my void returns 0. Can anyone explain to me why this is happening?
Posted 25-Feb-13 8:32am
Yvar Birx2.4K
Sergey Alexandrovich Kryukov 25-Feb-13 13:53pm

Are you still at school? Did not learn correspondent part of algebra and geometry?
—SA

Rate this:

## Solution 1

First, p1.X / p1.Y and p2.X / p2.Y are both exactly 1.0, so the result of the difference of the Atan values is 0.
Fundamentally, you're calculating this incorrectly, try:
public static double GetAngleBetween(Point p1, Point p2)
{
double deltaX = (double)(p2.X - p1.X);
if (deltaX < 1e-10)
return Math.PI / 2.0;
return Math.Atan(((double)(p2.Y - p1.Y)) / deltaX);
}
Also, note that this returns the angle in radians counterclockwise from the positive X direction with the origin at Point p1. To convert from radians to degrees multiply by 180 / π:
double angle = GetAngleBetween(new Point(1, 1), new Point(2, 2)) * 180.0 / Math.PI;
angle = GetAngleBetween(new Point(0, 0), new Point(1, 2)) * 180.0 / Math.PI;
v2
Sergey Alexandrovich Kryukov 25-Feb-13 14:09pm

I'm sorry, this solution is very incorrect, from the standpoint of precision. Also, you use a "magic" immediate constant. This is dirty.
Please see my comments to the answers to the OP's previous question, where I explain what happens with the precision.
—SA
Matt T Heffron 25-Feb-13 14:28pm

Mea culpa on the 1e-10. I could have just checked for ==0 (to prevent divide by zero) since System.Drawing.Point has X and Y as int.
Re: precision... again the input values are all integer and that will be at least as big of a limitation on precision as this implementation. (He's calculating sprite rotations, not computing chemotherapy dosages! :)
Sergey Alexandrovich Kryukov 25-Feb-13 14:42pm

Not a good practice, either. I answered how it should be done. You need to consider quadrants separated by 45-desgrees lines. Atan2 solves the problem, based on CPU instruction. This is the correct solution.
Sorry, "not chemotherapy" is just an attempt of excuse, and a very bad one. Have you ever heard that technology is universal?!
—SA
Rate this:

## Solution 2

Please see you previous question and my comment to the not-quite-correct answers and my considerations about precision.

The right function to use is this:
http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx[^].

This method automatically finds the angle between a vector and coordinate axes X, choosing appropriate sin or cos for each quadrant, to enable optimum precision.

You can find this angle for first and second vector separately, and than subtract one angle from another. Convert to degrees as required.

—SA

Top Experts
Last 24hrsThis month
 OriginalGriff 260 Sergey Alexandrovich Kryukov 220 Richard Deeming 210 Richard MacCutchan 175 Dave Kreskowiak 150
 OriginalGriff 7,296 KrunalRohit 4,554 Sergey Alexandrovich Kryukov 3,506 George Jonsson 2,905 Suvendu Shekhar Giri 2,246