Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear;
- raindropsize = 200;
- hf = figure(1);
- loc = get(hf,'Position');
- loc(3:4) = [600 400]; % make the figure 600x400
- set(hf, 'MenuBar','none', ...
- 'Position', loc, ...
- 'Color', [0.1 0.1 0.1], ...
- 'UserData', true, ...
- 'ButtonDownFcn', @buttondownfcn_callback);
- % have the axes take up the whole figure
- ha = axes('Position',[0,0,1,1]);
- % turn off axis tick marks and labeling
- axis off;
- % turn off automatic scaling of axis
- axis manual;
- % get random locations for rain drops
- % row 1 is x values
- % row 2 is y values
- % row 3 is z values
- dropLoc = rand(3,raindropsize);
- % push the random y locations for each drop
- % so that they are just off screen
- dropLoc(2,:) = 1 + 0.2 * dropLoc(2,:);
- % get speed and size based on z value
- [dropsize, dropspeed, dropwidth] = rainDropCalc(dropLoc(3,:));
- % init the raindrops
- for k = 1:raindropsize
- lx = [ dropLoc(1,k) dropLoc(1,k)];
- ly = [ dropLoc(2,k)-dropsize(k) dropLoc(2,k)];
- % There is a vectorized version of the line fuction
- % but I hate to use this looped version so I can init
- % each rainDrop to its own LineWidth.
- hl = line(lx, ly, 'Color', [.9 .2 .9], 'LineWidth', dropwidth(k));
- set(hl,'UserData', dropspeed(k));
- end
- simulate = true;
- while simulate
- hl = get(ha,'Children');
- N = length(hl);
- simulate = N > 0;
- resetRainDrop = get(hf,'UserData');
- for k = 1:N
- y = get(hl(k),'YData');
- dropspeed = get(hl(k),'UserData');
- y = y - dropspeed;
- if y(1) < 0
- if resetRainDrop
- % the raindrop fell off the screen
- % so reset it back to the top so it can fall again.
- y = 1 + 0.2 * rand;
- [newsize, newspeed, newwidth] = rainDropCalc(rand);
- y = [y+newsize y];
- set(hl(k), 'XData', rand*ones(1,2),...
- 'YData', y, 'Linewidth', newwidth, 'UserData', newspeed);
- else
- % just delete the raindrop
- delete(hl(k));
- end
- else
- % just move the raindrop
- set(hl(k),'YData',y);
- end
- end
- drawnow;
- end
- close(1);
- function [dropsize, dropspeed, dropwidth] = rainDropCalc(z)
- maxdropsize = 0.05;
- maxdropspeed = 0.04;
- maxdropwidth = 2.0;
- % smaller z is closer, bigger, and faster
- dropsize = maxdropsize * (1 - 0.9 * z);
- dropspeed = maxdropspeed * (1 - 0.4 * z);
- dropwidth = maxdropwidth * (1 - 0.9 * z);
- end
- function buttondownfcn_callback(src, ~)
- % this allows us to end the simulation gracefully when the
- % mouse button is pushed in the figure.
- set(src,'UserData',false);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement