I wanted to rewrite my answer to better explain answer the question. I'm using EXCEL for my formulas, and degrees for my units.
For simplicity, B
is the larger of the two values, and A
is the smaller of the two values. You can use MAX()
and MIN()
respectively in your solution later.
PART 1 - WHICH WAY TO GO?
What we want to do first is work out in which direction we want to perform the calculation, clockwise or anticlockwise. We use an IF()
Statement for that:
IF( (B-A)<=180, (Clockwise_Formula), (AntiClockwise_Formula) )
The above formula checks if going anticlockwise from B
to A
(which is the same as going clockwise from A
to B
) is less than or equal to 180 degrees. If not, it's going to be shorter to go the other direction.
To check this works: 90 - 45 = 45 (which is less than or equal to 180) makes the IF statement TRUE, so the clockwise direction is shorter, but 315 - 45 = 270 (which is larger than 180) makes the if statement FALSE, so the anticlockwise formula would be shorter.
PART 2 - CLOCKWISE FORMULA
Now you want to interpolate N
times between A
and B
, either clockwise or anticlockwise. The clockwise formula is relatively simple.
Clockwise_Formula: ((B-A)/N*S)+A
Where S
is a count of the number of interpolations, starting at 1 and finishing at N-1 (If S = N
, your answer will be B
)
Example: A
= 90, B
= 270, N
= 4
S=1: ((270-90)/4*1)+90 = 135
S=2: ((270-90)/4*2)+90 = 180
S=3: ((270-90)/4*3)+90 = 225
PART 3 - ANITCLOCKWISE FORMULA
The anitclockwise formula is going to be a little more complex, because we'll need to cross anticlockwise over the 360 degree angle. The easiest method I can think of is to add 360 to A
, then Modulate the answer by 360 using the MOD(FORMULA,VALUE)
function.
You'll also have to swap A
and B
around in the formula because B
is now the smallest number. (That might sound a bit confusing, but it works!)
(Unmodulated) AntiClockwise_Formula: (((A+360)-B)/N*S)+B
Example: A
= 60, B
= 300, N
= 4
S=1: (((60+360)-300)/4*1)+300 = 330
S=2: (((60+360)-300)/4*2)+300 = 360
S=3: (((60+360)-300)/4*3)+300 = 390
PART 4 - RESTRICTING ANSWERS TO BETWEEN 0 AND 360
See how sometimes (but not always) the answers are going to be greater than 360? This is where the wrapping your Anticlockwise_formula in a MOD()
function comes in:
AntiClockwise_Formula: MOD((((A+360)-B)/N*S)+B,360)
Modulating the example used in Part 3 will give you:
S=1: 330
S=2: 0
S=3: 30
PART 5 - PUTTING IT ALL TOGETHER
Combining all of the elements from Parts 1-4 together, the answer is:
IF((B-A)<=180,((B-A)/N*S)+A,MOD((((A+360)-B)/N*S)+B,360))
Where:
A
= The smaller of the two values (you can replace A with MIN())
B
= The larger of the two values (you can replace B with MAX())
N
= The number of interpolations you want to do (e.g. 2 is a half, 3 is into thirds etc)
S
= An incrimental count to a max of N-1 (see Part 2 for explanation)