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_kolam1D(size_of_kolam)
function M=propose_kolam1D(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);

for i=2:hp+1
    for j=2: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
    end
end
Mat(hp+2,1)=1;
Mat(1,hp+2)=1;
for j=2:hp+1
    Valid_by_Up=mate_pt_dn{pt_dn(Mat(hp+1,j))+1};
    Valid_by_Lt=mate_pt_rt{pt_rt(Mat(hp+2,j-1))+1};
    Valids=intersect(Valid_by_Up,Valid_by_Lt);
    Valids=intersect(Valids,v_self);
    try
        V=Valids(ceil(rand*length(Valids)));
        Mat(hp+2,j)=V;
    catch
        Mat(hp+2,j)=1;
    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
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