Colored unlimited raven chart cells in MATLAB

I have two sets of dots and plotthem in blue stars and red dots. Then I am plotthe Voronoi diagram of both sets with function voronoi(X,Y). I want to indicate the color of each cell, depending on which element it belongs to. I almost did it using the function patchas follows:

     [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

Where Dare the coordinates of the points of the sets, TheNumberOfSetsshows how many sets we have (in this part we have only 2 sets), colindicate a random gray color, PointsInSetindicate how many points we have in each set, and is lused to list the cells of the Voronoi diagram.

and this is the result: enter image description here

( !) . , ( , ).

?

+4
2

patch , Inf, , . .

, voronoi , . voronoi:

h = voronoi(D);
v1 = shiftdim(reshape([h(2).XData;h(2).YData],2,3,[]),2); % Arranged one edge per row, one vertex per slice in the third dimension

, , c:

nUnbounded = sum(cellfun(@(ic)ismember(1,ic),c));
v1Unbounded = v1(end-(nUnbounded-1):end,:,:);

. , voronoin - , , , pdist2:

[~,iBounded] = min(pdist2(v,v1Unbounded(:,:,1))); % Index of the bounded vertex
vUnbounded = v1Unbounded(:,:,2); % Displayed coordinate of the unbounded end of the cell edge

, patch(v(c{l},1),v(c{l},2),col); :

cPatch = c{l}; % List of vertex indices
vPatch = v(cPatch,:); % Vertex coordinates which may contain Inf
idx = find(cPatch==1); % Check if cell has unbounded edges
if idx
    cPatch = circshift(cPatch,-idx); % Move the 1 to the end of the list of vertex indices
    vPatch = [vPatch(1:idx-1,:)
              vUnbounded(iBounded == cPatch(end-1),:)
              vUnbounded(iBounded == cPatch(1),:)
              vPatch(idx+1:end,:)]; % Replace Inf values at idx with coordinates from the unbounded edges that meet the two adjacent finite vertices
end
patch(vPatch(:,1),vPatch(:,2),col);
+1

, Matlab R2018a (R2014b , R2015 ~ R2016), voronoi h, , . R2018a

 h(1)

ans = 

 handle to deleted Data

R2014b

h(1)

ans = 

  Line with properties:

              Color: [0 0.4470 0.7410]
          LineStyle: 'none'
          LineWidth: 0.5000
             Marker: '.'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [1x200 double]
              YData: [1x200 double]
              ZData: [1x0 double]

  Show all properties

, - .

+1

Source: https://habr.com/ru/post/1662632/


All Articles