function as = angleSep2d(a, b)

% !!!!!2d application!!!!

% anglesep(a,b) -- Find angle of separation between a and b.
% 
% The anglesep() function finds the angle of seaparation between an
% object facing heading a (on a 360 degree compass), and an object
% at b relative to the first object. For example, if you are standing
% facing 2 o'clock and object b is at 4 o'clock, there are 2:00 of
% separation. Similarly, if a is facing 20 degrees, and b is at 180 
% degrees relative to absolute (due) east, then a and b are separated
% by 160 degrees of arc relative to the facing of a.  This function
% accounts for the reflexivity of +- 180 degrees. This gives only
% the number of degrees, NOT the direction -- all numbers come out
% positive.

if ((a < 180) & (b < 180))
  % simplest case, small absolute angles

  as = abs(a - b);

else
  % Make sure to go the "short way" around
  % Thus, if a = 2 and b = 358, the answer should only be 4 degrees
  % and NOT 356.

  % Let absdiff stand for the absolute difference in numbers
  absdiff = abs(a - b);

  % Now just correct by "going the other way around"
  if (absdiff <= 180)
    as = absdiff;
  else
    as = abs(360 - absdiff);
  end

end

