% This function is used to obtain the limits of the four corners with respect to the centroid (X, Y) and the object's area.

% INPUT PARAMETERS:
% - "x":    Centroid X-coordinate.
% - "y":    Centroid Y-coordinate.
% - "maxx": Maximum value for the X-coordinate (upper limit of the X-axis / image width).
% - "maxy": Maximum value for the Y-coordinate (upper limit of the Y-axis / image height).
% - "r":    Area of the object.

% OUTPUT VARIABLE:
% - "mnX": Lower X-coordinate
% - "mxX": Larger X-coordinate
% - "mnY": Lower Y-coordinate
% - "mxY": Larger Y-coordinate

function [mnX,mxX,mnY,mxY]= FucCordenada(x,y,maxx,maxy,r)
    % Limit Y around the centroid; clip to [1, maxy].
    if y+round(r/2)>=maxy % max Y
        mxY=maxy;
    else
        mxY=y+round(r/2);
    end
    if y-round(r/2)<=1 % min Y
        mnY=1;
    else
        mnY=y-round(r/2);
    end
    % Limit X around the centroid; clip to [1, maxX].
    if x+round(r/2)>=maxx % max x 
        mxX=maxx;
    else
        mxX=x+round(r/2);
    end
    if x-round(r/2)<=1 %min x 
        mnX=1;
    else
        mnX=x-round(r/2);
    end
    % Ensures integer indices
    mnX=round(mnX);
    mxX=round(mxX);
    mnY=round(mnY);
    mxY=round(mxY);
end