function [isInside,lambda]=insideSegment(p1,p2,p,precision)
% function [isInside,lambda]=insideSegment(p1,p2,p,precision)
%
% This functions checks if point p belonging to the line defining by points p1 and p2
% are between then in the line
%
% Input parameters:
% p1,p2-> 2D points in cartesian coordinates defining a line, they are 2D column vectors
% p-> 2D point in cartesian coordinates in the line defined by p1 and p2, it is also a 2D column vector
% precision-> indicates the precision at which it is considered that points are inside a segment
%
% Output parameters:
% isInside-> it is a logic value, true (1) if p is between p1 and p2, and false (0) otherwise
% lambda-> lambda value that point p gets in line equation given by points p1 and p2
%
% Author: Antonio C. DomÃnguez Brito (adominguez@iusiani.ulpgc.es)
% Institution: SIANI-ULPGC (www.iusiani.ulpgc.es)
% Date: June 1st 2008
v=p2-p1;
if norm(v)<=precision % p1 and p2 are equal
isInside=(norm(p-p1)<=precision); lambda=0; return;
elseif abs(v(1))<=precision % vertical lines
lambda=(p(2)-p1(2))/v(2);
else % horizontal lines and generic case
lambda=(p(1)-p1(1))/v(1);
end
isInside= ((-precision<=lambda) & (lambda<=(1+precision)));
%if isInside fprintf('lambda=%.24f',lambda); end
return;