How can I calculate voronoi area?

6 次查看(过去 30 天)
Muhammet Ariturk
Muhammet Ariturk 2017-3-9
编辑: Naga 2024-9-16
Hi everyone, this is my solution for voronoi, but I have still problem calculating the areas of voronoi.
X = [621974 4255449 80; 620001 4256649 100; 620519 4254081 87; 621289 4255502 100; ... 621375 4256385 90; 614408 4256955 60; 621000 4255950 80; 618936 4249014 50; ... 614408 4256955 55; 619490 4252004 100; 614902 4255707 75]; [VX,VY] = voronoi(X(:,1),X(:,2)); h = plot(VX,VY,'-b',X(:,1),X(:,2),'.r'); %xlim([612000,622000]) %ylim([4250000,4257000]) %zlim([80,120]) % Assign labels to the points X. nump = size(X,1); plabels = arrayfun(@(n) {sprintf('X%d', n)}, (1:nump)'); hold on Hpl = text(X(:,1), X(:,2)+0.2, plabels, 'color', 'r', ... 'FontWeight', 'bold', 'HorizontalAlignment',... 'center', 'BackgroundColor', 'none');
% Compute the Voronoi diagram. dt = delaunayTriangulation(X); [V,R] = voronoiDiagram(dt);
% Assign labels to the points X. nump = size(X,1); plabels = arrayfun(@(n) {sprintf('X%d', n)}, (1:nump)'); hold on Hpl = text(X(:,1), X(:,2)+0.2, plabels, 'color', 'r', ... 'FontWeight', 'bold', 'HorizontalAlignment',... 'center', 'BackgroundColor', 'none');
% Compute the Voronoi diagram. dt = delaunayTriangulation(X); [V,R] = voronoiDiagram(dt);
% Assign labels to the Voronoi vertices V. % By convention the first vertex is at infinity. numv = size(V,1); vlabels = arrayfun(@(n) {sprintf('V%d', n)}, (2:numv)'); hold on Hpl = text(V(2:end,1), V(2:end,2)+.2, vlabels, ... 'FontWeight', 'bold', 'HorizontalAlignment',... 'center', 'BackgroundColor', 'none'); hold off

回答(1 个)

Naga
Naga 2024-9-16
编辑:Naga 2024-9-16
Hello Muhammet,
To calculate the areas of the Voronoi cells, you can use the vertices of each cell and apply a polygon area calculation method. Since you already have the Voronoi vertices (V) and the regions (R) from the 'voronoiDiagram' function, you can compute the area of each cell by iterating over the regions and using the vertices to calculate the area of the polygons. Please refer the code below:
% Compute the Voronoi diagram
dt = delaunayTriangulation(X(:, 1), X(:, 2));
[V, R] = voronoiDiagram(dt);
% Initialize an array for areas
areas = zeros(length(R), 1);
% Calculate the area of each Voronoi cell
for i = 1:length(R)
regionIndices = R{i};
if all(regionIndices ~= 1) % Check if the region is finite
vertices = V(regionIndices, :);
areas(i) = polyarea(vertices(:, 1), vertices(:, 2));
else
areas(i) = NaN; % Assign NaN for unbounded regions
end
end
% Display the areas
disp('Areas of the Voronoi cells:');
disp(areas);
  1. Use 'delaunayTriangulation' and 'voronoiDiagram' to obtain Voronoi vertices and regions.
  2. Check each region to ensure it is bounded (i.e., does not include the point at infinity).
  3. Compute the area of each bounded region using 'polyarea' function, which applies the shoelace formula.
  4. If a region is unbounded, you can assign a placeholder value like NaN.
This approach assumes that all relevant Voronoi cells are bounded. If you have unbounded regions that you want to handle differently, you will need to adjust the logic accordingly.
Refer to the below documentation page for more information on 'polyarea' function:
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