1 %% Copyright (c) 2011, INRA
2 %% 2010-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 PURPOSE
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 %% POSSIBILITY 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{mid} = } midPoint (@var{p1}, @var{p2})
36 %% @deftypefnx {Function File} {@var{mid} = } midPoint (@var{edge})
37 %% @deftypefnx {Function File} {[@var{midx}, @var{midy}] = } midPoint (@var{edge})
38 %% Middle point of two points or of an edge
40 %% Computes the middle point of the two points @var{p1} and @var{p2}.
42 %% If an edge is given, computes the middle point of the edge given by @var{edge}.
43 %% @var{edge} has the format: [X1 Y1 X2 Y2], and @var{mid} has the format [XMID YMID],
44 %% with XMID = (X1+X2)/2, and YMID = (Y1+Y2)/2.
46 %% If two output arguments are given, it returns the result as two separate variables or arrays.
48 %% Works also when @var{edge} is a N-by-4 array, in this case the result is a
49 %% N-by-2 array containing the midpoint of each edge.
61 %% @seealso{edges2d, points2d}
64 function varargout = midPoint(varargin)
69 mid = [mean(edge(:, [1 3]), 2) mean(edge(:, [2 4]), 2)];
72 % input are two points
76 % assert inputs are equal
79 if n1 != n2 && min(n1, n2)>1
80 error('geom2d:midPoint', ...
81 'Inputs must have same size, or one must have length 1');
84 % compute middle point
85 mid = bsxfun(@plus, p1, p2) / 2;
88 % process output arguments
92 varargout = {mid(:,1), mid(:,2)};
101 %! mid = midPoint(p1, p2);
102 %! assert (mid, exp);
118 %! mid = midPoint(p1, p2);
119 %! assert (mid, exp);
131 %! mid = midPoint(p1, p2);
132 %! assert (mid, exp);
145 %! mid = midPoint(p1, p2);
146 %! assert (mid, exp);
155 %! expX = [20 ; 30 ; 40];
156 %! expY = [30 ; 40 ; 50];
157 %! [x y] = midPoint(p1, p2);
162 %! edge = [10 20 30 40];
164 %! mid = midPoint(edge);
165 %! assert (mid, exp);
166 %! edge = [10 20 30 40; 30 40 50 60; 50 60 70 80];
167 %! exp = [20 30;40 50; 60 70];
168 %! mid = midPoint(edge);
169 %! assert (mid, exp);