% piet_mondrian.m    9/16/2018
%
%  Create a piece of art of the proof that 1/2+1/4+1/8+1/16+...=1
%  in the style of piet_mondrian
%  Here's the wikipedia entry:
%  https://en.wikipedia.org/wiki/Piet_Mondrian
%
%  And here's the 'generative art' entry that got me thinking...
%  https://generativeartistry.com/tutorials/piet-mondrian/
%  https://codepen.io/pen?&editors=0010

% Set paper size
figure(1); clf;
fig = gcf;
fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 18 18]; % 18x18 inches at 600dpi
                                 %print('piet_mondrian','-djpg','-r300')

LW=16
n=1024  % Starting size
N=9   % Number of rectangles to draw
      %colors='byrbyrbyr';
      %colors=[0 0.5 1;
      %        1 1 0;
      %        1 0 0];
colors=[31 127 201;  % Mondrian color scheme 
        253 237 1;
        246 2 1]/255;
cn=1;

% Starting position
sx=0;
sy=0;
w=n;
h=n;
for i=1:N;
    r=rectangle('Position',[sx sy w h],'LineWidth',LW);
    axis off
    hold on
    if i==2 | i==5 | i==8; %mod(i,3)==0; % Color!
        fprintf('Coloring rectangle %1.0f: [%3.0f %3.0f %3.0f %3.0f]\n',[i sx sy w h]);
        r.FaceColor=colors(cn,:);
        cn=cn+1;
    else;
        r.FaceColor='w';
    end;  
    if mod(i,2); % vertical line next
        w=w/2;
        sx=sx+w; % Comment out to draw to the left instead of right
    else; % horizontal line next
        h=h/2;
        sy=sy+h;
    end;
    fprintf('Position of next rectangle: [%3.0f %3.0f %3.0f %3.0f]\n',[sx sy w h]);
end;

print('piet_mondrian','-dpng','-r300')
