Optimize a Turn (rotation)

Given an Angle (A) that is between 0 and 359 degrees
and given Angle (B) the is also between 0 and 359 degrees

how to determine the direction and Miniimum # of degrees to turn (A) so it equals (B)

Note : A needs to increment towards B, not just be assigned a new value… So A will either be INCREMENTED or DECREMENTED by 1 degree at a time …

So if A were 0 and B were 45, thats easy, Increment by 1
If A were 45 and B were 0, then decrement

but if A were 45 and B were 350, it is Decrement by 1, but am not finding the easy math to solve that :frowning:

seems to me

difference = min(A-B, B-A) ?
if difference < 0 then 
  amtToAdd = -1
else
  amtToAdd = 1

while a <> b
  a = a + amtToAdd

Thanks… looks like that just might do it

Try this:

// distance b > a
dim delta1 as Integer = a - b
if (delta1 < 0) then
  delta1 = 360 + delta1
end if

// distance a > b
dim delta2 as Integer = b - a
if (delta2 < 0) then
  delta2 = 360 + delta2
end if

// determine "direction"
dim amount as Integer
if (delta1 > delta2) then
  amount = 1
else
  amount = -1
end if

// turn a towards b
while (a <> b)
  a = a + amount
  
  if (a = 0) then
    a = 360
  elseif (a = 360) then
    a = 0
  end if
wend