-
Notifications
You must be signed in to change notification settings - Fork 0
/
perpVector.m
59 lines (47 loc) · 1.53 KB
/
perpVector.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function v_perp=perpVector(point1,e_1,point2)
%finds the perpendicular vector v connecting the straight line defined by
%point1 and e_1 with point2
%
%SYNOPSIS v_perp=perpVector(point1,e_1,point2)
%
%INPUT point1, e_1 : point and UNIT vector defining the straight line
% point2: point from which the new perpendicular vector should start
% (all inputs can be lists of n-by-d, where d is the dimension)
%
%OUTPUT v_perp perpendicular vector from the straight line to the point
% (norm(v_perp)=distance of the point from the line)
%
%c: 1/03 Jonas
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%============================
% TEST INPUT
%============================
[nRows1, nCols1] = size(point1);
[nRowsE, nColsE] = size(e_1);
[nRows2, nCols2] = size(point2);
if ~all([nCols1,nCols2]==nColsE)
error('inconsistent dimensions!');
end
nEntries = max([nRows1,nRowsE,nRows2]);
if ~all([nRows1,nRowsE,nRows2]==nEntries | [nRows1,nRowsE,nRows2]==1)
error('inconsistent number of entries')
end
if nRows1 == 1
point1 = repmat(point1,nEntries,1);
end
if nRows2 == 1
point2 = repmat(point2,nEntries,1);
end
if nRowsE == 1
e_1 = repmat(e_1,nEntries,1);
end
%===============================
%===============================
% CALCULATE DISTANCE
%===============================
%vector between point1 and point2
v_temp=point2-point1;
%projection of v_temp on e_1
v_proj=(repmat(sum(v_temp.*e_1,2),1,nCols1)).*e_1;
%v_temp - the parallel projection = the perpendicular part
v_perp=v_temp-v_proj;