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 % E-mail: david.legland@inrae.fr 0030 % Created: 2003-10-31 0031 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE 0032 0033 % extract tolerance option 0034 tol = 1e-14; 0035 if ~isempty(varargin) 0036 tol = varargin{1}; 0037 end 0038 0039 % number of lines and edges 0040 nLines = size(line, 1); 0041 nEdges = size(edge, 1); 0042 0043 % origin and direction vector of lines 0044 lx0 = line(:,1); 0045 ly0 = line(:,2); 0046 ldx = line(:,3); 0047 ldy = line(:,4); 0048 0049 % origin and direction vector of edges 0050 ex1 = edge(:,1); 0051 ey1 = edge(:,2); 0052 ex2 = edge(:,3); 0053 ey2 = edge(:,4); 0054 edx = ex2 - ex1; 0055 edy = ey2 - ey1; 0056 0057 % normalizes direction vectors 0058 ldn = hypot(ldx, ldy); 0059 ldx = ldx ./ ldn; 0060 ldy = ldy ./ ldn; 0061 0062 % normalizes direction vectors 0063 edgeLength = hypot(edx, edy); 0064 edx = edx ./ edgeLength; 0065 edy = edy ./ edgeLength; 0066 0067 % indices of parallel lines 0068 par = abs(ldx .* edy - edx .* ldy) < tol; 0069 0070 % indices of colinear lines 0071 col = abs((ex1-lx0) .* ldy - (ey1-ly0) .* ldx) < tol & par ; 0072 0073 xi(col) = Inf; 0074 yi(col) = Inf; 0075 xi(par & ~col) = NaN; 0076 yi(par & ~col) = NaN; 0077 0078 i = ~par; 0079 0080 % compute intersection points 0081 if nLines == nEdges 0082 xi(i) = ((ey1(i)-ly0(i)).*ldx(i).*edx(i) + lx0(i).*ldy(i).*edx(i) - ex1(i).*edy(i).*ldx(i)) ./ ... 0083 (edx(i).*ldy(i)-ldx(i).*edy(i)) ; 0084 yi(i) = ((ex1(i)-lx0(i)).*ldy(i).*edy(i) + ly0(i).*ldx(i).*edy(i) - ey1(i).*edx(i).*ldy(i)) ./ ... 0085 (ldx(i).*edy(i)-edx(i).*ldy(i)) ; 0086 elseif nLines == 1 0087 xi(i) = ((ey1(i)-ly0).*ldx.*edx(i) + lx0.*ldy.*edx(i) - ex1(i).*edy(i).*ldx) ./ ... 0088 (edx(i).*ldy-ldx.*edy(i)) ; 0089 yi(i) = ((ex1(i)-lx0).*ldy.*edy(i) + ly0.*ldx.*edy(i) - ey1(i).*edx(i).*ldy) ./ ... 0090 (ldx.*edy(i)-edx(i).*ldy) ; 0091 elseif nEdges == 1 0092 xi(i) = ((ey1-ly0(i)).*ldx(i).*edx + lx0(i).*ldy(i).*edx - ex1(i).*edy.*ldx(i)) ./ ... 0093 (edx.*ldy(i)-ldx(i).*edy) ; 0094 yi(i) = ((ex1-lx0(i)).*ldy(i).*edy + ly0(i).*ldx(i).*edy - ey1(i).*edx.*ldy(i)) ./ ... 0095 (ldx(i).*edy-edx.*ldy(i)) ; 0096 end 0097 0098 % format output arguments 0099 point = [xi' yi']; 0100 0101 % compute position of points projected on the supporting line, by using 0102 % dot product and normalising by edge length 0103 pos = bsxfun(@rdivide, ... 0104 bsxfun(@times, bsxfun(@minus, xi, ex1), edx) + ... 0105 bsxfun(@times, bsxfun(@minus, yi, ey1), edy), edgeLength); 0106 0107 % set coordinates of points outside edge to NaN 0108 out = pos < -tol | pos > (1+tol); 0109 point(out, :) = NaN;