Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 7:32am
Yvar Birx2.4K
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

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;
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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: bad
good
Please Sign up or sign in to vote.

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 355
1 _Amy 235
2 Peter Leow 185
3 Andreas Gieriet 180
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,462
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100