clock - Modulo algorithm proving elusive -
i have color-wheel maps color each hour on 24-hour clock. given hour of day, want map colors 12-hour clock such colors 5 hours before , 6 hours after current hour used. gets bit tricky b/c 0th index of result has 0th color or 12th color of 24 color-wheel.
for example, given colors24
array of 24 colors , hour time of 5 final color12
array map colors24's indexes as:
{0,1,2,3,4,5,6,7,8,9,10,11}
if hour 3, then:
{0,1,2,3,4,5,6,7,8,9,22,23}
and if hour 9, then:
{12,13,14,15,4,5,6,7,8,9,10,11}
bonus points if algorithm can generalized 2 arrays regardless of size long first evenly divisible second.
if hours
total number of hours (24), length
number of colors displayed @ time (12), , hour
current hour, generic algorithm indexes color array:
result = []; add = hour + hours - (length / 2) - (length % 2) + 1; (i = 0; < length; i++) { result[(add + i) % length] = (add + i) % hours; }
here javascript implementation (generic, can used other ranges 24/12):
function getcolorindexes(hour, hours, length) { var i, result, add; if (hours % length) throw "number of hours must multiple of length"; result = []; add = hour + hours - (length / 2) - (length % 2) + 1; (i = 0; < length; i++) { result[(add + i) % length] = (add + i) % hours; } return result; } console.log ('hour=3: ' + getcolorindexes(3, 24, 12)); console.log ('hour=5: ' + getcolorindexes(5, 24, 12)); console.log ('hour=9: ' + getcolorindexes(9, 24, 12)); console.log ('hour=23: ' + getcolorindexes(23, 24, 12));
as stated in question, number of hours (24) must multiple of length of array return.
Comments
Post a Comment