Given the degree of x, find the closest degree in the degree array

I have an array of degrees, [10, 90, 200, 280, 355] for a circle.

I am given a degree, say 1. How do I determine that 1 is closest to 355 degrees?

+7
source share
9 answers

Subtract the two numbers. If the difference is greater than 180 [or below -180], subtract [or add] 360. Now you can simply compare the absolute values ​​of the difference.

+4
source

Here is the actual formula:

degreediff = min(abs(xy),360-abs(xy)) 
+4
source

It is more compact and efficient:

 function difference(a, b) { var d = Math.abs(a - b); return d > 180 ? 360 - d : d; }; function closest(a, bs) { var ds = bs.map(function(b) { return difference(a, b); }); return bs[ds.indexOf(Math.min.apply(null, ds))]; }; > difference(1, 355) 6 > closest(1, [10, 90, 200, 280, 355]) 355 
+1
source

You have one value that will contain the found degree of closure found_degree and one for the actual difference degree_difference .

Next, iterate over the entire array and calculate two values: abs(degree_at_position - target_degree) and abs(degree_at_position - 360 - target_degree) . If one of these values ​​is less than degree_difference , you will get a closer degree - save it in found_degree and update degree_difference accordingly.

What is it.

Perhaps you should initialize found_degree with -1 and degree_difference with 360 to make sure that you can correctly interpret the result in the case of an empty given array - or you just handle the case of empty input of the array separately.

Is this homework, by the way?

0
source

The brute force approach would be something like this:

 var closestElement; var closestDivergence = 360; var toCompare = 355; var choices = [1, 90, 200, 280, 355]; for(i=0;i<choices.length;i++){ var currentDivergence=choices[i] - toCompare; if (currentDivergence<0) { currentDivergence+=360; } if (currentDivergence < closestDivergence){ closestDivergence = currentDivergence; closestElement = i; } } if (closestElement != NaN){ alert('Closest value is '+choices[closestElement]); } 
0
source

Here is a nice little quick

 function closest(deg,ar) { return ar.sort(function(a,b){var c = deg; return Math.min(360 - (ac),Math.abs(ac)) - Math.min(360 - (bc),Math.abs(bc))}) } var myArray = [355, 280, 200, 181, 90, 30]; alert(closest(180,myArray)); 

Sorts and returns the array according to which it is closest to the degree provided. Index 0 is closest. This completes, making 355 closer to 0 than 10.

0
source

First check the array (check which element is closer) using the given degree (1 in your example), then add 360 and check with that degree (361). Compare which result is better:

x given degree, y - first result, z - second result

 if (abs(xy) < 360+xz) choose y; else choose z; 

If the array is sorted, you can test it with binary sort, which gives you O (log n) time in the worst case. Otherwise, you need to view the entire array twice.

0
source

This formula will only work with circles. Of course, this is pseudo code.

  degree diff = min(abs(xy),360-abs(xy)) 
0
source

Using the comments on this page, I managed to find this code:

 function closest(deg, degs) { deg = (deg / 360 > 1 ? deg - (Math.floor(deg / 360)*360) : deg); var difference = 360; var closest = -1; for(i=0;i<degs.length;i++) { var x = degs[i]; var diff = Math.min(Math.abs(x-deg),360-Math.abs(x-deg)) if(diff <= difference) { closest = i; difference = diff; } }; return closest; 

}

nearest (1000, [10, 90, 200, 280, 355]);

0
source

All Articles