-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_point_on_line.m
59 lines (50 loc) · 2.35 KB
/
find_point_on_line.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 coords = find_point_on_line(coords0,coords1,dist,disttype)
% This function finds the coordinates of a point on a line specified by the
% start point and end point. This point is either defined as a set distance
% or as a ratio from the start point (default). The coordinates of the
% start and endpoints can be in any number of dimensions
% Inputs:
% coords0 = a n-by-m matrix of
% n start points in m dimensions
% coords1 = a n-by-m matrix of
% n end points in m dimensions
% dist = the distance or ratio
% (default) to calculate on the
% line
% disttype = 'ratio' (default) or
% 'distance'. Specifies how to
% calculate the distance along
% the line
%
% Output:
% coords = the coordinates of the
% point on the line
%
% Note this code can be run on multiple coordinates with multipe specified
% distances to calculate at once. Just enter multiple start coordinates
% into coords0 and coords1 (1st column is x coords, 2nd is y coords etc).
% Can also enter different distances for each coordinate to calculate
% This is basically just a kind of linear interpolation
if nargin < 4
disttype = 'ratio';
end
if size(coords0) ~= size(coords1)
error('Same number of coordinates and dimensions required in coords0 and coords1')
end
if length(dist) ~= 1 && size(dist,1) ~= size(coords0,1)
error('dist needs to be a scalar or have a value for each pair of coordinates')
end
switch disttype
case 'ratio'
% when 0 < t < 1 the coordinate is between those defined by coords0
% and coords 1, when t < 0 the coordinate is closer to coords0, and
% when t > 1 the coordinate is closer to coords1
t = dist;
case 'distance'
d = sqrt(sum((coords0 - coords1).^2,2));
t = dist./d;
otherwise
error('Unrecognised entry for disttype')
end
coords = (1-t).*coords0 + t.*coords1;
end