function z = Intersect(a,b)
% a and b are length-4 vectors that collectively name four points.
% If the line segment that connects (a(1),b(1)) and (a(2),b(2)) intersects
% the line segment that connects (a(3),b(3)) and (a(4),b(4)), then
% (z(1),z(2)) is the point of intersection. If the two line segments
% fail to intersect, then z is the empty vector. Assume that the four points
% are distinct and that the two line segments are not parallel.

d = (a(2)-a(1))*(b(3)-b(4)) - (b(2)-b(1))*(a(3)-a(4));
lambda = ((a(3)-a(1))*(b(3)-b(4)) - (b(3)-b(1))*(a(3)-a(4)))/d;
mu = ((a(2)-a(1))*(b(3)-b(1)) - (b(2)-b(1))*(a(3)-a(1)))/d;
if 0 <= lambda && lambda<=1 && 0<=mu && mu<=1
z = [a(1)+lambda*(a(2)-a(1)) b(1)+lambda*(b(2)-b(1))];
else
z = [];
end