How to find the area of voronoi shapes

11 views (last 30 days)
Sean
Sean on 25 Jun 2014
Answered: Ronit on 8 Nov 2024 at 9:51
clc
clear all
close all
n= 60;
x=720*rand(1,n);
y=360*rand(1,n);
voronoi(x,y)
[vx,vy] =voronoi(x,y);
plot(x,y,'r+',vx,vy,'b-');
axis equal;
With this example of a voronoi diagram, is it possible to find the areas of each respective voronoi shape? If so, how?
  1 Comment
Sean
Sean on 25 Jun 2014
figure(2)
for j=1:length(vx(1,:))
line([vx(1,j) vx(2,j)],[vy(1,j) vy(2,j)])
end
axis equal
Outlines the voronoi diagram.

Sign in to comment.

Answers (1)

Ronit
Ronit on 8 Nov 2024 at 9:51
Hello Sean,
To find the areas of each respective Voronoi cell in a Voronoi diagram, this can be accomplished using the "voronoin" function, which provides the vertices of the Voronoi cells. You can then calculate the area of each cell using these vertices. Please refer to the following code for implementation:
% Create the Voronoi diagram
[v, c] = voronoin([x(:), y(:)]);
% Plot the Voronoi diagram
figure(1);
voronoi(x, y);
hold on;
plot(x, y, 'r+');
axis equal;
% Calculate the area of each Voronoi cell
areas = zeros(1, n);
for i = 1:length(c)
if all(c{i} ~= 1) % If the cell is not infinite
% Get the vertices of the Voronoi cell
vertices = v(c{i}, :);
% Calculate the area using the polyarea function
areas(i) = polyarea(vertices(:, 1), vertices(:, 2));
else
areas(i) = NaN; % Infinite cell, area is undefined
end
end
For more information regarding the MATLAB functions used above, please refer to the following documenation pages:
  1. voronoin: https://www.mathworks.com/help/matlab/ref/voronoin.html
  2. polyarea: https://www.mathworks.com/help/matlab/ref/polyarea.html
I hope it resolves your query!

Categories

Find more on Voronoi Diagram in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!