Colouring patches from a voronoi tessalation

조회 수: 9 (최근 30일)
mbvoyager
mbvoyager 2022년 11월 18일
댓글: Bjorn Gustavsson 2022년 11월 19일
I want to color patches generated from a Voronoi tessellation. However, some of the elements stay white. You can see the midpoints and the elements in the first figure below. Some elements (patches) remain white.
In the second figure you can see the assumable reason for this, the elements corresponding to the outer points are not closed and therefore do not form a valid patch.
Do you guys have a solution for how I can color all patches? Maybe by closing the outer ones artificially?
Here is my MWE:
%MWE Voronoi tesselation cell colouring
sample_set = randn(100,2);
figure
[vMGFPoints, vMGFcells] = voronoin(sample_set);
[vMGFx, vMGFy] = voronoi(sample_set(:,1), sample_set(:,2));
hold on
color = randn(100,1);
for vcell = 1:length(sample_set)
patch(vMGFPoints(vMGFcells{vcell},2),vMGFPoints(vMGFcells{vcell},1), color(vcell))
plot(sample_set(vcell,2),sample_set(vcell,1),'k.')
end
plot(vMGFy,vMGFx, 'k')
colorbar_obj = colorbar;
colorbar_obj.Label.String = 'Weighting';
xlim([-3 3])
ylim([-3 3])
Generating this image:
Zooming out shows this
  댓글 수: 3
Star Strider
Star Strider 2022년 11월 18일
It would be nice to have the missing data. I can’t run the code as posted.
mbvoyager
mbvoyager 2022년 11월 18일
Sorry, I failed to replace one variable, now it should work. Thanks for pointing out the issue!

댓글을 달려면 로그인하십시오.

채택된 답변

mbvoyager
mbvoyager 2022년 11월 19일
편집: mbvoyager 2022년 11월 19일
Inspired by Star striders boundary approach, I searched for these terms and found this idea (stackoverflow-python-voronoi). In the second answer in StackOverflow the idea is to artificially bound my 2D space by simply adding far away points in the "corners of the 2D space". These artificially added points will likely change the Voronoi cells and the tessellation but for my purpose this might still be alright.
%MWE Voronoi tesselation cell colouring
sample_set = randn(100,2);
bound_point = 999; %adding 4 artificial boundary points
sample_set = [sample_set; [-bound_point, -bound_point]; [-bound_point, bound_point]; [bound_point, -bound_point]; [bound_point, bound_point]];
figure
[vMGFPoints, vMGFcells] = voronoin(sample_set);
[vMGFx, vMGFy] = voronoi(sample_set(:,1), sample_set(:,2));
hold on
color = randn(length(sample_set),1);
for vcell = 1:length(sample_set)
patch(vMGFPoints(vMGFcells{vcell},2),vMGFPoints(vMGFcells{vcell},1), color(vcell))
plot(sample_set(vcell,2),sample_set(vcell,1),'k.')
end
plot(vMGFy,vMGFx, 'k')
colorbar_obj = colorbar;
colorbar_obj.Label.String = 'Weighting';
xlim([-3 3])
ylim([-3 3])
  댓글 수: 1
Bjorn Gustavsson
Bjorn Gustavsson 2022년 11월 19일
You better test how this affect your results. One suggestion is to put your "dummy-points" further and further away and see if and how the effect varies. It should have a very small (I think even no) effect inside the outermost patches.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
Star Strider 2022년 11월 18일
편집: Star Strider 2022년 11월 18일
It may be possible to close the exterior of the plot using the boundary function. I was able to do that reasonably well with:
v1 = vMGFx(:); % Column Vector For 'boundary' Call
v2 = vMGFy(:); % Column Vector For 'boundary' Call
bidx = boundary(v1,v2, 0.95);
vxf = ismember(vMGFx, v1(bidx));
[r,cx] = find(vxf);
vyf = ismember(vMGFy, v2(bidx));
[r,cy] = find(vyf);
Vx = vMGFx(:,cx) % Boundary Points
Vy = vMGFx(:,cy) % Boundary Points
However, I do not understand your code well enough to incorporate these points into it. Also, since the data are random, it may be necessary to adjust the ‘shrink factor’ in the boundary call each time. (That would not be necessary with fixed values.)
I am not not certain how to use these results with the patch function to close the patch areas. It may require a separate patch call to use them. I will help as I can to work with you to solve this.
.
  댓글 수: 1
mbvoyager
mbvoyager 2022년 11월 19일
Thanks for your help and your time, really appreciated!
Honestly, I do not really understand your approach (most likely because I've never worked with boundary), so the pounts Vx and Vy are suppose to be the boundary points for the outer elements generated by the MATLAB voronoi function?Is the second line here correct, shouldn't vMGFy be accessed in the second line?
Vx = vMGFx(:,cx) % Boundary Points
Vy = vMGFx(:,cy) % Boundary Points
If I edit this line to include
Vy = vMGFy(:,cy) % Boundary Points
and run the following,
plot(vMGFy,vMGFx, 'k') % Plot original points from voroin
hold on
scatter(Vx, Vy, '+r')
I receive this:
These additional points could help but some of them are not really useful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Voronoi Diagram에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by