Put area of Voronoi polygon to exact location

2 次查看(过去 30 天)
Hello
How to match area of each voronoi polygon with location of points used to create that polygon.
I calculate area of each polygon and when I plot it is not OK. For example (see attached picture) polygon around point T9 has area 5.389, but on the picture it is 5.2436.
X = [-1.5 3.2; 1.8 3.3; -3.7 1.5; -1.5 1.3; 0.8 1.2; ...
3.3 1.5; -4.0 -1.0; -2.3 -0.7; 0 -0.5; 2.0 -1.5; ...
3.7 -0.8; -3.5 -2.9; -0.9 -3.9; 2.0 -3.5; 3.5 -2.25];
[VX,VY] = voronoi(X(:,1),X(:,2));
h = plot(VX,VY,'-b',X(:,1),X(:,2),'.r');
xlim([-4,4])
ylim([-4,4])
[V,C,XY]=VoronoiLimit(X(:,1),X(:,2));
% Compute area of each voronoi polygon
A = zeros(length(C),1) ;
for i = 1:length(C)
v1 = V(C{i},1) ;
v2 = V(C{i},2) ;
patch(v1,v2,rand(1,3));
A(i) = polyarea(v1,v2) ;
end
x=X(:,1);
y=X(:,2);
nump = size(x,1);
plabels = arrayfun(@(n) {sprintf('T%d', n)}, (1:nump)');
hold on
plot(x,y,'.')
text(x, y, plabels, ...
'FontWeight', 'bold', 'HorizontalAlignment',...
'right','BackgroundColor', 'none');
hold off
hold on
text(x,y,num2str(A),'fontsize',8,'HorizontalAlignment',...
'left');
hold off
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 Giometar,
I made the following modifications to your code:
  1. Removed the call to 'VoronoiLimit' and instead used MATLAB's built-in 'voronoiDiagram' function directly with delaunayTriangulation. This is a direct approach to get the Voronoi vertices V and cells C.
  2. Simplified the plotting and labeling by using a single loop to plot both the point labels (T1, T2, ..., Tn) and their corresponding area values. This reduces redundancy and makes the code more concise.
  3. Consolidated the plotting commands with a single hold on and hold off pair, ensuring that all plotting operations happen within the same context and reducing potential errors.
X = [-1.5 3.2; 1.8 3.3; -3.7 1.5; -1.5 1.3; 0.8 1.2; ...
3.3 1.5; -4.0 -1.0; -2.3 -0.7; 0 -0.5; 2.0 -1.5; ...
3.7 -0.8; -3.5 -2.9; -0.9 -3.9; 2.0 -3.5; 3.5 -2.25];
[VX,VY] = voronoi(X(:,1),X(:,2));
h = plot(VX,VY,'-b',X(:,1),X(:,2),'.r');
xlim([-4,4])
ylim([-4,4])
[V,C] = voronoiDiagram(delaunayTriangulation(X));
% Compute area of each Voronoi polygon
A = zeros(length(C),1);
for i = 1:length(C)
v1 = V(C{i},1);
v2 = V(C{i},2);
patch(v1,v2,rand(1,3));
A(i) = polyarea(v1,v2);
end
% Plot points and labels
hold on
plot(X(:,1), X(:,2), '.r');
for i = 1:length(X)
text(X(i,1), X(i,2), sprintf('T%d', i), 'FontWeight', 'bold', 'HorizontalAlignment', 'right');
text(X(i,1), X(i,2), sprintf('%.4f', A(i)), 'FontSize', 8, 'HorizontalAlignment', 'left');
end
hold off
Please use the documentation links below to know more about the functions 'voronoiDiagram' and 'delaunayTriangulation':
  1. voronoiDiagram: https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.voronoidiagram.html
  2. delaunayTriangulation:https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.html

类别

Help CenterFile Exchange 中查找有关 Voronoi Diagram 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by