How to create an offset to the voronoi cells in Voronoi diagram?

4 次查看(过去 30 天)
A voronoi diagram has been constructed using the following codes from the mathworks documentation:
x = gallery('uniformdata',[1 10],0);
y = gallery('uniformdata',[1 10],1);
voronoi(x,y)
In this voronoi diagram, how to create an offset to the voronoi cells like the attached image file?
Please find the attachment.

回答(1 个)

Naga
Naga 2024-9-16
编辑:Naga 2024-9-16
Hello Ruban,
To create an offset to the Voronoi cells like in the attached image, you can use the 'polybuffer' function in MATLAB to create a buffer around each Voronoi cell. Here’s an example of how you can achieve this:
% Generate random points
x = gallery('uniformdata',[1 10],0);
y = gallery('uniformdata',[1 10],1);
% Create Voronoi diagram
[vx, vy] = voronoi(x, y);
plot(vx, vy, '-r', x, y, '.k');
hold on;
% Compute Voronoi vertices and cells
dt = delaunayTriangulation(x', y');
[V, C] = voronoiDiagram(dt);
% Plot original Voronoi cells
for i = 1:length(C)
if all(C{i}~=1) % Skip the first vertex which is at infinity
fill(V(C{i},1), V(C{i},2), 'r', 'FaceAlpha', 0.3);
end
end
% Create offset Voronoi cells
offset = 0.05; % Adjust this value for the desired offset
for i = 1:length(C)
if all(C{i}~=1) % Skip the first vertex which is at infinity
poly = polyshape(V(C{i},1), V(C{i},2));
offsetPoly = polybuffer(poly, -offset);
plot(offsetPoly, 'EdgeColor', 'b', 'FaceAlpha', 0.3);
end
end
hold off;
This code will create an offset around each Voronoi cell, similar to the blue lines in your attached image. Adjust the offset value to control the distance of the offset.
Refere to the below documentation link for more information on 'polybffer' function:

类别

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