Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

spread_virus

By: a guest on Apr 21st, 2013  |  syntax: MatLab  |  size: 2.38 KB  |  views: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function virus_spread_viz()
  2.    
  3.     [X,Y,Z,Q] = get_earth_surface();
  4.     %C = get_surf_color(Q);
  5.     C = get_surf_color_random(Q);
  6.  
  7.     clf
  8.     [xx,yy,zz] = get_curved_lines();
  9.     hLine = zeros(size(xx,1),1);
  10.     for i=1:numel(hLine)
  11.         hLine(i) = line(xx(i,:),yy(i,:),zz(i,:), 'Color','m', ...
  12.             'LineWidth',2, 'LineStyle','-');
  13.     end
  14.  
  15.     hTxt = text(9,20,3.5, '', 'BackgroundColor',[.7 .9 .7]);
  16.  
  17.     hSurf = surface('XData',X, 'YData',Y, 'ZData',Z.*0.5, ...
  18.         'CData',C);
  19.     axis tight; grid on; box on; view(-15,30)
  20.     zlim([0 6]); %axis([1 40 1 20 0 10])
  21.  
  22.     for i=1:100
  23.         C = get_surf_color_random(Q);
  24.         set(hSurf, 'CData',C)
  25.         set(hLine(1), 'Visible',get_visibility())
  26.         set(hLine(2), 'Color',rand(1,3))
  27.         set(hLine(3), 'LineWidth',randi([1,5]))
  28.         set(hTxt, 'String',get_string_random())
  29.         drawnow
  30.         pause(0.1)
  31.     end
  32. end
  33.  
  34. function [X,Y,Z,Q] = get_earth_surface()
  35.     % lets create earth surface (X/Y/Z coords)
  36.     [X,Y] = meshgrid(1:20,1:40);
  37.  
  38.     % continents
  39.     Q = zeros(size(X));
  40.     Q(20:35,3:7) = 1;   % north america
  41.     Q(5:10,5:7) = 2;    % south america
  42.     Q(7:17,10:14) = 3;  % africa
  43.     Q(21:33,11:18) = 4; % europe
  44.     Q(9:12,17:19) = 5;  % australia
  45.  
  46.     Z = zeros(size(X));
  47.     Z(Q>0) = 1;
  48. end
  49.  
  50. function C = get_surf_color_random(Q)
  51.     sz = [40,20];
  52.     C_r = zeros(sz);
  53.     C_g = zeros(sz);
  54.     C_b = ones(sz);
  55.     for i=1:5
  56.         C_r(Q==i) = rand();
  57.         C_g(Q==i) = rand();
  58.         C_b(Q==i) = rand();
  59.     end
  60.     C = cat(3, C_r,C_g,C_b);
  61. end
  62.  
  63. function [x,y,z] = get_curved_lines()
  64.     t = linspace(1,3,30);
  65.     x = zeros(3,30);
  66.     y = zeros(3,30);
  67.     z = zeros(3,30);
  68.  
  69.     % [7,7,0]  -> [15,30,0]
  70.     x(1,:) = spline(1:3, [7,11,15], t);
  71.     y(1,:) = spline(1:3, [7,18.5,30], t);
  72.     z(1,:) = spline(1:3, [0,2,0], t);
  73.     % [5,30,0] -> [13,10,0]
  74.     x(2,:) = spline(1:3, [5,9,13], t);
  75.     y(2,:) = spline(1:3, [30,20,10], t);
  76.     z(2,:) = spline(1:3, [0,3,0], t);
  77.     % [18,10,0] -> [16,26,0]
  78.     x(3,:) = spline(1:3, [18,17,16], t);
  79.     y(3,:) = spline(1:3, [10,18,26], t);
  80.     z(3,:) = spline(1:3, [0,2,0], t);
  81. end
  82.  
  83. function str = get_string_random()
  84.     alpha = 'a':'z';
  85.     str = alpha(randperm(numel(alpha),randi([4 10])));
  86. end
  87.  
  88. function vis = get_visibility()
  89.     states = {'on','off'};
  90.     vis = states{randi(numel(states))};
  91. end