-
Notifications
You must be signed in to change notification settings - Fork 4
/
angleDiff.m
33 lines (28 loc) · 814 Bytes
/
angleDiff.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function delta = angleDiff(v1, v2)
% ANGLEDIFF Determine smallest angle between two directions.
%
% ANGLEDIFF(v1, v2)
%
% Parameters:
% v1 - n x 1, vector representing first direction
% v2 - n x 1, vector representing second direction
% Output:
% delta - angle in radians between first and second directions
%
% N. Fachada
% Instituto Superior Técnico, Lisboa, Portugal
% Determine number of dimensions
numDims = size(v1, 1);
% Make sure vectors are in 1st or 2nd quadrants (from a 2D perspective,
% although this should work for m-dimensions)
if v1(numDims, 1) < 0
v1 = -1 * v1;
end;
if v2(numDims, 1) < 0
v2 = -1 * v2;
end;
% Obtain angle between vectors (thus, between the directions they
% represent)
cosDelta = dot(v1, v2) / (norm(v1) * norm(v2));
delta = acos(cosDelta);
end