Question: how to calculate the difference between two angles?
Answer: Just take the absolute value of their difference, then, if larger than 180, substract 360° and take the absolute value of the result. If the direction doesn't matter (you want the one that yields the smallest value), then do the reverse subtraction (mod 360) and calculate the smallest value of the two (e.g. with Math.