This is a little function I've been using for ages now and thought I'd post it up, as its proved to be very useful.
Heres one for using floats:
function WrapAngToAng(a#, b#, s#)
offset# = 180.0 - b#
offseta# = wrapvalue(a# + offset#)
if offseta# > 180.0
if offseta# > (180.0 + s#)
newa# = offseta# - s#
else
newa# = 180.0
endif
else
if offseta# < (180.0 - s#)
newa# = offseta# + s#
else
newa# = 180.0
endif
endif
newa# = wrapvalue(newa# - offset#)
endfunction newa#
Heres for integers:
function WrapAngToAng(a, b, s)
offset = 180 - b
offseta = wrapvalue(a + offset)
if offseta > 180
if offseta > (180 + s)
newa = offseta - s
else
newa = 180
endif
else
if offseta < (180 - s)
newa = offseta + s
else
newa = 180
endif
endif
newa = wrapvalue(newa - offset)
endfunction newa
To wrap angle a to angle b by degrees 3 you would use:
a = WrapAngToAng(a, b, 3)
Of course just choose which function you want.
There may be a faster way to do it, but hope its useful.
Simple... yet fun!