image thumbnail

Kolam Generator

by

 

30 Apr 2012 (Updated )

This suite of functions can be used to generate geometric line drawing of Kolams.

M=propose_kolam2D(size_of_kolam)
function M=propose_kolam2D(size_of_kolam)


pt_dn=[0 1 0 0 0 1 0 0 1 0 1 1 1 0 1 1];
pt_rt=[0 0 1 0 0 1 1 0 0 1 0 1 1 1 0 1];

mate_pt_dn{1}=[2 3 5 6 9 10 12];
mate_pt_dn{2}=setdiff(2:16,mate_pt_dn{1});
mate_pt_rt{1}=[2 3 4 6 7 11 13];
mate_pt_rt{2}=setdiff(2:16,mate_pt_rt{1});
odd = (mod(size_of_kolam,2) ~= 0); 
if odd
hp=fix((size_of_kolam-1)/2);
else
    hp=size_of_kolam/2;
end
Mat=ones(hp+1);
h_inv=[1 2 5 4 3 9 8 7 6 10 11 12 15 14 13 16];
v_inv=[1 4 3 2 5 7 6 9 8 10 11 14 13 12 15 16];
h_self=find(h_inv==1:16);
v_self=find(v_inv==1:16);

flip_90=[1 3 2 5 4 6 9 8 7 11 10 13 12 15 14 16];
diagsym=[1 6 8 16];
Mat(1,hp+2)=1;
for i=2:hp+1
    if i==2
        ch=ceil(rand*2);
        Mat(2,2)=diagsym(ch);
    else
    Valid_by_Up=mate_pt_dn{pt_dn(Mat(i-1,i))+1};
    Valid_by_Lt=mate_pt_rt{pt_rt(Mat(i,i-1))+1};
    Valids=intersect(Valid_by_Up,Valid_by_Lt);
    Valids=intersect(Valids,diagsym);
    try
        V=Valids(ceil(rand*length(Valids)));
        Mat(i,i)=V;
    catch
        Mat(i,i)=1;
    end
    end
    for j=(i+1):(hp+1)
        Valid_by_Up=mate_pt_dn{pt_dn(Mat(i-1,j))+1};
        Valid_by_Lt=mate_pt_rt{pt_rt(Mat(i,j-1))+1};
        Valids=intersect(Valid_by_Up,Valid_by_Lt);
        try
            V=Valids(ceil(rand*length(Valids)));
            Mat(i,j)=V;
        catch
            Mat(i,j)=1;
        end
        Mat(j,i)=flip_90(Mat(i,j));
    end
end



for i=2:hp+1
    Valid_by_Up=mate_pt_dn{pt_dn(Mat(i-1,hp+2))+1};
    Valid_by_Lt=mate_pt_rt{pt_rt(Mat(i,hp+1))+1};
    Valids=intersect(Valid_by_Up,Valid_by_Lt);
    Valids=intersect(Valids,h_self);
    try
        V=Valids(ceil(rand*length(Valids)));
        Mat(i,hp+2)=V;
    catch
        Mat(i,hp+2)=1;
    end
    Mat(hp+2,i)=flip_90(Mat(i,hp+2));
end

Valid_by_Up=mate_pt_dn{pt_dn(Mat(hp+1,hp+2))+1};
Valid_by_Lt=mate_pt_rt{pt_rt(Mat(hp+2,hp+1))+1};
Valids=intersect(Valid_by_Up,Valid_by_Lt);
Valids=intersect(Valids,h_self);
Valids=intersect(Valids,v_self);
try
    V=Valids(ceil(rand*length(Valids)));
    Mat(hp+2,hp+2)=V;
catch
    Mat(hp+2,hp+2)=1;
end


Mat1=Mat(2:hp+1,2:hp+1);
Mat3=v_inv(Mat1(end:-1:1,:));
Mat2=h_inv(Mat1(:,end:-1:1)); 
Mat4=v_inv(Mat2(end:-1:1,:));
if odd
M=[Mat1 Mat(2:end-1,end) Mat2;
    Mat(end,2:end) h_inv(Mat(end,(end-1):-1:2));
    Mat3 v_inv(Mat((end-1):-1:2,end))' Mat4];
else
    M=[Mat1 Mat2 ; Mat3 Mat4];
end

Contact us