• Remix
  • Share
  • New Entry

on 29 Nov 2023
  • 19
  • 56
  • 0
  • 2
  • 951
drawframe(1);
Write your drawframe function below
From my craters function on the File Exchange.
function drawframe(f)
persistent h
if f == 1
%% Input validation
rng default
nCraters = 1000;
craterRadLim = [.02, .5];
depthScale = 0.2;
n = 500;
%% Create cratered spherical surface
% Generate unit sphere coordinates
[Xs,Ys,Zs]=sphere(n-1);
% Define crater locations
% Random sample from a uniform distribution over sphere surface.
rand3D = randn(nCraters, 3);
craterLoc = rand3D.*repmat(1./sqrt(sum(rand3D.^2,2)),1,3);
% Define crater radii (gamma distribution, scaled).
craterRadii = rescale(randg(1,1,nCraters),...
craterRadLim(1), ...
craterRadLim(2));
% Compute linear distance from points along the sphere to each crater center.
% This is much faster than using spherical distance but still takes a few seconds.
distFcn=@(M,i)(M(:)-craterLoc(:,i)').^2;
distToCraters=sqrt(distFcn(Xs,1)+distFcn(Ys,2)+distFcn(Zs,3)); % N*M for N sphere points and M craters
% Crater depths are a function of crater radii so that larger craters make deeper impact.
% Crater profiles are defined by a logistic function.
craterDepths = depthScale * craterRadii;
D = distToCraters./craterRadii;
normProfile = 1./((1+.4.*exp(-10*D+4)).^2);
sphRadii = normProfile.*craterDepths+1-craterDepths;
% Modify sphere coordinates to add craters
% The radius of each spherical point is determined by the deepest
% crater that affected that point, or min(sphRadii,[],2).
[T,P]=cart2sph(Xs,Ys,Zs);
R=reshape(min(sphRadii,[],2),n,n);
% Convert back to cartesian coordinates
[Xe,Ye,Ze]=sph2cart(T,P,R);
% Plot results
axes('position',[0 0 1 1])
h = surf(Xe,Ye,Ze,'EdgeColor','none','FaceColor','interp');
set(gcf,'Color', 'k')
axis equal
% axis('vis3d')
axis('off')
light('Position',[1 0 1],'Color',[1 1 1]);
light('Position',[-1 0 -1],'Color',[.2 .2 .2]);
colormap(gray(265))
clim([-.4, 1.1])
camup([0, .6 .8])
camva(7)
material dull
axis vis3d
end
rotate(h, [-.6, .6 0], -2) % -360/49 for full rotation but it's too fast
end
Animation
Remix Tree