移动光源的表面强度
Surface intensity for a moving light source
我正在尝试编写一个模拟程序,将光源移动到物体表面,在每一步中测量反射回光源的光强度。
然后绘制表面每个点的强度。每一步都添加到第一个图中。从而建立表面形貌图。
有点乱,能不能给点建议整理一下。
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
这是我所知道的。
一个完全符合您的代码的代码:(感谢@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
的所有值......等等。希望这能让你继续前进 ;)
我正在尝试编写一个模拟程序,将光源移动到物体表面,在每一步中测量反射回光源的光强度。
然后绘制表面每个点的强度。每一步都添加到第一个图中。从而建立表面形貌图。
有点乱,能不能给点建议整理一下。
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
这是我所知道的。
一个完全符合您的代码的代码:(感谢@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
的所有值......等等。希望这能让你继续前进 ;)