Click here to Skip to main content
13,344,765 members (62,271 online)
Rate this:
Please Sign up or sign in to 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:[^]

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
Please don't re-post!
Are you still at school? Did not learn correspondent part of algebra and geometry?
Rate this: bad
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;
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.
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?!
Rate this: bad
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:[^].

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2018
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