function PEng7_7

% points in plane
p1 = struct('x',1,'y',-2);
p2.x = 3;
p2.y = -4;

d = dist(p1,p2);

fprintf('Distance between (%.4f,%.4f) and (%.4f,%.4f) is %.4f\n',p1.x,p1.y,p2.x,p2.y,d);

% points on a line
p3 = struct('x',1);
p4 = struct('x',3);

d = dist(p3,p4);

fprintf('Distance between %.4f and %.4f is %.4f\n',p1.x,p2.x,d);

end


% DIST    computes the distance between two points in the plane or
% on a line. 
% DIST(P1,P2) computes distance betwwen P1 and P2. P1 and P2 should be 
% structures with only .x or .x and .y fileds.


function f=dist(p1,p2)

% 1)
% if(~isfield('y',p1) && ~isfield('y',p2))
%     f = abs(p1.x-p2.x);
% else
%     f = sqrt((p1.x-p2.x)^2+(p1.y-p2.y)^2);
% end

% 2)
M1 = fieldnames(p1);
M2 = fieldnames(p2);

if(numel(M1)~=numel(M2))
    error('can not find the distance');
else
    if(numel(M1)==1 && numel(M2)==1)
        f = abs(p1.x-p2.x);
    else
        f = sqrt((p1.x-p2.x)^2+(p1.y-p2.y)^2);
    end
end



end
