How to create a surface with random spheres protruding?

2 visualizaciones (últimos 30 días)
Ayan Siddiq
Ayan Siddiq el 4 de Ag. de 2020
Comentada: Ayan Siddiq el 6 de Ag. de 2020
I'm trying to generate a surface with randomly placed spheres protruding from it.
  • Spheres can overlap.
  • Sphere diameters should be randomized on a normal distrubution. (e.g. using normrnd, or something similar)
  • Sphere height relative to surface should also be randomized on a normal distrubution.
I have this code currently which exhibits what I'm looking for except for the shapes being gaussian functions as opposed to spheres.
Thanks in advance!
close all % closes open graphs
clear all % clears out prev variables from workspace
clc % clears console / cmd window
rng default % keeps RNG seed constant
M = zeros(100,100); % initial matrix
gritDensity = 0.002; % grit per area
N = size(M,1) .* size(M,2) .* gritDensity; % number of grit
sigma = 4; % stdev (width) of Gaussian function
maxHeight = 10; % maximum height
[x,y] = meshgrid(1:size(M,1),1:size(M,2)); % creates grid
for k = 1:N
% random location of grit
xc = randi(size(M,1)); % picks random location on x axis between 0 and max value
yc = randi(size(M,2)); % picks random location on y axis between 0 and max value
% Gaussian function
exponent = ((x-xc).^2 + (y-yc).^2)./... % numerator
(2*sigma^2); % denominator
amplitude = rand()*maxHeight; % picks random grit height from maxHeight value
% add Gauss to the matrix M
M = max(M, amplitude*exp(-exponent));
end
surf(M)
axis equal
shading faceted

Respuesta aceptada

Bruno Luong
Bruno Luong el 5 de Ag. de 2020
Editada: Bruno Luong el 5 de Ag. de 2020
Is this you want?
x = linspace(0,1);
y = linspace(0,1);
[X,Y] = meshgrid(x,y);
Z = zeros(size(X));
N = 30;
for k=1:N
xc = min(x)+rand()*(max(x)-min(x));
yc = min(y)+rand()*(max(y)-min(y));
r = 0.1*rand(); % adapt to random law you want, eg r = 0.03*randn();
s = r^2-((X-xc).^2+(Y-yc).^2);
b = s>=0;
Z(b) = max(Z(b),sqrt(s(b)));
end
surf(x,y,Z);
axis equal
shading faceted

Más respuestas (0)

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by