Matlab - 表面的周期性平铺

3
我有一个表现为附着生物分布的表面(随机生成),但是一些点会在期望区域之外生成。我希望任何在表面之外生成的点能够出现在另一侧。
这样,如果你将这个表面想象成一个“瓷砖”,当无限个瓷砖铺设时,就不会出现跳跃或间隙,它是周期性的。
clear

n_gaussians=20;         % number of barnacles
gaussians=0;            
sigma=1;                % std deviation
mindist=0.8*sigma;      % if distance is smaller than this gaussians "collide" and are not plotted
height=0.3;             % height of the barnacle

[x,y]= meshgrid(-5:0.05:5,-5:0.05:5);
used=[];
Z=zeros(size(x));
while gaussians<n_gaussians

    xm=(rand(1)-0.5)*10;
    ym=(rand(1)-0.5)*10;
    notvalid=0;

        for ii=1:size(used,2)
        % if we are too close to any point.
       if norm([xm-used(1,ii),ym-used(2,ii)])<mindist
           notvalid=1; % do not add this gauusian
       end
    end
    if notvalid
        continue
    end
    used(:,end+1)=[xm;ym];
    Zaux = height/sigma*exp(-5*((x-xm).^2+(y-ym).^2)/sigma.^2)-0.1;
    Zaux(Zaux<0)=0;
    Z=Z+Zaux;
    gaussians=gaussians+1;
end

surf(x,y,Z);
axis equal
shading interp

这张图片希望能够解释所需内容:

enter image description here

我还需要保留间距,因此,如果xm,ym在绘图的另一侧的高斯分布距离范围内,它们需要被折扣。我已经尝试过让它工作,但似乎找不到方法。任何帮助将不胜感激!

2
尝试在一个周期性空间上运行相同的代码,该空间比您的空间大9倍(3x3)。然后,不要为给定的高斯放置1个附着物,而是周期性地放置9个,并在所有地方进行错误检查。然后将中央正方形作为输出。(不知道是否清楚) - BillBokeey
1个回答

5

这个想法是通过使用mod将轴绕着边缘值包装。在得到您的默许后,我有权对您的代码进行修改:

n_gauss = 20;
k_gauss = 0;            
sigma   = 1;
mindist = 0.8*sigma;
height  = 0.3;
L       = 5;
dL      = 0.05;

[x,y] = meshgrid(-L:dL:L, -L:dL:L);
used  = NaN(2,n_gauss);
Z     = zeros(size(x));
while k_gauss < n_gauss
        xm = (rand-0.5)*2*L;
        ym = (rand-0.5)*2*L;

        valid = true;
        for k = 1:k_gauss
                Dx = mod(abs(used(1,k)-xm),2*L);
                Dy = mod(abs(used(2,k)-ym),2*L);
                if norm([Dx;Dy]) < mindist
                        valid = false;
                        break;
                end;
        end;

        if valid
                Dx = mod(x-xm,2*L)-L;
                Dy = mod(y-ym,2*L)-L;
                Zaux = height/sigma ...
                     * exp(-5*(Dx.^2 + Dy.^2)/sigma.^2) - 0.1;
                Zaux(Zaux<0) = 0;
                Z = Z + Zaux;

                k_gauss = k_gauss+1;
                used(:,k_gauss) = [xm;ym];
        end;
end

surf(x,y,Z);
axis equal;
shading interp;

这样就清晰多了!对于周期性边界条件,这肯定是有效的,但最小距离不再被观察到,我一直在输出中得到重叠。 http://i.imgur.com/LfsH2PN.jpg - user5576489
@andy-m 我发现了错误,我会纠正代码。 - user2271770
2
@andy-m 代码已经修复。 - user2271770
1
@andy-m 很高兴我能帮到你。 - user2271770

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接