• Remix
  • Share
  • New Entry

on 13 Nov 2023
  • 24
  • 38
  • 0
  • 0
  • 1049
function drawframe(f)
% create the sphere
[n, r] = deal(20);
[x, y, z] = sphere(n);
x = x * r; y = y * r; z = z * r; % Scale the sphere by r
% plot the sphere
surf(x,y,z, 'EdgeAlpha', .5, 'FaceAlpha', .5);
% generate the randon spin directions
randSpin = randi(3, size(x, 1)-1);
% Create the patches
for i = 1:size(x, 1)-1
for j = 1:size(x, 2)-1
% Get the vertices of the quadrilateral patch
v =[x(i, j), x(i+1, j), x(i+1, j+1), x(i, j+1);
y(i, j), y(i+1, j), y(i+1, j+1), y(i, j+1);
z(i, j), z(i+1, j), z(i+1, j+1), z(i, j+1)]';
% push the vertexes a bit out with respect of the center
v = v + v(1,:) * (f-1) * .03;
% Map the average z-coordinate to a color
c = mapColor(mean(v(:, 3)), z);
% spin each patch in the random direction
v = rotate(v, randSpin(i,j), f);
% render the patch
patch('Vertices', v, 'Faces', [1 2 3 4], 'FaceColor', c, 'EdgeAlpha', .5, 'FaceAlpha', .7);
end
end
% rotate the view
view(f, 45);
% axis adjustments
l = [-30 30];
xlim(l)
ylim(l)
zlim(l)
set(gcf,'Color',[.1 .1 .1]);
axis off;
end
function v = rotate(v, spin, f)
% Compute the center of the polygon
center = mean(v);
% Translate the polygon to the origin
v = v - center;
% Define the rotation matrix
a = (f-1)* pi/48; % Change this value to change the angle of rotation
if spin == 1
R = [1 0 0; 0 cos(a) -sin(a); 0 sin(a) cos(a)];
elseif spin == 2
R = [cos(a) 0 sin(a); 0 1 0; -sin(a) 0 cos(a)];
elseif spin == 3
R = [cos(a) -sin(a) 0; sin(a) cos(a) 0; 0 0 1];
end
% Rotate the polygon
v = (R * v')';
% Translate back
v = v + center;
end
% Map a z-coordinate to a color based on the current colormap
function c = mapColor(z, zRange)
cmap = colormap;
cIndex = fix((z - min(zRange(:))) / range(zRange(:)) * (size(cmap, 1) - 1)) + 1;
c = cmap(max(min(abs(cIndex),255), 1), :);
end
Animation
Remix Tree