大物相关的光的衍射图像绘制
效果图:
——————————————————————————
滑动条拖动到不同位置时会绘制不同的衍射图像
完整代码:
function OPD
hold on,axis equal
axis([-10,40,-30,30])
set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
set(gca,'color','w')
plot([0,0;0,0],[5,-5;30,-30],'color',[0.1,0.1,0.1],'LineWidth',2)
plot([-10,-10;0,0],[5,-5;5,-5],'color',[0.3,0.3,0.3],'LineWidth',0.5)
text(-5,5,'>','color',[0.3,0.3,0.3])
text(-5,-5,'>','color',[0.3,0.3,0.3])
plot([30;30],[20,-20],'color',[0.1,0.1,0.1],'LineWidth',4,'tag','board')
plot([0;40],[5,5+(20-5)/3*4],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[5,5+(-20-5)/3*4],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[-5,-5+(20+5)/3*4],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[-5,-5+(-20+5)/3*4],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
light_source1=[0 5];
light_source2=[0 -5];
n=1;
for i=-20:0.1:20
pos=[30,i];
v1=pos-light_source1;v2=pos-light_source2;
l1=sqrt(sum(v1.^2));l2=sqrt(sum(v2.^2));
cos1=cos(l1*2.5);cos2=cos(l2*2.5);
skewing=abs(cos1+cos2);
color_bar(n,1:3)=ones(1,3).*(skewing/2);
n=n+1;
end
colormap(color_bar);
colorbar
sl=uicontrol('style','slider',...
'position',[210,20,210,20],...
'value',2/3,...
'callback',@movesl);
function movesl(~,~)
x=10+30*get(sl,'value');
delete(findobj('tag','light'));
delete(findobj('tag','board'));
plot([x;x],[20,-20],'color',[0.1,0.1,0.1],'LineWidth',4,'tag','board')
plot([0;40],[5,5+(20-5)/x*40],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[5,5+(-20-5)/x*40],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[-5,-5+(20+5)/x*40],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
plot([0;40],[-5,-5+(-20+5)/x*40],'color',[0.3,0.3,0.3],'LineWidth',0.5,'tag','light')
n=1;
for j=-20:0.1:20
pos=[x,j];
v1=pos-light_source1;v2=pos-light_source2;
l1=sqrt(sum(v1.^2));l2=sqrt(sum(v2.^2));
cos1=cos(l1*2.5);cos2=cos(l2*2.5);
skewing=abs(cos1+cos2);
color_bar(n,1:3)=ones(1,3).*(skewing/2);
n=n+1;
end
colormap(color_bar)
colorbar
end
end