我试图写一个模拟,它将一个光源移动到物体的表面,每一步测量反射光的强度回到光源。
然后从表面的每一点绘制强度图。将每一步添加到第一幅图中。从而形成了一幅关于表面形貌的图片。
如果你能提出一种清理的方法,那就有点麻烦了。
x = -10:0.25:10;
y = -10:0.25:10;
xlength=length(x);
ylength=length(y);
ymin=min(y);
ymax=max(y);
xmin=min(x);
xmax=max(x);
D=zeros(xlength);
[X,Y] = meshgrid(x,y);
z = 10-(X.^2)-(Y.^2)+5*sin(X);
rnd=rand(xlength);
c=randi(xlength);
d=randi(xlength);
s=1;
t=0;
for i=1:ylength
t=t+1;
for j=1:xlength
if s==c
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end
elseif s==c+1
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end
elseif s==c+2
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end
elseif s==c+3
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end
elseif s==(c+4)
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end
else
D(s,t)=0;
end
s=s+1;
end
s=1;
end
z1=z -(D/20);
z2=z1-z;
s=0;
figure
surf(X,Y,z)
axis([xmin xmax ymin ymax])
xlabel('X')
ylabel('Y')
zlabel('Z')
for i=-10:2.5:10
hold on
light('position',[i,0,50])
surf(X,Y,z,'EdgeColor', 'none')
axis([xmin xmax ymin ymax])
drawnow
pause (1)
delete(findall(gcf,'Type','light'))
hold off
end这就是我所能得到的。
发布于 2015-08-05 12:00:21
一种与您的代码完全相同的代码:(感谢@Hoki帮助简化代码)
clear;clc
x = -10:0.25:10;
y = -10:0.25:10;
xlength=length(x);
ylength=length(y);
ymin=min(y);
ymax=max(y);
xmin=min(x);
xmax=max(x);
[X,Y] = meshgrid(x,y);
z = 10-(X.^2)-(Y.^2)+5*sin(X);
%% plot!
figure
surf(X,Y,z,'EdgeColor', 'none','lineStyle','none') % taken out the linestyle because it looks cooler
axis([xmin xmax ymin ymax])
xlabel('X')
ylabel('Y')
zlabel('Z')
% Added repetitions and decreased step size, plus making it go forward and backward.
repetitions=4;
for jj=1:repetitions
hl = light('position',[-10,0,50]) ; %// create the light source
for i=-10:2.5:10
set(hl,'position',[i,0,50]) ; %// set the new light position
drawnow %// flush graphic pipeline
pause (0.1) %// let human user see something
end
delete(hl) %// delete the light source
end这给出了以下gif:
http://i.imgur.com/42ffYll.gifv
关于密码的其余部分..。
记住这一点
if t==d
D(s,t)=z(s,t);
elseif t==d+1
D(s,t)=z(s,t);
elseif t==d+2
D(s,t)=z(s,t);
elseif t==d+3
D(s,t)=z(s,t);
elseif t==d+4
D(s,t)=z(s,t);
elseif t==d+5
D(s,t)=z(s,t);
else
D(s,t)=0;
end实际上与以下内容相同:
if (t>=d && t<=d+5)
D(s,t)=z(s,t);
else
D(s,t)=0;
end这将帮助您减少代码。也有向量化。
你可以做到
D(s,(t>=d && t<=d+5))=z(s,(t>=d && t<=d+5)));所有的s值..。等等,希望这能让你走;)
https://stackoverflow.com/questions/31829847
复制相似问题