]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/midPoint.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / midPoint.m
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>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%     
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.
17 %%
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.
29 %%
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.
33
34 %% -*- texinfo -*-
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
39 %%
40 %%   Computes the middle point of the two points @var{p1} and @var{p2}.
41 %%
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.
45 %%
46 %%   If two output arguments are given, it returns the result as two separate variables or arrays.
47 %%
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.
50 %%
51 %%   Example
52 %%
53 %% @example
54 %%   p1 = [10 20];
55 %%   p2 = [30 40];
56 %%   midPoint([p1 p2])
57 %%   ans =
58 %%       20  30
59 %% @end example
60 %%
61 %%   @seealso{edges2d, points2d}
62 %% @end deftypefn
63
64 function varargout = midPoint(varargin)
65
66   if nargin == 1
67       % input is an edge
68       edge = varargin{1};
69       mid = [mean(edge(:, [1 3]), 2) mean(edge(:, [2 4]), 2)];
70       
71   elseif nargin == 2
72       % input are two points
73       p1 = varargin{1};
74       p2 = varargin{2};
75       
76       % assert inputs are equal
77       n1 = size(p1, 1);
78       n2 = size(p2, 1);
79       if n1 != n2 && min(n1, n2)>1
80           error('geom2d:midPoint', ...
81               'Inputs must have same size, or one must have length 1');
82       end
83       
84       % compute middle point
85       mid = bsxfun(@plus, p1, p2) / 2;
86   end
87
88   % process output arguments
89   if nargout<=1
90       varargout{1} = mid;
91   else
92       varargout = {mid(:,1), mid(:,2)};
93   end
94
95 endfunction
96
97 %!test
98 %!  p1 = [10 20];
99 %!  p2 = [30 40];
100 %!  exp = [20 30];
101 %!  mid = midPoint(p1, p2);
102 %!  assert (mid, exp);
103
104 %!test
105 %!  p1 = [ ...
106 %!      10 20 ; ...
107 %!      30 40 ; ...
108 %!      50 60 ; ...
109 %!      ];
110 %!  p2 = [ ...
111 %!      30 40; ...
112 %!      50 60; ...
113 %!      70 80];
114 %!  exp = [...
115 %!      20 30; ...
116 %!      40 50; ...
117 %!      60 70];
118 %!  mid = midPoint(p1, p2);
119 %!  assert (mid, exp);
120
121 %!test
122 %!  p1 = [30 40];
123 %!  p2 = [ ...
124 %!      30 40; ...
125 %!      50 60; ...
126 %!      70 80];
127 %!  exp = [...
128 %!      30 40; ...
129 %!      40 50; ...
130 %!      50 60];
131 %!  mid = midPoint(p1, p2);
132 %!  assert (mid, exp);
133
134 %!test
135 %!  p1 = [ ...
136 %!      10 20 ; ...
137 %!      30 40 ; ...
138 %!      50 60 ; ...
139 %!      ];
140 %!  p2 = [30 40];
141 %!  exp = [...
142 %!      20 30; ...
143 %!      30 40; ...
144 %!      40 50];
145 %!  mid = midPoint(p1, p2);
146 %!  assert (mid, exp);
147
148 %!test
149 %!  p1 = [ ...
150 %!      10 20 ; ...
151 %!      30 40 ; ...
152 %!      50 60 ; ...
153 %!      ];
154 %!  p2 = [30 40];
155 %!  expX = [20 ; 30 ; 40];
156 %!  expY = [30 ; 40 ; 50];
157 %!  [x y] = midPoint(p1, p2);
158 %!  assert (x, expX);
159 %!  assert (y, expY);
160
161 %!test
162 %!  edge = [10 20 30 40];
163 %!  exp = [20 30];
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);
170