function [ri,ii_between]=line_intersect(x0,y0,phi0,x,y)
% Intersection of line from (x0,y0) in direction phi0 with lines
% from x(:,1),y(:,1) to x(:,2),y(:,2).

% Translate x0,y0 to origin and make phi in pos y direction:
nrow=size(x,1);
x1=x - x0; y1=y - y0; rng=sqrt(x1.^2 + y1.^2); brg=atan2(x1,y1);
x2=rng.*sin(brg-phi0); y2=rng.*cos(brg-phi0);

ri=zeros(nrow,1);
iiok=find(x2(:,1) ~= x2(:,2)); nok=length(iiok);
if nok>0,
	ri(iiok)=y2(iiok,1) - x2(iiok,1).*(y2(iiok,2)-y2(iiok,1))./ ...
		(x2(iiok,2)-x2(iiok,1));
end
if nok<nrow,
	iibad=find(x2(:,1) == x2(:,2));
	ri(iibad)=Inf;
end

ii_between=(ri>=0 & x2(:,1).*x2(:,2)<=0);

%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
