INTERSECTLINEEDGE Return intersection between a line and an edge. P = intersectLineEdge(LINE, EDGE); returns the intersection point of lines LINE and edge EDGE. LINE is a 1-by-4 array containing parametric representation of the line (in the form [x0 y0 dx dy], see 'createLine' for details). EDGE is a 1-by-4 array containing the coordinates of first and second points (in the form [x1 y1 x2 y2], see 'createEdge' for details). In case of colinear line and edge, returns [Inf Inf]. If line does not intersect edge, returns [NaN NaN]. If each input is N-by-4 array, the result is a N-by-2 array containing intersections for each couple of edge and line. If one of the input has N rows and the other 1 row, the result is a N-by-2 array. P = intersectLineEdge(LINE, EDGE, TOL); Specifies the tolerance option for determining if a point belongs to an edge and if lines are parallel. See also: lines2d, edges2d, intersectEdges, intersectLines
0001 function point = intersectLineEdge(line, edge, varargin) 0002 %INTERSECTLINEEDGE Return intersection between a line and an edge. 0003 % 0004 % P = intersectLineEdge(LINE, EDGE); 0005 % returns the intersection point of lines LINE and edge EDGE. 0006 % LINE is a 1-by-4 array containing parametric representation of the line 0007 % (in the form [x0 y0 dx dy], see 'createLine' for details). 0008 % EDGE is a 1-by-4 array containing the coordinates of first and second 0009 % points (in the form [x1 y1 x2 y2], see 'createEdge' for details). 0010 % 0011 % In case of colinear line and edge, returns [Inf Inf]. 0012 % If line does not intersect edge, returns [NaN NaN]. 0013 % 0014 % If each input is N-by-4 array, the result is a N-by-2 array containing 0015 % intersections for each couple of edge and line. 0016 % If one of the input has N rows and the other 1 row, the result is a 0017 % N-by-2 array. 0018 % 0019 % P = intersectLineEdge(LINE, EDGE, TOL); 0020 % Specifies the tolerance option for determining if a point belongs to an 0021 % edge and if lines are parallel. 0022 % 0023 % See also: 0024 % lines2d, edges2d, intersectEdges, intersectLines 0025 % 0026 0027 % --------- 0028 % author : David Legland 0029 % INRA - TPV URPOI - BIA IMASTE 0030 % created the 31/10/2003. 0031 % 0032 0033 % HISTORY 0034 % 19/02/2004: add support for multiple lines. 0035 % 08/03/2007: update doc 0036 0037 % extract tolerance option 0038 tol = 1e-14; 0039 if ~isempty(varargin) 0040 tol = varargin{1}; 0041 end 0042 0043 % number of lines and edges 0044 nLines = size(line, 1); 0045 nEdges = size(edge, 1); 0046 0047 % origin and direction vector of lines 0048 lx0 = line(:,1); 0049 ly0 = line(:,2); 0050 ldx = line(:,3); 0051 ldy = line(:,4); 0052 0053 % origin and direction vector of edges 0054 ex1 = edge(:,1); 0055 ey1 = edge(:,2); 0056 ex2 = edge(:,3); 0057 ey2 = edge(:,4); 0058 edx = ex2 - ex1; 0059 edy = ey2 - ey1; 0060 0061 % normalizes direction vectors 0062 ldn = hypot(ldx, ldy); 0063 ldx = ldx ./ ldn; 0064 ldy = ldy ./ ldn; 0065 0066 % normalizes direction vectors 0067 edgeLength = hypot(edx, edy); 0068 edx = edx ./ edgeLength; 0069 edy = edy ./ edgeLength; 0070 0071 % indices of parallel lines 0072 par = abs(ldx .* edy - edx .* ldy) < tol; 0073 0074 % indices of colinear lines 0075 col = abs((ex1-lx0) .* ldy - (ey1-ly0) .* ldx) < tol & par ; 0076 0077 xi(col) = Inf; 0078 yi(col) = Inf; 0079 xi(par & ~col) = NaN; 0080 yi(par & ~col) = NaN; 0081 0082 i = ~par; 0083 0084 % compute intersection points 0085 if nLines == nEdges 0086 xi(i) = ((ey1(i)-ly0(i)).*ldx(i).*edx(i) + lx0(i).*ldy(i).*edx(i) - ex1(i).*edy(i).*ldx(i)) ./ ... 0087 (edx(i).*ldy(i)-ldx(i).*edy(i)) ; 0088 yi(i) = ((ex1(i)-lx0(i)).*ldy(i).*edy(i) + ly0(i).*ldx(i).*edy(i) - ey1(i).*edx(i).*ldy(i)) ./ ... 0089 (ldx(i).*edy(i)-edx(i).*ldy(i)) ; 0090 elseif nLines == 1 0091 xi(i) = ((ey1(i)-ly0).*ldx.*edx(i) + lx0.*ldy.*edx(i) - ex1(i).*edy(i).*ldx) ./ ... 0092 (edx(i).*ldy-ldx.*edy(i)) ; 0093 yi(i) = ((ex1(i)-lx0).*ldy.*edy(i) + ly0.*ldx.*edy(i) - ey1(i).*edx(i).*ldy) ./ ... 0094 (ldx.*edy(i)-edx(i).*ldy) ; 0095 elseif nEdges == 1 0096 xi(i) = ((ey1-ly0(i)).*ldx(i).*edx + lx0(i).*ldy(i).*edx - ex1(i).*edy.*ldx(i)) ./ ... 0097 (edx.*ldy(i)-ldx(i).*edy) ; 0098 yi(i) = ((ex1-lx0(i)).*ldy(i).*edy + ly0(i).*ldx(i).*edy - ey1(i).*edx.*ldy(i)) ./ ... 0099 (ldx(i).*edy-edx.*ldy(i)) ; 0100 end 0101 0102 % format output arguments 0103 point = [xi' yi']; 0104 0105 % compute position of points projected on the supporting line, by using 0106 % dot product and normalising by edge length 0107 pos = bsxfun(@rdivide, ... 0108 bsxfun(@times, bsxfun(@minus, xi, ex1), edx) + ... 0109 bsxfun(@times, bsxfun(@minus, yi, ey1), edy), edgeLength); 0110 0111 % set coordinates of points outside edge to NaN 0112 out = pos < -tol | pos > (1+tol); 0113 point(out, :) = NaN;