function [xRound,yRound]=textSpace(x,y,xOverlap,yOverlap)

% This function removes and/or shifts overlapping x & y coordinates
% Inputs:
% x = x-coordinates
% y = y-coordinates
% xOverlap = maximum allowed overlap of x-coords with the same y-coords
% yOverlap = maximum allowed overlap of y-coords with the same x-coords

% x=meanRatings.ValenceMean;
% y=meanRatings.ArousalMean;
% xOverlap=.4;
% yOverlap=.1;

xRound=round(x,1); % Round x-coords to 1 decimal point 
yRound=round(y,1); % Round y-coords to 1 decimal point 

% Delete overlapping labels with the same x & y coords
for xx=1:length(x)
    ind=find(xRound==xRound(xx) & yRound==yRound(xx));
    if numel(ind)>1
        for ii=2:numel(ind)
            xRound(ind(ii))=NaN;
            yRound(ind(ii))=NaN;
        end        
    end   
end

sum(isnan(xRound))

% For each .1 increment of overlap, find overlapping y values and remove them 
for xx=1:length(x)
    for jj=.1:.1:yOverlap
        yClose=round(yRound(xx)-jj,1);        
        ind=find(xRound==xRound(xx) & yRound==yClose);
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=NaN;
                yRound(ind(ii))=NaN;
            end
        end
    end
end

sum(isnan(xRound))

% For each .1 increment of overlap, find overlapping y values and remove them 
for xx=1:length(x)
    for jj=.1:.1:yOverlap
        yClose=round(yRound(xx)+jj,1);        
        ind=find(xRound==xRound(xx) & yRound==yClose);
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=NaN;
                yRound(ind(ii))=NaN;
            end
        end
    end
end

sum(isnan(xRound))

% For each .1 increment of overlap, find overlapping x values and shift them 
for xx=1:length(x)
    for jj=.1:.1:xOverlap
        xClose=round(xRound(xx)-jj,1);        
        ind=find(xRound==xClose & yRound==yRound(xx));
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=round(xRound(xx)-2*xOverlap,1);
            end
        end
    end
end

% For each .1 increment of overlap, find overlapping x values and shift them 
for xx=1:length(x)
    for jj=.1:.1:xOverlap
        xClose=round(xRound(xx)+jj,1);        
        ind=find(xRound==xClose & yRound==yRound(xx));
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=round(xRound(xx)+2*xOverlap,1);
            end
        end
    end
end

% For each .1 increment of overlap, find overlapping x values and remove them 
for xx=1:length(x)
    for jj=.1:.1:xOverlap+.1
        xClose=round(xRound(xx)-jj,1);        
        ind=find(xRound==xClose & yRound==yRound(xx));
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=NaN;
                yRound(ind(ii))=NaN;
            end
        end
    end
end
sum(isnan(xRound))

% For each .1 increment of overlap, find overlapping x values and remove them 
for xx=1:length(x)
    for jj=.1:.1:xOverlap+.1
        xClose=round(xRound(xx)+jj,1);        
        ind=find(xRound==xClose & yRound==yRound(xx));
        if numel(ind)>0
            for ii=1:numel(ind)
                xRound(ind(ii))=NaN;
                yRound(ind(ii))=NaN;
            end
        end
    end
end
sum(isnan(xRound))

% Delete overlapping labels with the same x & y coords
for xx=1:length(x)
    ind=find(xRound==xRound(xx) & yRound==yRound(xx));
    if numel(ind)>1
        for ii=2:numel(ind)
            xRound(ind(ii))=NaN;
            yRound(ind(ii))=NaN;
        end        
    end   
end

sum(isnan(xRound))

end