• Remix
  • Share
  • New Entry

on 24 Nov 2023
  • 14
  • 20
  • 0
  • 0
  • 637
drawframe(1);
Write your drawframe function below
function drawframe(f)
persistent r c
n = 100;
[X,Y,Z] = peaks(n);
figure
surf(X,Y,Z)
hold on
[~,i] = max(Z,[],'all');
% i = i - 1;
if f==1
[r,c] = ind2sub([n,n],i);
end
x = X(i);
y = Y(i);
z = Z(i)+0.3;
p = plot3(x,y,z,'o',MarkerSize = 10,MarkerFaceColor = 'r');
for k = 1:f
[rnew,cnew] = trystep(Z,n,r,c);
if isequal(rnew,r) & isequal(cnew,c)
break
else
r = rnew;
c = cnew;
end
p.XData = X(r,c);
p.YData = Y(r,c);
p.ZData = Z(r,c);
end
function [r_new,c_new] = trystep(Z,n,r_old,c_old)
step_v = [-1,0,1];
R = max(min(r_old + step_v,n),1);
C = max(min(c_old + step_v,n),1);
[~,I] = min(Z(R,C),[],'all');
[r_step,c_step] = ind2sub([3,3],I);
r_new = max(min(r_old + r_step - 2,n),1);
c_new = max(min(c_old + c_step - 2,n),1);
end
end
Animation
Remix Tree