How can I get the region of a voronoi diagram that I selected?

7 Ansichten (letzte 30 Tage)
chen yuqin
chen yuqin am 12 Mär. 2024
Beantwortet: Shivam am 20 Mär. 2024
I generated a voronoi diagram using the code below, and I want the area of all the closed convergence cells and only plot them. Just the cells in the red rectangle. How can I do that? Thanks all!
Rad3Over2 = sqrt(3) / 2;
L = 11;
[X Y] = meshgrid(0:1:L);
n = size(X,1);
X = sqrt(3)*Rad3Over2 *X + 0.8*rand(n)-0.4;
Y = sqrt(3)*(Y + repmat([0 0.5],[n,n/2])) + 0.8*rand(n)-0.4;
X = reshape(X,n^2,1);
Y = reshape(Y,n^2,1);
P = [X Y];
m = size(P,1);
[v c] = voronoin(P) ;
figure
hold on
voronoi(P(:,1),P(:,2))
A = zeros(length(c),1) ;
for i = 1:length(c)
v1 = v(c{i},1) ;
v2 = v(c{i},2) ;
patch(v1,v2,'white')
axis equal, axis([1 10 1 10]), zoom on
A(i) = polyarea(v1,v2) ;
end

Antworten (1)

Shivam
Shivam am 20 Mär. 2024
Hi Chen,
From the details provided, I get that you are generating the Voronoi diagram and want to plot the cells that strictly lie inside the red rectangle marked and store the area of those cells.
To achieve the goal, you can make the following changes to the code:
  1. Remove cells with infinite vertices: To skip cells with infinite vertices before doing any further processing, including plotting.
  2. Apply range check on the cell's vertices: By looking at the range of the rectangle in the figure, the vertices range can be assumed to be [0, 16.5, 0.5, 19.5], which refers to allowable min and max values of X and Y coordinates, respectively.
  3. Avoid the Voronoin function to plot the cells: The Voronoi function plots all the cells, and since you want to plot only specific cells, you can plot those cells using the patch function.
You can follow the modified code below, implementing all the aforementioned steps:
Rad3Over2 = sqrt(3) / 2;
L = 11;
[X, Y] = meshgrid(0:1:L);
n = size(X,1);
X = sqrt(3)*Rad3Over2 * X + 0.8*rand(n)-0.4;
Y = sqrt(3)*(Y + repmat([0 0.5],[n,n/2])) + 0.8*rand(n)-0.4;
X = reshape(X, n^2, 1);
Y = reshape(Y, n^2, 1);
P = [X Y];
[v, c] = voronoin(P);
figure
hold on
A = []; % Array to store areas of valid cells
range = [0, 16.5, 0.5, 19.5]; % [0, 16.5] min & max X coordinate. [0.5, 19.5] min & max Y coordinate
for i = 1:length(c)
v1 = v(c{i},1);
v2 = v(c{i},2);
% Skip cells with infinite vertices
if any(isinf(v1)) || any(isinf(v2))
continue; % Move to the next iteration if the cell has infinite vertices
end
% Check if the cell vertices are within the specified range
if all(v1 <= range(2) & v1 >= range(1) & v2 <= range(4) & v2 >= range(3))
patch(v1, v2, 'green', 'EdgeColor', 'black'); % Fill the cell with green color
A = [A, polyarea(v1, v2)]; % Calculate and store the area of the valid cell
end
end
axis equal;
axis([-2 19 -2 23]);
zoom on;
You can adjust the range as per your future requirement, if needed.
For more information related to the use of the patch function, you can explore the provided documentation:
I believe this addresses your query.
Thanks

Kategorien

Mehr zu Voronoi Diagram finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by