I have two sets of points and plot
them in blue stars and red dots. Then I plot
Voronoi diagram of both sets with voronoi(X,Y)
function. I want to specify color of each cell depends on which set it's site is belong. I've almost done this one by the use of patch
function this way:
[v,c]=voronoin(D);
for p=1:TheNumberOfSets
r=rand()/2+0.5; % random gray color
col=[r r r];
for s=1:PointsInSet(p)
l=l+1;
patch(v(c{l},1),v(c{l},2),col); % color
axis([0 10 0 10]);
end
end
WhereD
is the coordinates of the points of sets, TheNumberOfSets
show how many sets do we have (in this particular part we have just 2 sets), col
specify a random gray color, PointsInSet
specify how many points do we have in each set and l
is used to enumerate cells of Voronoi diagram.
now my problem (as you can see!) is about unbounded cells. This code just change the color of the bounded cells and I want to color unbounded cells with their specified set's color in the range of axis box (i.e. the box that you can see in the image).
Any suggestion?
Your example does in fact create
patch
objects for the unbounded cells, but because they contain vertices that haveInf
values representing the unbounded edges, they aren't displayed. You need to replace these vertices with finite ones to complete the patches.The vertices generated by
voronoi
to draw the unbounded cell edges are useful for this purpose. You can obtain these when drawing the voronoi diagram:The unbounded edges are plotted last, so you can isolate these by counting unbounded cells in
c
:The first listed vertex of these edges is the finite vertex of the cell. These don't always exactly match the coordinates returned by
voronoin
due to floating-point error, so identify which numbered vertices these correspond to by finding the minimum pairwise distance frompdist2
:To substitute in these coordinates you can then replace
patch(v(c{l},1),v(c{l},2),col);
with the following: