How to create a surface with random spheres protruding?

1 回表示 (過去 30 日間)
Ayan Siddiq
Ayan Siddiq 2020 年 8 月 4 日
コメント済み: Ayan Siddiq 2020 年 8 月 6 日
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

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 5 日
編集済み: Bruno Luong 2020 年 8 月 5 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by