将色调分类为最短跨度的算法,例如 `350,354,2,10,15`? [0-360度]

In color schemes, I would like to sort the hues, but would like to avoid 'big gaps', i.e. prefer 350,354,2,10,15 over 2,10,15,350,354 (when expressed as 0-360 degree values). What's the best approach of doing that (eg in php)? Is it finding the 'biggest gap' and start after that? Any better ideas?

If you don't have that many:

  1. just sort in order
  2. find the variance (modulo 360) (i.e, how far out are they from the 'modulo 360 mean')
  3. Move the first to the end, check variance again.
  4. After you have tried all of them, choose the one with the smallest.

This algorithm is O(N^2) in the size of the of the list.

The main takeaway is that you only have N 'rotations' here. Decide a 'gappiness' statistic, and brute force it over all N rotations, and use the arrangement that minimizes the 'gappiness'.

Just find the biggest gap and put it in the beginning.

  1. Sort the array
  2. Find biggest gap (loop through the array, find the biggest distance between two neighbors)
  3. Move the gap to be the beginning (Another loop to shift all the numbers)