1 %% Copyright (c) 2011, INRA
2 %% 2004-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
14 %% 2. Redistributions in binary form must reproduce the above copyright notice,
15 %% this list of conditions and the following disclaimer in the documentation
16 %% and/or other materials provided with the distribution.
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PUR@var{pos}E
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% @var{pos}SIBILITY OF SUCH DAMAGE.
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
35 %% @deftypefn {Function File} {@var{pos} =} linePosition (@var{point}, @var{line})
36 %% Position of a point on a line.
38 %% Computes position of point @var{point} on the line @var{line}, relative to origin
39 %% point and direction vector of the line.
40 %% @var{line} has the form [x0 y0 dx dy],
41 %% @var{point} has the form [x y], and is assumed to belong to line.
43 %% If @var{line} is an array of NL lines, return NL positions, corresponding to
46 %% If @var{point} is an array of NP points, return NP positions, corresponding
49 %% If @var{point} is an array of NP points and @var{line}S is an array of NL lines,
50 %% return an array of [NP NL] position, corresponding to each couple
56 %% line = createLine([10 30], [30 90]);
57 %% linePosition([20 60], line)
62 %% @seealso{lines2d, createLine, projPointOnLine, isPointOnLine}
65 function d = linePosition(point, lin)
72 % if both inputs have the same size, no problem
75 dxp = point(:, 1) - lin(:, 1);
76 dyp = point(:, 2) - lin(:, 2);
79 % one point, several lines
82 dxp = point(ones(Nl, 1), 1) - lin(:, 1);
83 dyp = point(ones(Nl, 1), 2) - lin(:, 2);
86 % one lin, several points
87 dxl = lin(ones(Np, 1), 3);
88 dyl = lin(ones(Np, 1), 4);
89 dxp = point(:, 1) - lin(1);
90 dyp = point(:, 2) - lin(2);
93 % expand one of the array to have the same size
94 dxl = repmat(lin(:,3)', Np, 1);
95 dyl = repmat(lin(:,4)', Np, 1);
96 dxp = repmat(point(:,1), 1, Nl) - repmat(lin(:,1)', Np, 1);
97 dyp = repmat(point(:,2), 1, Nl) - repmat(lin(:,2)', Np, 1);
101 d = (dxp.*dxl + dyp.*dyl) ./ (dxl.^2 + dyl.^2);
106 %! point = [20 60;10 30;25 75];
107 %! lin = createLine([10 30], [30 90]);
108 %! pos = linePosition(point, lin)
110 %! plot(point(:,1),point(:,2),'ok');
112 %! drawLine(lin,'color','r');
113 %! plot(lin(1)+lin(3)*pos,lin(2)+lin(4)*pos,'xb')
118 %! lin = createLine([10 30], [30 90]);
120 %! pos = linePosition(point, lin);
121 %! assert (res, pos);
124 %! point = [20 60;10 30;25 75];
125 %! lin = createLine([10 30], [30 90]);
126 %! res = [.5; 0; .75];
127 %! pos = linePosition(point, lin);
128 %! assert (res, pos);
132 %! lin1 = createLine([10 30], [30 90]);
133 %! lin2 = createLine([0 0], [20 60]);
134 %! lin3 = createLine([20 60], [40 120]);
135 %! lines = [lin1;lin2;lin3];
137 %! pos = linePosition(point, lines);
138 %! assert (res, pos);