Advertisement
Guest User

Pixelated Gingerbread-Man

a guest
Dec 14th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.58 KB | None | 0 0
  1. function pixelatedGBM
  2.     rng(42)
  3.     [x,y] = gingerbreadman;
  4.     %scale and rotate our gingerbread man
  5.     r = [x y] * [cosd(135) -sind(135); sind(135) cosd(135)];
  6.     minR = min(r); maxR = max(r);
  7.     r = (r - repmat(minR,size(r,1),1))./repmat(maxR-minR,size(r,1),1);
  8.     r(:,2) = r(:,2).^1.5;
  9.    
  10.     %create a pixel representation of it
  11.     N = 25;
  12.     b = linspace(0,1,N);
  13.     dif = b(2)-b(1);
  14.     [xb,yb] = meshgrid(b,b);
  15.    
  16.     C = zeros(N);
  17.     x = r(:,1); y = r(:,2);
  18.     for i = 1:numel(xb)
  19.         C(i) = length(find(x >= xb(i) & x < xb(i) +dif & y >= yb(i) & y < yb(i)+dif));
  20.     end
  21.     C = reshape(C,size(xb));
  22.     %smooth a little for better results
  23.     smooth = @(A,L) ((eye(size(A,1)) + L ^ 2 * diff(eye(size(A,1)),2)' * diff(eye(size(A,1)),2) + 2 * L * diff(eye(size(A,1)))' * diff(eye(size(A,1)))) \ A);
  24.     D = smooth(smooth(C,0.1)',0.1)';
  25.    
  26.     %let's draw it :)
  27.     set(figure,'Position',[0 0 300 400],'Color',[1 1 1])
  28.     movegui(gcf,'center')
  29.     set(surf(xb,yb,zeros(size(D)),D),'ZData',xb.*0-0.01);
  30.     view(2); shading flat; grid off; axis off equal;
  31.     colormap([1 1 1; pink(19)])
  32.     hold on
  33.     t = linspace(0,2*pi,50);
  34.     set(fill((sin(t))/9+0.5,(sin(2*t))/18+0.65,[0 .8 0]),'EdgeAlpha',0)
  35.     set(fill((sin(t))/30+0.5,(cos(t))/30+0.65,[0 .9 0]),'EdgeAlpha',0)
  36.     t = linspace(0,2*pi,5);
  37.     set(fill((sin(t))/20+0.5,(cos(t))/20+0.5,[.8 0 0]),'EdgeAlpha',0)
  38.     set(fill((sin(t))/20+0.5,(cos(t))/20+0.3,[.8 0 0]),'EdgeAlpha',0)
  39.     plot3(r(1:10:end,1),r(1:10:end,2),r(1:10:end,1)*0-0.005,'.','MarkerSize',3,'MarkerEdgeColor',[1 1 1]);
  40. end
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement