Question

I am wondering if there is an easy way to change the color of the Voronoi diagram using the patch command. In fact, it seems there must be a relatively easy way because I use the set command to change the limits of the colorbar so I can standardize the colors when I am plotting different Voronoi diagrams.

for i = 1:length(p)
if all(p{i}~=1)   % If at least one of the indices is 1, 
                  % then it is an open region and we can't 
                  % patch that.
patch(v(p{i},1),v(p{i},2),log10(Color(i)))**; % use color i.
set(gca, 'CLim', [0 7]);
colorbar
end
end

The patch command is used to take the output information from the voronoin command and then create the faces for the convex polygons. The important part is the coloring for those patches. As you can see that is the third input argument I pass into the patch command, and is a mx1 vector of values.

My question is, is it possible to use patch to create those polygons with the initial colors mapped onto those polygons, and then be able to use the set command (or something similar) to feed in another mx1 vector of colors to change the colors of all of those polygons at once, using the original mapping of the initial coloring.

The way I do it before is a very round about method and extremely time consuming. Because each color must correspond with its particular polygon, I had to recreate all of the polygons every time I wanted a new color. I am hoping there is a much easier way to change the colors with a new color vector after the polygons have been created.

Let me know if there is anything I can do to make this more clear, thank you!

Was it helpful?

Solution

I am not totally sure whether this is what you are asking, but it might be:

% Is this how you generate your Voronoi diagram?
x = gallery('uniformdata',[1 10],0);
y = gallery('uniformdata',[1 10],1);
dt = DelaunayTri(x(:),y(:));
v = voronoiDiagram(dt);

% Original colors of your Voronoi patches (mx3 matrix)
origColors = rand(numel(p), 3);

% To store handles to the generated patches
pHandle = nan(1, numel(p));

for i = 1:numel(p)
    if all(p{i}~=1)
        pHandle(i) = patch(v(p{i},1), v(p{i},2), colors(i)); 
    end
end
set(gca, 'CLim', [0 7]);

% New colors for the patches (mx3 matrix)
newColors = rand(numel(p), 3);

% Recolor the closed regions
closedIdx = find(~isnan(patchHandle));
arrayfun(@(i) set(pHandle(i), 'FaceColor', colors(i,:)), closedIdx);
set(gca, 'CLim', [0 7]);

OTHER TIPS

x = [3 6 9 1 2 10 2 2 5 8]';
y = [5 10 15 3 5 12 5 8 10 7]';

[v,p] = voronoin([x,y]);

% Original colors of your Voronoi patches (mx3 matrix)
colors = [10 9 NaN NaN 2 NaN NaN 1 NaN NaN]';

% To store handles to the generated patches
pHandle = nan(1, numel(p));

for i = 1:numel(p)
if all(p{i}~=1)
    pHandle(i) = patch(v(p{i},1), v(p{i},2), colors(i)); 
end
end
colorbar 
% New colors for the patches (mx3 matrix)
colors = [1 2 NaN NaN 9 NaN NaN 10 NaN NaN]';

% Recolor the closed regions
closedIdx = find(~isnan(pHandle));
arrayfun(@(i) set(pHandle(i),   'FaceColor','flat','FaceVertexCData',colors(i,:),'CDataMapping','scaled'),closedIdx);

I had to use NaN's for the patches that go off into infinity and are not bounded, so the ones that are bounded, I can color them. Thanks for the help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top