1## Copyright (C) 2021 David Legland 2## All rights reserved. 3## 4## Redistribution and use in source and binary forms, with or without 5## modification, are permitted provided that the following conditions are met: 6## 7## 1 Redistributions of source code must retain the above copyright notice, 8## this list of conditions and the following disclaimer. 9## 2 Redistributions in binary form must reproduce the above copyright 10## notice, this list of conditions and the following disclaimer in the 11## documentation and/or other materials provided with the distribution. 12## 13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' 14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23## 24## The views and conclusions contained in the software and documentation are 25## those of the authors and should not be interpreted as representing official 26## policies, either expressed or implied, of the copyright holders. 27 28function line = medianLine(varargin) 29%MEDIANLINE Create a median line between two points. 30% 31% L = medianLine(P1, P2); 32% Create the median line of points P1 and P2, that is the line containing 33% all points located at equal distance of P1 and P2. 34% 35% L = medianLine(PTS); 36% Creates the median line of 2 points, given as a 2*2 array. Array has 37% the form: 38% [ [ x1 y1 ] ; [ x2 y2 ] ] 39% 40% L = medianLine(EDGE); 41% Creates the median of the edge. Edge is a 1*4 array, containing [X1 Y1] 42% coordinates of first point, and [X2 Y2], the coordinates of the second 43% point. 44% 45% Example 46% % Draw the median line of two points 47% P1 = [10 20]; 48% P2 = [30 50]; 49% med = medianLine(P1, P2); 50% figure; axis square; axis([0 100 0 100]); 51% drawEdge([P1 P2], 'linewidth', 2, 'color', 'k'); 52% drawLine(med) 53% 54% % Draw the median line of an edge 55% P1 = [50 60]; 56% P2 = [80 30]; 57% edge = createEdge(P1, P2); 58% figure; axis square; axis([0 100 0 100]); 59% drawEdge(edge, 'linewidth', 2) 60% med = medianLine(edge); 61% drawLine(med) 62% 63% 64% See also: 65% lines2d, createLine, orthogonalLine 66% 67% --------- 68% author : David Legland 69% INRA - TPV URPOI - BIA IMASTE 70% created the 31/10/2003. 71% 72 73% history 74% 2010-08-06 vectorize and change behaviour for N-by-4 inputs 75 76nargs = length(varargin); 77 78if nargs == 1 79 tab = varargin{1}; 80 if size(tab, 2)==2 81 % input is an array of two points 82 x0 = tab(1,1); 83 y0 = tab(1,2); 84 dx = tab(2,1)-x0; 85 dy = tab(2,2)-y0; 86 else 87 % input is an edge 88 x0 = tab(:, 1); 89 y0 = tab(:, 2); 90 dx = tab(:, 3) - tab(:, 1); 91 dy = tab(:, 4) - tab(:, 2); 92 end 93 94elseif nargs==2 95 % input is given as two points, or two point arrays 96 p1 = varargin{1}; 97 p2 = varargin{2}; 98 x0 = p1(:, 1); 99 y0 = p1(:, 2); 100 dx = bsxfun(@minus, p2(:, 1), x0); 101 dy = bsxfun(@minus, p2(:, 2), y0); 102 103else 104 error('Too many input arguments'); 105end 106 107% compute median using middle point of the edge, and the direction vector 108% rotated by 90 degrees counter-clockwise 109line = [bsxfun(@plus, x0, dx/2), bsxfun(@plus, y0, dy/2), -dy, dx]; 110