How to determine the surrounding vertices of a particular node/voronoi cell ?

3 次查看(过去 30 天)
I want to determine the surrounding(corresponding) vertices of all the nodes of the voronoi cells. Please help adding to the program below. x=[2 2 3 3 4 5 5 5 6 7 8]; y=[1 3 1 3 4 4 5 6 5 4 2]; N=[x' y']; axis([0 10 0 10]); hold on; scatter(x,y, [], 'filled'); %Labelling the nodes labels = cellstr( num2str([1:length(x)]') ); plot(N(:,1), N(:,2), 'bx') text(N(:,1), N(:,2), labels, 'VerticalAlignment','bottom', ... 'HorizontalAlignment','right') %Voronoi voronoi(x,y,'green'); [vx,vy]=voronoi(x,y); plot(vx,vy,'rx'); grid on [V C]=voronoin(N); % %Labelling the vertices labels = cellstr( num2str([1:length(V)]') ); plot(V(:,1), V(:,2), 'rx') text(V(:,1), V(:,2), labels, 'VerticalAlignment','bottom', ... 'HorizontalAlignment','right')

回答(1 个)

Naga
Naga 2024-9-16
编辑:Naga 2024-9-16
Hello Aida,
To determine the surrounding vertices of all the nodes of the Voronoi cells, you need to identify which vertices belong to each cell. The 'voronoin' function in MATLAB returns a cell array C where each cell C{i} contains the indices of the vertices in V that form the Voronoi cell corresponding to the i-th point in N.
Here's how you can modify your script to display the surrounding vertices for each node:
x = [2 2 3 3 4 5 5 5 6 7 8];
y = [1 3 1 3 4 4 5 6 5 4 2];
N = [x' y'];
axis([0 10 0 10]);
hold on;
scatter(x, y, [], 'filled');
% Labelling the nodes
labels = cellstr(num2str([1:length(x)]'));
plot(N(:,1), N(:,2), 'bx');
text(N(:,1), N(:,2), labels, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% Voronoi diagram
voronoi(x, y, 'green');
[vx, vy] = voronoi(x, y);
plot(vx, vy, 'rx');
grid on;
% Voronoi vertices and cells
[V, C] = voronoin(N);
% Labelling the vertices
vertex_labels = cellstr(num2str((1:length(V))'));
plot(V(:,1), V(:,2), 'rx');
text(V(:,1), V(:,2), vertex_labels, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% Display surrounding vertices for each node
for i = 1:length(C)
vertices = C{i};
if all(vertices ~= 1) % Ignore the vertex at infinity
node_str = sprintf('Node %d: ', i);
vertices_str = sprintf('%d ', vertices);
disp([node_str, 'Vertices: ', vertices_str]);
else
disp(['Node ', num2str(i), ': Contains infinite vertex']);
end
end
  1. C{i} contains indices of vertices in V for the Voronoi cell of the i-th node.
  2. Sometimes Voronoi cells extend to infinity, which is represented by the first vertex (index 1). The code checks for this condition using 'if all(vertices ~= 1)'.
  3. This script will print the indices of the vertices for each Voronoi cell, excluding those that include the vertex at infinity. Adjust the display as needed for your specific requirements.
Hope this helps!

类别

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