Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
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
Comments
Sergey Alexandrovich Kryukov 25-Feb-13 13:53pm    
Please don't re-post!
Are you still at school? Did not learn correspondent part of algebra and geometry?
—SA

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
 
Share this answer
 
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:
C#
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 / π:
C#
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;
 
Share this answer
 
v2
Comments
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.
I explained what to do in my answer, please see.
—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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900