%%
% Scipt to render images with hexagonal pixels
%%
clf
clc
%
s ={[1 0]' ; [0 1]'};
e = {0.5*[sqrt(3) -1]'; [0 1]'};

n = 10; % number of rows
m =  9; % number of cols

f(1) = figure(1);

% Square pixels
F = imread('PhantomS.png','png');
F = double(F)/double(max(F(:)));

n = size(F,1);
m = size(F,2);

for c=0:(m-1)
    for r=0:(n-1)
        p = c*s{1} + (n-r-1)*s{2};
        v = F(r+1,c+1);
        Sqpix(p,1.0,v);
    end
end
hold off
axis square
set(f(1),'Position',[1 1 1000 1000])

% Hexagonal pixels
f(2) = figure(2);

hold on
i=0;
for c=0:(m-1)
    for r=0:(n-1)
        p = c*e{1} + (n-r+i-1)*e{2};
        v = F(r+1,c+1);
        Hexpix(p,1.2,v)
    end
    if mod((c+1),2) == 0
       i = i + 1;
    end
end
hold off
set(f(2),'Position',[1 1 1000 1000])

%%
%% Local Functions
%%

%%
%% Procedure to draw a hexagonal pixel
%%
function A = Hexpix(p,s,l)
N_sides = 6;
t=(1/(N_sides*2):1/N_sides:1)'*2*pi;
xhex=0.5*s*sin(t);
yhex=0.5*s*cos(t);

patch(xhex+p(1),yhex+p(2),[l l l])
end

%%
%% Procedure to draw a square pixel
%%
function A = Sqpix(p,s,l)
xhex=[0.5 -0.5 -0.5  0.5];
yhex=[0.5  0.5 -0.5 -0.5];

patch(xhex+p(1),yhex+p(2),[l l l])
end

