How do I compose a voronoi diagram?

1.6k Views Asked by At

I am trying to understand the algorithm of plotting a voronoi diagram. Here is a code I developed using whatever I could get off wikipedia. However the implementation is very slow and the complexity seems $n^2$. Is there a better approach? I am more concerned with the voronoi diagram using the manhattan distance.

close all;
% Voronoi script
p=[100,20;100,190;10,100;170,100;100,100;155,110];
voronoi(p(:,1),p(:,2))
axis([1,200,1,200])
n=zeros (200);
m=n;
d=zeros(size(p));
for i=1:200
    for j=1:200
        % euclidian
        d1=(sum((p-repmat([i,j],size(p,1),1)).^2,2)).^(0.5);   
        % manhattan
        d2=sum(abs(p-repmat([i,j],size(p,1),1)),2);
        idx1=find(d1==min(d1));
        idx2=find(d2==min(d2));
        if length(idx1)>1
            n(i,j)=0;
        else
            n(i,j)=idx1(1);
        end
        if length(idx2)>1
            m(i,j)=0;
        else
            m(i,j)=idx2(1);
        end
    end
end
figure;
axis([1,200,1,200])
%imagesc(flip(n'));
imagesc(n');
hold on;
pf=p;
scatter(pf(:,1),pf(:,2));

figure;
axis([1,200,1,200])
imagesc(m');
hold on;
pf=p;
scatter(pf(:,1),pf(:,2));