# How to calculate relative degree changes in 0 to 360.

I'm working on a project that measures wind direction and I'm stuck on this what appears to be a simple degree problem.

Example: Lets say I'm a compass (0' .. 360' ) now I'm pointing due north 0' , I want to take readings every minute and now I want to calculate the the minimal difference (relative to last reading) in degrees from the last reading..That is i want to calculate the shortest rotation in degrees that would get me to the current value

Assume we start at 0'
Reading 2:  10'  diff=10' (aka rotate right)
Reading 3:  350  diff=20' (rotate left)
reading 4:  180  diff=170' (rotate left)

My issue is that something like CurrentDeg- LastDeg =degChange doesn't work for boundary case for example

0 - 350 = -340 (It didnt roate 340 degreess but only 10', looking for minimal degree change - aka Relative offset)

Any help would be appreciated

-
um, 0-350 is not 340 – deinst Feb 16 '12 at 23:09
@tonyb, are you writing this up in computer code? – Edison Feb 16 '12 at 23:33

break this up into cases:

case1: absolute value of difference in angles is less or equal to 180 degrees. this is the only case you are considering.

case2: absolute value in difference in angles is greater than 180 degrees. If this is the case, then take 360-(difference in angles)

So in psudo-code, it would look something like:

Degree_Change=|current_degree-last_degree| is what you initially calculate. But you have to make sure that it is less than or equal to 180 degrees, so you look at a couple of cases:

if Degree_Change $\leq$ 180 degrees then Degree_Change=|current_degree-last_degree|

so you do not need to modify it

if Degree_Change $\ge$ 180 degrees then Degree_Change=360-|current_degree-last_degree|

subtracting by 360 gives you an angle less than 180.

-