function [dx, dy] = pointDiff(xyA, xyB)
%calculate the 'closest' difference of two points A and B
%with rrespect of periodic space
%global SIZE_X, SIZE_Y required
%Syntax:
%   [dx, dy] = pointDiff(xyA, xyB);
% or
%   dxy = pointDiff(xyA, xyB);

global SIZE_X SIZE_Y
x = xyA(1);
y = xyA(2);
x = [-SIZE_X+x x x+SIZE_X ];
y = [-SIZE_Y+y y y+SIZE_Y ]';
dxy = repmat(x-xyB(1), 3, 1).^2 + repmat(y-xyB(2), 1, 3).^2;
[mn, diy] = min(dxy);  
[~, dix] = min(mn);  
diy = diy(dix);
dx = [x(dix) y(diy)] - xyB;
if nargout == 2
    dy = dx(2);
    dx = dx(1);
end


    