% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% function_calculateAngle calculates the angle of every point existing is  %
% "SetOfPoints" regarding the "PointOfInterest". This is the angle that    %
% both points make with the x axis:                                        %
%       Example:                                                           %
%       ==============================================================     %
%       ||2nd Q  |                                      1st Quadrant||     %
%       ||       |                                                  ||     %
%       ||       |PointOfInterest                                   ||     %
%       ||-------x----)----------------------------------------XAxis||     %
%       ||       |\***) <---- Angle                                 ||     %
%       ||       | \*)                                              ||     %
%       ||       |  \                                               ||     %
%       ||       |   \                                              ||     %
%       ||       |    \  SetOfPoint(i)                              ||     %
%       ||       |     x                                            ||     %
%       ||     YAxis                                                ||     %
%       ||3rd Q  |                                      4th Quadrant||     %
%       ==============================================================     %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %

%  INPUTS:   Name           | Type    | Dimensions | Description
%            PointOfInterest| 1Point  | 1x2        | Angle to Study
%            SetOfPoints    | nPoints | nx2        | Points to Calculate Angle from
%
% OTUPUTS:   Name           | Type    | Dimensions | Description
%            t              | int     | 1x1        | Nbr of Calculated Angles
%            Angle          | nAngles | 1xt        | Calculated Angles



function Angle = function_calculateAngle(PointOfInterest, SetOfPoints)
    t = 0;
    for i = 1:size(SetOfPoints,1);
        if((PointOfInterest(1,1) ~= SetOfPoints(i,1)) || (PointOfInterest(1,2) ~= SetOfPoints(i,2)))
        % If the points are not the same, then we can calculate the angle
        % between them.
            t = t + 1;                      % Increment in the number of calculated angles
            xPoI = PointOfInterest(1,1);    % x of point of interest
            yPoI = PointOfInterest(1,2);    % y of point of interest

            xSoP = SetOfPoints(i,1);        % x of a given point
            ySoP = SetOfPoints(i,2);        % y of a given point
            
            if(xSoP >=  xPoI)
            % If x of point in study is greater than the x of the point of
            % interest, we are in the 1st or 4th quadrant, then:
            %           yPoI - ySoP
            % Angle = --------------- * 180 / pi ;
            %         | yPoI - ySoP |
                Angle(t) = atan((yPoI - ySoP)/abs(xPoI - xSoP)) * 180 / pi;
            else
            % If x of point in study is lesser than the x of the point of
            % interest, we are in the 2nd or 3rd quadrant, then:
            %         (     | xPoI - xSoP |           )
            % Angle = (90 + --------------- * 180 / pi) * sign(yPoI - ySoP);
            %         (     | yPoI - ySoP |           )
                Angle(t) = ( 90 + atan((xPoI - xSoP)/abs(yPoI - ySoP)) * 180 / pi ) * sign(yPoI - ySoP);
            end
            if(abs(Angle(t) > 180))
                Angle(t) = Angle(t) - 360*sign(Angle(t));
            end
        end
    end
end