matlab - Surface intensity for a moving light source -


i’m trying write simulation move light source on surface of object, on each step measure intensity of reflected light light source.

then plot intensity each point of surface. each step adding first plot. building picture of surface topography.

it bit messy if can suggest way tidy up.

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;   i=1:ylength     t=t+1;     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')     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 

this far have got .

a code yours does: (thanks @hoki in simplifying code more)

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 linestyle because looks cooler axis([xmin xmax ymin ymax])  xlabel('x') ylabel('y') zlabel('z') % added repetitions , decreased step size, plus making go forward , backward. repetitions=4; jj=1:repetitions     hl = light('position',[-10,0,50]) ;     %// create light source    i=-10:2.5:10        set(hl,'position',[i,0,50]) ;       %// set new light position        drawnow                             %// flush graphic pipeline        pause (0.1)                         %// let human user see    end    delete(hl)   %// delete light source end 

this gives following gif:

http://i.imgur.com/42ffyll.gifv

about rest part of code....

remember

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 

is literally same as:

if (t>=d && t<=d+5)    d(s,t)=z(s,t); else     d(s,t)=0; end 

this should reduce code. there vectorization.

you can do

d(s,(t>=d && t<=d+5))=z(s,(t>=d && t<=d+5))); 

for values of s... etc etc. hope gets going ;)


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -