问题
I am trying to update the Voronoi's intersection point array by find the intersection of overhanging polygon lines that intersect a defined square's perimeter. I want to be able to recreate a new Voronoi intersection point array that should replace those overhanging points with the intersected ones.
Below I have some code that I created for experiments.
function grainnum = sentuarusgrain(L,H,grainsize,numsamples,plot_grain)
for r=1:numsamples
n = randi([10,50],1,1);%chooses random number between 0 to 100. n is our randomizer factor
random = grainsize*n;
x = gallery('uniformdata',[1,random],1); %set of points for x that depends on grainsize*n
y = gallery('uniformdata',[1,random],0);
x = x*L;
y = y*H;
[v,c] = voronoin([x(:) y(:)]); %returns an array V with vertices and a cell array C with a matrix for each cell of the diagram.
for k = 1 : numel(c) %in the cell c , every 1 corresponds to a inf vector
c{k} = c{k}(c{k} ~= 1);%gets rid of 1 in cell array C
end
for i=1:random
% First we make a new matrix that has each of the required elements for the desired format
% The index of x, the value of x, the index of y and the value of y
TEMParea = polyarea(coord(:, 1),coord(:, 2));
TOTALarea = TOTALarea + TEMParea;
tempCoord = [coord(:, 1).'; coord(:, 2).'];
coord
end
%VSgraph(:, 1) = random;
random
AVERAGEarea = TOTALarea/random
%VSgraph(:,2) = AVERAGEarea;
VSgraph(:,r) = random;
VSgraph(:,r+1) = AVERAGEarea;
VSgraph
if plot_grain == 1
rectangle('Position',[0,0,L,H]);% makes a section with LxH dimensions. Variables in the function parameters
hold on
xlim([0 L])
ylim([0 H])
a = voronoi(x,y);
%plots the whole voronoi diagram
figure;a;
%labels the points in voronoi
% Hpl = text(x,y, plabels, 'FontWeight', ...
% 'light', 'HorizontalAlignment','center', ...
% 'BackgroundColor', 'none');
axis equal
hold off
end
end
end
For example,
Some shapes in the Voronoi diagram have points that exceed a certain point and are left open. Say our square is 1x1. Essentially, we want those open coordinates and the intersection points to close that polygon. Instead of this array: (specifically shape 37)
coord =
0.1448 0.7194
0.1729 0.7858
Notice the overhanging line that leaves outside the square. I want the coord array to be updated with these points.
The highlighted region is specifically shape 37 The new coord array should look something like this:
newcoord =
0.1448 0.7194
0.1729 0.7858
%intersecting points
The code should do these for all of the overhanging shapes. What my intention with this shape is that I want a square, and only in that square is the Voronoi diagram.
To run the function type:
sentuarusgrain(1,1,1,1) for L,H being the length and the height of the square. The grainsize, how many samples, and if you want to plot a graph or not (1 being yes, 0 being no).
This code is different from another request. The other code is limited to a 1x1 square. But this function has parameters (L,H) that should not be limited to only 1 and 1.
回答1:
There is one shortcut you can take to avoid writing additional geometric intersection code. If you reflect the Voronoi sites across the domain boundaries (the four sides of the square) the resulting Voronoi diagram will contain the desired square boundary (since it is exactly half way between the original and artificial sites. Then you can just ignore the Voronoi cells for all the artificial sites created outside the the domain.
Here is an example of a Voronoi diagram with six sites and an extended Voronoi diagram created after reflecting each of the Voronoi sites across each of the four sides of the domain boundary.
This is a little expensive: the extended Voronoi diagram has five times as many sites as the original Voronoi diagram. To be a little more efficient, you can first create the Voronoi diagram with the original sites, determine which Voronoi cells touch the boundary of the domain and then only reflect those sites. But the simplest code is to just reflect all the points and compute a single Voronoi diagram.
来源:https://stackoverflow.com/questions/57471445/how-to-find-intersection-points-of-overhanging-lines-of-voronoi-diagram-intersec