Clear Filters
Clear Filters

Hw can I find out vertices of all the polygons/cells in a voronoi diagram including unbounded cells?

9 views (last 30 days)
How can I find out vertices of all the polygons/cells in a voronoi diagram including unbounded cells? [V,C] =voronoin() functions gives vertices of only bounded cells but I need vertices of all the cells whether it is bounded or unbounded. How do I get it?

Answers (1)

Naga
Naga on 17 Sep 2024 at 6:50
Hello Nidhi,
I understand you need the vertices of all the polygons in a voronoi diagram. The 'voronoin' function provides the vertices of the Voronoi diagram and the indices of these vertices for each cell. While it might seem like it only gives you bounded cells, it actually provides the vertices for all cells, including unbounded ones.
The key is in how you interpret the output. 'V' is a matrix where each row represents a vertex in the Voronoi diagram. 'C' is a cell array where each cell contains a vector of indices into V, representing the vertices of the corresponding Voronoi cell. Unbounded cells will have vertices that include Inf in their coordinates. These are represented as indices in 'C' that point to the first row of 'V', which usually contains Inf values.
Here's how you can extract the vertices for all Voronoi cells, including unbounded ones:
% Sample points
points = rand(10, 2);
% Compute the Voronoi diagram
[V, C] = voronoin(points);
% Plot the Voronoi diagram
figure; hold on;
plot(points(:,1), points(:,2), 'ro'); % Plot the points
% Iterate over each cell
for i = 1:length(C)
% Extract the vertices for the i-th Voronoi cell
cellVertices = V(C{i}, :);
% Plot bounded cells
if all(~isinf(cellVertices), 'all')
fill(cellVertices(:,1), cellVertices(:,2), 'cyan', 'FaceAlpha', 0.3);
else
% Plot lines for unbounded cells
finiteVertices = cellVertices(~isinf(cellVertices(:,1)), :);
plot(finiteVertices(:,1), finiteVertices(:,2), 'b-');
end
end
title('Voronoi Diagram with Bounded and Unbounded Cells');
axis equal; hold off;
This approach allows you to handle and visualize both bounded and unbounded Voronoi cells effectively

Categories

Find more on Voronoi Diagram in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!