title: Matlab - Periodic tiling of a surface
body: <p>I have a surface that is representative of a barnacle distribution (randomly generated), however some of the points are generated off the desired area. I would like for any points that are generated off the surface to reappear on the other side. </p>
  
  <p>This is so that if you imagine this surface as a 'tile', when infinite tiles are laid down there are no jumps or gaps, it is periodic. </p>
  
  <pre><code>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&lt;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)])&lt;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&lt;0)=0;
      Z=Z+Zaux;
      gaussians=gaussians+1;
  end
  
  surf(x,y,Z);
  axis equal
  shading interp
  </code></pre>
  
  <p>This picture hopefully explains what is needed:</p>
  
  <p><a href="https://i.stack.imgur.com/7SM54.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7SM54.jpg" alt="enter image description here"></a></p>
  
  <p>I also need to keep the spacing though, so if xm,ym are within a distance of the guassian distribution on the other side of the plot, they need to be discounted. </p>
  
  <p>I have tried to get this to work but I cannot seem to find a way. Any help would be greatly appreciated!</p>