function [curvature] = UkAvoidTest( k, n, coords, barrier, targetx, targety, w)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
    gamma = 100;
    
    xk = coords(k, 1);
    yk = coords(k, 2);
    thetak = coords(k, 3);
    
    relative_barrier = barrier;
    relative_barrier(:, 1) = barrier(:,1) - xk;
    relative_barrier(:, 2) = barrier(:,2) - yk;
    
    x_unit_vector = [cos(thetak), sin(thetak)];
    y_unit_vector = [cos(thetak + pi/2), sin(thetak + pi/2)];
    
    b_minus_y = relative_barrier;
    b_minus_y(:,1) = relative_barrier(:,1) - y_unit_vector(1);
	b_minus_y(:,2) = relative_barrier(:,2) - y_unit_vector(2);

    bsize = size(barrier);
    blength = bsize(1);
    cutoff = barrier;
    for i = 1:blength
        distance = norm(b_minus_y(i,:), 2);
        cutoff(i, 1) = C(distance, 0, w);
    end
    cutoff(:,2) = cutoff (:, 1);
    b_minus_y_cutoff = b_minus_y.*cutoff;
    nu_k = sum(b_minus_y_cutoff, 1);
    if norm(nu_k,2) == 0
        nu_k_normal = [0, 0];
    else
        nu_k_normal = nu_k./norm(nu_k,2);
    end
    avoid_curvature = 2*sign(dot(nu_k_normal, -1*x_unit_vector))*dot(nu_k_normal, y_unit_vector);
    seek_curvature = gamma*UjkSeek(targetx, targety, xk, yk, thetak);
    swarm_curvature = 0;
    for i = 1:n
       if i == k
       else
           xj = coords(i, 1);
           yj = coords(i, 2);
           rlength = sqrt((xj-xk)^2 + (yj-yk)^2);
%            if avoid_curvature == 0
%                swarm_curvature = swarm_curvature + 3*Ujk(i, k, coords);
%            else
%                swarm_curvature = swarm_curvature + C(rlength, 0, w)*UjkSeek(xj, yj, xk, yk, thetak);
%            end
           swarm_curvature = swarm_curvature + C(rlength, 0, w)*UjkSeek(xj, yj, xk, yk, thetak);          
       end
    end
    curvature = seek_curvature + avoid_curvature + swarm_curvature;
end

%     for i = 1:blength
%         x2 = barrier(i, 1);
%         y2 = barrier(i, 2);
%         bdistance = sqrt((x2-xk)^2 + (y2-yk)^2);
%         if bdistance < minbdist
%             minbdist = bdistance;
%         end
%         xsum = xsum + x2*C(bdistance, 0, w);
%         ysum = ysum + y2*C(bdistance, 0, w);
%         numberseen = numberseen + C(bdistance, 0, w);
%     end
%     
%     if numberseen == 0
%         nu = 0;
%     else
%         xaverage = xsum/numberseen;
%         yaverage = ysum/numberseen;
%         
%         thetab = atan((yaverage-yk)/(xaverage-xk)) - thetak;
%         if (xaverage - xk) < 0
%             thetab = thetab + pi;
%         end
%         thetab = mod(thetab, 2*pi);
%         
%         if thetab < pi
%             m = -1;
%         else
%             m = 1;
%         end
%         nu = m*cos(thetab);
%     end  
