

% Plot commands for the 2D voronoi polyhedra.

%Created by Jim Work, University of South Carolina, August 13th, 2003

function vor2dplot(M)

currentMax = 0;
j=0;

for i=1:(size(M,1))
    for j=1:(size(M,1))
        if dist2d(M(i,2), M(i,3), M(j,2), M(j,3))>currentMax
            
            currentMax = dist2d(M(i,2), M(i,3), M(j,2), M(j,3));
        end
    end
end

radius = currentMax;

% find the center of mass by summing the x, and y values of each point,
% then dividing by the number of points

currentX = 0;
currentY = 0;


for i=1:(size(M,1))
    currentX = (M(i,2))+currentX;
    currentY = (M(i,3))+currentY;
    
end

centerX = currentX/(size(M,1));
centerY = currentY/(size(M,1));

center = [centerX, centerY];

% define a regular 20-gon, centered at the origin with radius of one.
BOUND = [10 0; 9.510565163 3.090169944; 8.090169943 5.877852524;...
5.877852522 8.090169944; 3.090169938 9.510565165;...
0 10; -3.090169938 9.510565165;...
-5.877852522 8.090169944; -8.090169943 5.877852524;...
-9.510565163 3.090169944; -10 0;...
-9.510565163 -3.090169944; -8.090169943, -5.877852524;...
-5.877852522 -8.090169944; -3.090169938 -9.510565165;...
0 -10; 3.090169938 -9.510565165;...
5.877852522 -8.090169944; 8.090169943 -5.877852524;...
9.510565163 -3.090169944];


% explode BOUND about the origin

BOUND = BOUND*radius*1.5; 


%M = remindex(M);

M = (M(:,2:3));

for i = 1:size(BOUND,1)
    BOUND(i,1)=BOUND(i,1)+centerX;
    BOUND(i,2)=BOUND(i,2)+centerY;
end
F = [M; BOUND];


K = voronoi(F(:,1), F(:,2),'k');

% Now we zoom in to see the original target points (thus effectively ignoring
% the bounding box).

zoom(6.5);

%This code (below) is a collection of artefacts from the 3d case, and should probably be deleted.

%hold on;
%figure(1)
%for hull = 1:size(M,1);
    % For each volume 'hull'
%    X = V(C{hull},:);     % View hull Voronoi cell.

%    K = convhulln(X);
    
  
%    d = [1:size(K,2), 1];
    % d = [1 2 3 1];       % Index into K
    % For each face i;
%    for i = 1:size(K,1)
%        j = K(i,d);
        % Plot a polygon that is white with black lines, on each face i
%        h(i)=patch(X(j,1),X(j,2),'w');
        
        % Uncomment this for transparent polyhedra.
        % set(h(i),'facecolor', 'none');
%    end
%end

%view(3)
%axis equal
%title('The total voronoi tessalation')
%rotate3d;
