%c1da = [(randn(25,1))-2 .5*randn(25,1)];
%c1db = [(randn(25,1))-5 .5*randn(25,1)-1];
%c1dc = [(randn(25,1))-5 .5*randn(25,1)-2];
%c1dd = [(randn(25,1))-2 .5*randn(25,1)-3];
%c1d =  [c1da;c1db;c1dc;c1dd];
%c2d = randn(100,2)+1.5;
c1d = point(1:450, :);
c2d = stop(1:450,:);

data = [c1d;c2d];

dsize = size(data);
dlen = dsize(1);
d1size = size(c1d);
d1len = d1size(1);
d2size = size(c2d);
d2len = d1size(1);
path1 = [];
path2 = [];


nclust = 40; 

c1 = [0 0];
c2 = [.5 -.5];

clust = ones(nclust,1)*zeros(1,dsize(2));

for it = 1:8
it
newc = ones(nclust, 1)*zeros(1,dsize(2));
cct = zeros(nclust,1);

for n = 1:nclust
	clust(n,:) = data(n,:);
end

for p = 1:dlen
	d = norm(data(p,:)-clust(1,:));
	dmin = d;
	whichcluster = 1;
	for n = 1:nclust
		d = norm(data(p,:)-clust(n,:));
		if(d < dmin)
			dmin = d;
			whichcluster = n;
		end
	end
	newc(whichcluster,:) = newc(whichcluster,:)+data(p,:);
	cct(whichcluster) = cct(whichcluster)+1;
end

for n = 1:nclust
	clust(n,:) = newc(n,:)./cct(n);
end
end
hold off;
plot(clust(:,1), clust(:,2), 'magentao');
hold on;
plot(c1d(:,1), c1d(:,2), 'red*');
plot(c2d(:,1), c2d(:,2), 'blue*');

c1d = point;
c2d = stop;

data = [c1d;c2d];

dsize = size(data);
dlen = dsize(1);
d1size = size(c1d);
d1len = d1size(1);
d2size = size(c2d);
d2len = d1size(1);
path1 = [];
path2 = [];

for p = 1:dlen
	d = norm(data(p,:)-clust(1,:));
	dmin = d;
	whichcluster = 1;
	for n = 1:nclust
		d = norm(data(p,:)-clust(n,:));
		if(d < dmin)
			dmin = d;
			whichcluster = n;
		end
	end
	categ(p) = whichcluster;
%	text(data(p,1)+.08, data(p,2), num2str(whichcluster));
end
return;

hold off;
plot(0,0,'white.');
hold on;
for p = 1:d2len
	d1 = norm(c2d(p,:)-c1);
	d2 = norm(c2d(p,:)-c2);
	if(d1 < d2)
		plot(c2d(p,1), c2d(p,2), 'black+');
	else
		plot(c2d(p,1), c2d(p,2), 'black*');
	end
end
for p = 1:d1len
	d1 = norm(c1d(p,:)-c1);
	d2 = norm(c1d(p,:)-c2);
	if(d1 < d2)
		plot(c1d(p,1), c1d(p,2), 'red*');
	else
		plot(c1d(p,1), c1d(p,2), 'red+');
	end
end
plot(path1(:,1), path1(:,2), 'red')
plot(path2(:,1), path2(:,2), 'black')
plot(c1(:,1), c1(:,2), 'redo');
plot(c2(:,1), c2(:,2), 'blacko');
c1
c2
