Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating absolute differences between two angles

I have two angles a and b, I want to calculate the absolute difference between both angles. Examples

>> absDiffDeg(360,5)
ans = 5
>> absDiffDeg(-5,5)
ans = 10
>> absDiffDeg(5,-5)
ans = 10
like image 573
Daniel Avatar asked Aug 28 '15 17:08

Daniel


2 Answers

Normalize the difference, abs operation is not necessary because mod(x,y) takes the sign of y.

normDeg = mod(a-b,360);

This will be a number between 0-360, but we want the smallest angle which is between 0-180. Easiest way to get this is

absDiffDeg = min(360-normDeg, normDeg);
like image 192
Forss Avatar answered Nov 05 '22 20:11

Forss


How about unsing unwrap ? Here is a try:

absDiffDeg = @(a,b) abs(diff(unwrap([a,b]/180*pi)*180/pi));

Best,

like image 4
Ratbert Avatar answered Nov 05 '22 21:11

Ratbert