Minimum distance between two degrees in a circle?

I am looking for a formula to find the shortest distance in degrees between two degrees in a circle: for example, 30 degrees and 170 degrees (140 degrees).

Signs of two degrees can be almost any real number and not necessarily between 0 and 360 (they can be negative or much more than 360, for example, -528.2 and 740 (which is 171.8 degrees)). However, the distance should always be <= 180 degrees and> = 0 degrees.

It sounds simple enough. But I tried to find a good solution for this, and I tried a lot of other code, but nothing that I have found so far does not work in all cases that I have tried. I work in C ++. Does anyone have any idea?

+8
c ++ trigonometry circle
source share
7 answers
  • Step 1: Get the Raw Difference. For example, given -528.2 and 740.0 this is 1268.2 .

    • one way: raw_diff = first > second ? first - second : second - first raw_diff = first > second ? first - second : second - first
    • another way: raw_diff = std::fabs(first - second)
  • Step 2: 360.0 multiple of 360.0 to get a value between 0.0 (inclusive) and 360.0 (exclusive).

    • mod_diff = std::fmod(raw_diff, 360.0)
  • Step 3: If this value is greater than 180.0 , subtract it from 360.0 .

    • one way: dist = mod_diff > 180.0 ? 360.0 - mod_diff : mod_diff dist = mod_diff > 180.0 ? 360.0 - mod_diff : mod_diff
    • another way: dist = 180.0 - std::fabs(mod_diff - 180.0)

This is probably the most readable as a series of statements:

 double raw_diff = first > second ? first - second : second - first; double mod_diff = std::fmod(raw_diff, 360.0); double dist = mod_diff > 180.0 ? 360.0 - mod_diff : mod_diff; 

But if desired, it is easy to express all this in one expression:

 180.0 - std::fabs(std::fmod(std::fabs(first - second), 360.0) - 180.0) 
+18
source share

You can also use vector math and trigonometry; angles would be in radians here.

 float angle(float angle1, float angle2) { float x1=cos(angle1); float y1=sin(angle1); float x2=cos(angle2); float y2=sin(angle2); float dot_product = x1*x2 + y1*y2; return acos(dot_product); } 
+2
source share

You can apply the formula that you will find here ( http://en.wikipedia.org/wiki/Arc_(geometry) ) in both corners and in both directions. Thus, you find two additional distances (if you sum them, you get a circle (or you can get the length of one arc by subtracting the length of the other arc from the circle).

You can then compare the two lengths to get the minimum distance between two points at different angles.

In C ++, you have the math.h library: http://www.cplusplus.com/reference/clibrary/cmath/

0
source share

You will need to import the math library, of course, for fmod and fabs.

 double a = -528.2; double b = 740.0; double diff = (a > b ? a - b : b - a); double mod_diff = fmod(diff, 360); double result = (mod_diff < 180 ? mod_diff : 360 - mod_diff); 
0
source share

I had a similar problem to find

  • The smallest distance from any point to any point in the circle. I got the solution as follows:

if N = number of points in a circle

  0 -> N-1 j before n/2 after (nj) 1 -> N-1 (j-1) before [(n/2)+1] after n-j+1 2 -> N-1 (j-2) before [(n/2)+2] after n-j+2 and so on 

where j is the second point, and i is the first point

Here is a little python code for the solution.

 for i in range(0, n): for j in range(i,n): if j < n/2+i: s_rt = ji else : s_rt = n-j+i 

I think that this can be used to find a solution by slightly changing the degree.

0
source share

You can try to get the absolute value of the difference of the residuals of two angles when dividing by 360.

 #include <iostream> #include <cmath> using namespace std; int degree_difference(int a, int b) { return abs(a%360-b%360); } int main() { int result = degree_difference(-235, 15); cout << "Difference: " << result << endl; return 0; } 
-one
source share

We must assume that the circle has only 360 degrees, otherwise it will be difficult.

So, the first thing you need to do is get each mark in the range from 0 to 360. To do this, you can take the module of both labels 360. If the amount is less than 0, add 360.

Let's say our points are 520 and -45.

 mark1 = ((520 % 360) >= 0) ? (520 % 360) : 360 - (520 % 360); mark2 = ((-45 % 360) >= 0) ? (-45 % 360) : 360 - (-45 % 360); 

mark1 will be 160. Sign 2 will be 315.

Now you just accept the absolute value of the difference:

 result = abs(mark1 - mark2) = 155 
-one
source share

All Articles