Questions about the Detailed Settings of Voronoi Diagrams ボロノイ図の細かな設定について質問です

5 Ansichten (letzte 30 Tage)
irosy
irosy am 11 Sep. 2024
Beantwortet: Ronit am 16 Sep. 2024
I have been reading an example from a book and am trying to recreate the diagram using Voronoi diagrams. However, I want to consider a bounded region [0,1], [1,0], but when using the voronoi function, the lines extend beyond the boundaries. I would like to obtain information such as the area and centroid of the bounded regions, so I want to create the Voronoi diagram with these bounded constraints in mind. Is this possible?
ある書籍の例を読み、ボロノイ図を用いてその図を再現したいと考えています。
しかし有界の領域[0,1],[1,0]について考えたいところ、voronoiでは枠外を超えて線が伸びてしまいます。
有界領域の面積や重心などの情報を得たいため、ボロノイ図を有界の制約を踏まえて作成したいのですが可能でしょうか?
clear
close all
figure()
x = [0.2 0.6 0.9 0.7 0.8 0.5 0.4 0.1 0.3 0.5];
y = [0.7 0.9 0.8 0.6 0.3 0.4 0.2 0.1 0.3 0.5];
X = [x',y'];
vrn_obj = voronoi(X(:,1),X(:,2));
[vx,vy] = voronoi(X(:,1),X(:,2));
edge_set =[vx',vy'];
nump = size(X,1);
plabels = arrayfun(@(n) {sprintf('X%d', n)}, (1:nump)');
hold on
Hpl = text(X(:,1), X(:,2), plabels, 'FontWeight', ...
'bold', 'HorizontalAlignment','center', ...
'BackgroundColor', 'none');
dt = delaunayTriangulation(X);
hold on
triplot(dt,'--');
hold off
axis equal
% xlim([0 1])
% ylim([0 1])
grid on
  1 Kommentar
Sam Chak
Sam Chak am 11 Sep. 2024
Bearbeitet: Sam Chak am 11 Sep. 2024
Have you tried using these commands?
A = polyarea(x, y)
A = area()
[x, y] = centroid()

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ronit
Ronit am 16 Sep. 2024
Hello Irosy,
To create a Voronoi diagram with bounded constraints (within the region [0,1] x [0,1]), you can clip the Voronoi cells at the boundaries. This can be achieved by using a combination of functions from MATLAB's Computational Geometry Toolbox. The following steps can be used:
  1. Delaunay Triangulation: The delaunayTriangulation function is used to create a triangulation from the set of points X.
  2. Voronoi Diagram: The voronoiDiagram function generates the Voronoi vertices v and cells c.
  3. Bounding Box: A polyshape object is created to represent the bounding box, which is used to clip the Voronoi cells.
  4. Intersection: The intersect function is used to clip each Voronoi cell with the bounding box, resulting in a bounded cell.
  5. Area Calculation: The area method of the polyshape object calculates the area of the bounded Voronoi cell.
  6. Centroid Calculation: The centroid method provides the x and y coordinates of the centroid of the bounded Voronoi cell.
  7. Plotting: Each bounded Voronoi cell and its centroid are plotted, with the centroid marked by a red cross.
clear
close all
figure()
x = [0.2 0.6 0.9 0.7 0.8 0.5 0.4 0.1 0.3 0.5];
y = [0.7 0.9 0.8 0.6 0.3 0.4 0.2 0.1 0.3 0.5];
X = [x', y'];
dt = delaunayTriangulation(X);
[v, c] = voronoiDiagram(dt);
% Define the bounding box as a polyshape
boundingBox = polyshape([0 1 1 0], [0 0 1 1]);
% Plot the Voronoi diagram with bounded constraints
hold on
for i = 1:length(c)
if all(c{i} ~= 1)
% Get the coordinates of the Voronoi cell
poly = v(c{i}, :);
voronoiCell = polyshape(poly);
boundedCell = intersect(voronoiCell, boundingBox);
plot(boundedCell, 'FaceColor', rand(1,3), 'FaceAlpha', 0.5, 'EdgeColor', 'k');
% Calculate the area of the bounded Voronoi cell
A = area(boundedCell);
disp(['Area of cell ', num2str(i), ': ', num2str(A)]);
% Calculate the centroid of the bounded Voronoi cell
[cx, cy] = centroid(boundedCell);
disp(['Centroid of cell ', num2str(i), ': (', num2str(cx), ', ', num2str(cy), ')']);
% Plot the centroid
plot(cx, cy, 'rx', 'MarkerSize', 8, 'LineWidth', 2);
end
end
Area of cell 4: 0.085536
Centroid of cell 4: (0.72839, 0.59819)
Area of cell 6: 0.047202
Centroid of cell 6: (0.52781, 0.36424)
Area of cell 7: 0.092515
Centroid of cell 7: (0.45537, 0.12928)
Area of cell 9: 0.084104
Centroid of cell 9: (0.22274, 0.35244)
Area of cell 10: 0.062357
Centroid of cell 10: (0.46638, 0.55962)
% Plot the points
plot(X(:,1), X(:,2), 'MarkerFaceColor', 'k');
nump = size(X, 1);
plabels = arrayfun(@(n) {sprintf('X%d', n)}, (1:nump)');
Hpl = text(X(:,1), X(:,2), plabels, 'FontWeight', ...
'bold', 'HorizontalAlignment', 'center', ...
'BackgroundColor', 'none');
xlim([0 1])
ylim([0 1])
axis equal
grid on
hold off
Please go through the following MATLAB documentations for better understanding.
I hope it helps with your query!

Kategorien

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

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by