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));endsurf(M)axis equalshading faceted
Best Answer