% input n and calculate the n-gon
function [x, y] = calc_ngon(n)
% generate xs and ys initially fill zero
x = zeros(1, n);
y = zeros(1, n);
% calculate the degree of each point
theta = 2 * pi / n;
for i=1:n
deg = i * theta;
x(i) = cos(deg);
y(i) = sin(deg);
end

end

clear all; clr;
close all;
n = input(' Enter the number of mice: ');
file1.m:
% get the n-gon
[x, y] = calc_ngon(n);

figure(1);
plot([x x(1)], [y y(1)]);
title('mice');
xlim([-1 1]);
ylim([-1 1]);

d = 0.01;

file2.m:

% when two mice are close enough, terminal the while loop
finish_delta = 1e-1;

while true
% the new x of the mice
new_x = zeros(1, n);
% the new y of the mice
new_y = zeros(1, n);
for i=1:n
mice_x = x(i);
mice_y = y(i);
neigh_idx = mod(i, n)+1;
neigh_x = x(neigh_idx);
neigh_y = y(neigh_idx);
    line_vec = [(neigh_x-mice_x) (neigh_y-mice_y)];
    line_distance = sqrt(line_vec * line_vec');
    % calculate the new_x new_y of each mice
    new_x(i) = mice_x + d/line_distance*line_vec(1);
    new_y(i) = mice_y + d/line_distance*line_vec(2);
end
x = new_x;
y = new_y;
hold on;
scatter(x, y, 5);
% line between first and second mice
line_vec = [(x(2)-x(1)) (y(2)-y(1))];
distance = sqrt(line_vec * line_vec');

if distance < finish_delta
   break;
end
pause(0.1);
end

