function [curvature] = UkAvoid(k, n, coords, barrier, w)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
    curvature = 0;
%     for i = 1:n
%        if i == k
%        else
%            curvature = curvature + Ujk(i, k, coords);
%        end
%     end
    bsize = size(barrier);
    blength = bsize(1);
    
    x1 = coords(k, 1);
    y1 = coords(k, 2);
    theta1 = coords(k, 3);
    
    xsum = 0;
    ysum = 0;
    cutoff = 0;
    for i = 1:blength
        x2 = barrier(i, 1);
        y2 = barrier(i, 2);
        bdistance = sqrt((x2-x1)^2 + (y2-y1)^2);
        xsum = xsum + x2*C(bdistance, 0, w);
        ysum = ysum + y2*C(bdistance, 0, w);
        cutoff = cutoff + C(bdistance, 0, w);
    end
    
    if cutoff == 0
        nu = 0;
    else
        xaverage = xsum/blength;
        yaverage = ysum/blength;
        
        thetab = atan((yaverage-y1)/(xaverage-x1)) - theta1;
        if (xaverage - x1) < 0
            thetab = thetab + pi;
        end
        thetab = mod(thetab, 2*pi);
        
        if thetab < pi/2
            m = -1;
        elseif thetab < 3*pi/2
            m = 0;
        else
            m = 1;
        end
        nu = m*cos(thetab);
    end
    
    curvature = curvature + nu;
end

% OLD IMPLEMENTATION WITH AVERAGE TAKEN OF CURVATURE INSTEAD OF VECTOR
%
%     nu = 0;
%     for i = 1:blength
%         x1 = coords(k, 1);
%         y1 = coords(k, 2);
%         theta1 = coords(k, 3);
%         
%         x2 = barrier(i, 1);
%         y2 = barrier(i, 2);
%         
%         bdistance = sqrt((x2-x1)^2 + (y2-y1)^2);
%         thetab = atan((y2-y1)/(x2-x1)) - theta1;
%         if (x2 - x1) < 0
%             thetab = thetab + pi;
%         end
%         thetab = mod(thetab, 2*pi);
%         
%         if thetab < pi/2
%             m = -1;
%         elseif thetab < 3*pi/2
%             m = 0;
%         else
%             m = 1;
%         end
%         nu = nu + m*cos(thetab)*C(bdistance, 0, w);
%     end
%     curvature = curvature + nu;


