Home > matGeom > geom2d > isPointOnRay.m

isPointOnRay

PURPOSE ^

ISPOINTONRAY Test if a point belongs to a ray.

SYNOPSIS ^

function b = isPointOnRay(point, ray, varargin)

DESCRIPTION ^

ISPOINTONRAY Test if a point belongs to a ray.

   B = isPointOnRay(PT, RAY);
   Returns 1 if point PT belongs to the ray RAY.
   PT is given by [x y] and RAY by [x0 y0 dx dy].

   If PT is a N-by-2 array, and RAY is a M-by-4 array, then the result is
   a N-by-M array containing the result of each pair-wise test.

   B = isPointOnRay(PT, RAY, TOL);
   Specifies the tolerance to use for testing if point is on the ray.

   Example
     ray = [10 20 3 4];
     % test for a point on the ray
     p1 = [16 28]; 
     isPointOnRay(p1, ray)
     ans =
       logical
        0
     % test for a point on the supporting line but "before" the origin
     p2 = [7 16];
     isPointOnRay(p1, ray)
     ans =
       logical
        0
 
   See also:
   rays2d, points2d, isPointOnLine, isPointOnEdge

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function b = isPointOnRay(point, ray, varargin)
0002 %ISPOINTONRAY Test if a point belongs to a ray.
0003 %
0004 %   B = isPointOnRay(PT, RAY);
0005 %   Returns 1 if point PT belongs to the ray RAY.
0006 %   PT is given by [x y] and RAY by [x0 y0 dx dy].
0007 %
0008 %   If PT is a N-by-2 array, and RAY is a M-by-4 array, then the result is
0009 %   a N-by-M array containing the result of each pair-wise test.
0010 %
0011 %   B = isPointOnRay(PT, RAY, TOL);
0012 %   Specifies the tolerance to use for testing if point is on the ray.
0013 %
0014 %   Example
0015 %     ray = [10 20 3 4];
0016 %     % test for a point on the ray
0017 %     p1 = [16 28];
0018 %     isPointOnRay(p1, ray)
0019 %     ans =
0020 %       logical
0021 %        0
0022 %     % test for a point on the supporting line but "before" the origin
0023 %     p2 = [7 16];
0024 %     isPointOnRay(p1, ray)
0025 %     ans =
0026 %       logical
0027 %        0
0028 %
0029 %   See also:
0030 %   rays2d, points2d, isPointOnLine, isPointOnEdge
0031 %
0032 
0033 %   ---------
0034 %   author : David Legland
0035 %   INRA - TPV URPOI - BIA IMASTE
0036 %   created the 31/10/2003.
0037 %
0038 
0039 %   HISTORY
0040 %   07/07/2005 normalize condition to test if on the line and add support
0041 %       of multiple rays or points
0042 %   22/05/2009 rename to isPointOnRay, add psb to specify tolerance
0043 %   26/01/2010 was drawing a line before making test
0044 
0045 % extract computation tolerance
0046 tol = 1e-14;
0047 if ~isempty(varargin)
0048     tol = varargin{1};
0049 end
0050 
0051 % number of rays and points
0052 Nr = size(ray, 1);
0053 Np = size(point, 1);
0054 
0055 % if several rays or several points, adapt sizes of arrays
0056 x0 = repmat(ray(:,1)', Np, 1);
0057 y0 = repmat(ray(:,2)', Np, 1);
0058 dx = repmat(ray(:,3)', Np, 1);
0059 dy = repmat(ray(:,4)', Np, 1);
0060 xp = repmat(point(:,1), 1, Nr);
0061 yp = repmat(point(:,2), 1, Nr);
0062 
0063 % test if points belongs to the supporting line
0064 b1 = abs((xp-x0).*dy - (yp-y0).*dx) ./ (dx.*dx + dy.*dy) < tol;
0065 
0066 % check if points lie the good direction on the rays
0067 ind     = abs(dx) > abs(dy);
0068 t       = zeros(size(b1));
0069 t(ind)  = (xp(ind) - x0(ind)) ./ dx(ind);
0070 t(~ind) = (yp(~ind) - y0(~ind)) ./ dy(~ind);
0071 
0072 % combine the two tests
0073 b = b1 & (t >= 0);

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