Use matlab to implement a function that randomly generates N disjoint balls in a specified three-dimensional area

Since writing this blog, more people have asked me this question.

Simply write a matlab function that randomly generates balls in a three-dimensional area_Wind-like Hangge's blog-CSDN blog_comsol randomly generates spheres

The function I briefly wrote last time can achieve some relatively sparse distributions. When the distribution is relatively dense, there will be a problem of too long calculation time. In response to this situation, this article has been updated. Thanks to the fans for their discussions and exchanges, so that the implementation can be continuously optimized.

Implementation ideas: 1. Random generation requires the use of random number generation functions. There are several types of random number generation functions in matlab. Here, the rand and randi functions are used. The difference is that one implements a double type and the other generates an int type value.

2. The position is random. Of course, the coordinates of the center of the sphere are random numbers, and the random numbers are within a given interval.

3. Disjoint. Since we want to disjoin, the distance between any two balls must be greater than the diameter of the ball. Therefore, every time a point is generated, we must determine whether all distances meet the conditions. If so, add them to the coordinates of the center of the ball. In the array, if it is not satisfied, it will be iterated next.

4. How to draw a sphere? I use the surf function to  create a three-dimensional surface diagram. It is a three-dimensional surface with solid-color edges and solid-color surfaces. This function   plots the values ​​in the matrix as   heights above the grid in the xy plane defined by  and  . The color of the surface   changes based on the specified height.surf(X,Y,Z)ZXYZ

The following is the specific code part:

function  PlotNsphere_in_cubeRect(X1,Y1,Z1,X2,Y2,Z2,N,R)
%plotNcircle_in_rect(X1,X2,Z1,Y1,Y2,Z2,N,R) 函数说明:生成随机N个互不相交的球体
%  参数说明:三维区域为(X1,Y1,Z1)->(X2,Y2,Z2)
%  N:表示N个球体,R表示球体半径
%  输出是一张plot绘制的图,
close all
MAXIRTER = 10000; % 最大迭代次数
i=1;
s=[];%矩阵s,第一行存储x,第二行存储y,第三行存储z
c=1; %控制循环次数
rng('shuffle')          %%每次随机种子不一样,防止每次结果都是一样的
while c<MAXIRTER
    %% 如果采用以下三行代码,那么生成的随机数为整数
    tempx=randi([int32(X1),int32(X2)],1);
    tempy=randi([int32(Y1),int32(Y2)],1);
    tempz=randi([int32(Z1),int32(Z2)],1);
    %% 如果采用以下三行代码,那么生成的随机数为double类型,也即是默认的取值
%     tempx=X1+(X2-X1)*rand();
%     tempy=Y1+(Y2-Y1)*rand();
%     tempz=Z1+(Z2-Z1)*rand()
    %如果矩阵s为空矩阵,则存储第一个随机数值
    if(isempty(s)==1)
        s(1,i)=tempx;
        s(2,i)=tempy;
        s(3,i)=tempz;
        i=i+1;
        c=c+1;
    end
    %如果矩阵s不为空矩阵,求出各个坐标之间的距离矩阵disl,将与所有点之间距离都大于10的坐标存储
    if(isempty(s)==0)
        for j=1:i-1
            disl(1,j)=sqrt(((tempx-s(1,j))^2)+((tempy-s(2,j))^2)+((tempz-s(3,j))^2));
            j=j+1;
        end
        if(isempty(find(disl<2*R,1))==1)  % 或size(find(l<n),1)==0 表示矩阵l中小于n的个数等于0
            s(1,i)=tempx;
            s(2,i)=tempy;
            s(3,i)=tempz;
            i=i+1; 
        end
        c=c+1;
    end
    if(i>=N+1)
        break;
    end
end
X=s(1,:);
Y=s(2,:);
Z=s(3,:);

%% 绘制球体
    figure
for i=1:1:N
    myplotsphere(X(i),Y(i),Z(i),R);
    hold on 
    axis equal
    grid on
end
end

%% 绘制球体的子函数
function myplotsphere(X,Y,Z,R)
% myplotsphere使用三点和半径进行绘制一个球体
%   x,y,z;三维坐标;R:半径
[x,y,z]=sphere;
surf(R*x+X,R*y+Y,R*z+Z)
% mesh(R*x+X,R*y+Y,R*z+Z)
hold on
% axis equal
end

There are comments in the specific function implementation, which should be understandable.

Example:

After running the command PlotNsphere_in_cubeRect(0,0,0,50+3.77,50+3.77,20+2,680,1.65), wait for a while and a picture will be output:

When running PlotNsphere_in_cubeRect(0,0,0,50+3,50+3,20+2,1000,1.2), that is, to generate 1000 spheres, the result will be:

 

 There are a lot of spheres mentioned above, so let’s just take a look. The maximum test size is 1000. There is no test for any larger size.

Finally, thank you for your attention. If you have any questions, please send a private message or leave a message.

 

Guess you like

Origin blog.csdn.net/weixin_41579872/article/details/124066502#comments_28398196