color a voronoi diagram in matlab according to the color values of the initial points

Use voronoiDiagram to get the polygons.

dt = delaunayTriangulation(start_coord(:,1:2));
[V,R] = voronoiDiagram(dt);

Then R{i} will be the vertices of polygon from start_coord(i,:) So set the color to start_coord(i,3)’s color and:

A=V(R{i},:);
B=A(any(~isinf(A),2),:); % omit points at infinity
plot(polyshape(B));

The only hiccup there is that the vertices at infinity get chopped off. But maybe that will get you close enough to what you want. If you need to fill to the edge, check out the VoronoiLimit function (which I have not tested).

e.g.:

X = [-1.5 3.2; 1.8 3.3; -3.7 1.5; -1.5 1.3; ...
0.8 1.2; 3.3 1.5; -4.0 -1.0;-2.3 -0.7; ...
0 -0.5; 2.0 -1.5; 3.7 -0.8; -3.5 -2.9; ...
-0.9 -3.9; 2.0 -3.5; 3.5 -2.25];

X(:,3) = [ 1 2 1 3 1 2 2 2 2 3 3 3 3 3 3]';

ccode = ["red","green","blue"];

dt = delaunayTriangulation(X(:,1:2));
[V,R] = voronoiDiagram(dt);
figure
voronoi(X(:,1),X(:,2))
hold on
for i = 1:size(X,1)
    A=V(R{i},:);
    B=A(any(~isinf(A),2),:);
    if(size(B,1)>2)
        plot(polyshape(B),'FaceColor',ccode(X(i,3)));
    end
end

Result: enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top