Advertisement
Guest User

Matlab Purple Rain

a guest
Dec 16th, 2016
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.66 KB | None | 0 0
  1. clear;
  2.  
  3. raindropsize = 200;
  4. hf = figure(1);
  5. loc = get(hf,'Position');
  6. loc(3:4) = [600 400]; % make the figure 600x400
  7. set(hf, 'MenuBar','none', ...
  8.        'Position', loc, ...
  9.        'Color', [0.1 0.1 0.1], ...
  10.        'UserData', true, ...
  11.        'ButtonDownFcn', @buttondownfcn_callback);
  12. % have the axes take up the whole figure
  13. ha = axes('Position',[0,0,1,1]);
  14.  
  15. % turn off axis tick marks and labeling
  16. axis off;
  17. % turn off automatic scaling of axis
  18. axis manual;
  19.  
  20. % get random locations for rain drops
  21. % row 1 is x values
  22. % row 2 is y values
  23. % row 3 is z values
  24. dropLoc = rand(3,raindropsize);
  25.  
  26. % push the random y locations for each drop
  27. % so that they are just off screen
  28. dropLoc(2,:) = 1 + 0.2 * dropLoc(2,:);
  29.  
  30. % get speed and size based on z value
  31. [dropsize, dropspeed, dropwidth] = rainDropCalc(dropLoc(3,:));
  32.  
  33. % init the raindrops
  34. for k = 1:raindropsize
  35.     lx = [ dropLoc(1,k) dropLoc(1,k)];
  36.     ly = [ dropLoc(2,k)-dropsize(k) dropLoc(2,k)];
  37.     % There is a vectorized version of the line fuction
  38.     % but I hate to use this looped version so I can init
  39.     % each rainDrop to its own LineWidth.
  40.     hl = line(lx, ly, 'Color', [.9 .2 .9], 'LineWidth', dropwidth(k));
  41.     set(hl,'UserData', dropspeed(k));
  42. end
  43.  
  44. simulate = true;
  45. while simulate
  46.     hl = get(ha,'Children');
  47.     N = length(hl);
  48.     simulate = N > 0;
  49.     resetRainDrop = get(hf,'UserData');
  50.     for k = 1:N
  51.         y = get(hl(k),'YData');
  52.         dropspeed = get(hl(k),'UserData');
  53.         y = y - dropspeed;
  54.         if y(1) < 0
  55.             if resetRainDrop
  56.                 % the raindrop fell off the screen
  57.                 % so reset it back to the top so it can fall again.
  58.                 y = 1 + 0.2 * rand;
  59.                 [newsize, newspeed, newwidth] = rainDropCalc(rand);
  60.                 y = [y+newsize y];
  61.                 set(hl(k), 'XData', rand*ones(1,2),...
  62.                     'YData', y, 'Linewidth', newwidth, 'UserData', newspeed);
  63.             else
  64.                 % just delete the raindrop
  65.                 delete(hl(k));
  66.             end
  67.         else
  68.             % just move the raindrop
  69.             set(hl(k),'YData',y);
  70.         end
  71.     end
  72.     drawnow;
  73. end
  74. close(1);
  75.  
  76. function [dropsize, dropspeed, dropwidth] = rainDropCalc(z)
  77.  
  78. maxdropsize = 0.05;
  79. maxdropspeed = 0.04;
  80. maxdropwidth = 2.0;
  81.  
  82. % smaller z is closer, bigger, and faster
  83. dropsize = maxdropsize * (1 - 0.9 * z);
  84. dropspeed = maxdropspeed * (1 - 0.4 * z);
  85. dropwidth = maxdropwidth * (1 - 0.9 * z);
  86. end
  87.  
  88. function buttondownfcn_callback(src, ~)
  89. % this allows us to end the simulation gracefully when the
  90. % mouse button is pushed in the figure.
  91. set(src,'UserData',false);
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement