Home > matGeom > geom2d > midPoint.m

midPoint

PURPOSE ^

MIDPOINT Middle point of two points or of an edge.

SYNOPSIS ^

function varargout = midPoint(varargin)

DESCRIPTION ^

MIDPOINT Middle point of two points or of an edge.

   MID = midPoint(P1, P2)
   Compute the middle point of the two points P1 and P2.

   MID = midPoint(EDGE)
   Compute the middle point of the edge given by EDGE.
   EDGE has the format: [X1 Y1 X2 Y2], and MID has the format [XMID YMID],
   with XMID = (X1+X2)/2, and YMID = (Y1+Y2)/2.

   [MIDX MIDY] = midPoint(...)
   Return the result as two separate variables or arrays.

   Works also when EDGE is a N-by-4 array, in this case the result is a
   N-by-2 array containing the midpoint of each edge.


   Example
   P1 = [10 20];
   P2 = [30 40];
   midPoint([P1 P2])
   ans =
       20  30

   See also
   edges2d, points2d

 ------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 Created: 2010-08-06,    using Matlab 7.9.0.529 (R2009b)
 Copyright 2010 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = midPoint(varargin)
0002 %MIDPOINT Middle point of two points or of an edge.
0003 %
0004 %   MID = midPoint(P1, P2)
0005 %   Compute the middle point of the two points P1 and P2.
0006 %
0007 %   MID = midPoint(EDGE)
0008 %   Compute the middle point of the edge given by EDGE.
0009 %   EDGE has the format: [X1 Y1 X2 Y2], and MID has the format [XMID YMID],
0010 %   with XMID = (X1+X2)/2, and YMID = (Y1+Y2)/2.
0011 %
0012 %   [MIDX MIDY] = midPoint(...)
0013 %   Return the result as two separate variables or arrays.
0014 %
0015 %   Works also when EDGE is a N-by-4 array, in this case the result is a
0016 %   N-by-2 array containing the midpoint of each edge.
0017 %
0018 %
0019 %   Example
0020 %   P1 = [10 20];
0021 %   P2 = [30 40];
0022 %   midPoint([P1 P2])
0023 %   ans =
0024 %       20  30
0025 %
0026 %   See also
0027 %   edges2d, points2d
0028 %
0029 % ------
0030 % Author: David Legland
0031 % e-mail: david.legland@grignon.inra.fr
0032 % Created: 2010-08-06,    using Matlab 7.9.0.529 (R2009b)
0033 % Copyright 2010 INRA - Cepia Software Platform.
0034 
0035 if nargin == 1
0036     % input is an edge
0037     edge = varargin{1};
0038     mid = [mean(edge(:, [1 3]), 2) mean(edge(:, [2 4]), 2)];
0039     
0040 elseif nargin == 2
0041     % input are two points
0042     p1 = varargin{1};
0043     p2 = varargin{2};
0044     
0045     % assert inputs are equal
0046     n1 = size(p1, 1);
0047     n2 = size(p2, 1);
0048     if n1~=n2 && min(n1, n2)>1
0049         error('geom2d:midPoint', ...
0050             'Inputs must have same size, or one must have length 1');
0051     end
0052     
0053     % compute middle point
0054     mid = bsxfun(@plus, p1, p2) / 2;
0055 end
0056 
0057 % process output arguments
0058 if nargout<=1
0059     varargout{1} = mid;
0060 else
0061     varargout = {mid(:,1), mid(:,2)};
0062 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019