function detext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% determines the extended inner bsplines & computes coefficients
% n                  : degree
% h                  : grid width
% D=[i j xx(i) yy(j)]: all b-splines supporting the region 
% bb                 : extended(3), inner(2) and outer(1) b-splines
% gi, gj             : inner and outer b-splines
% C                  : boundary contours
% E                  : extended matrix
% Created by G.Apaydin in June 2006
global gi gj bb h D n C E NC bound;
F=sparse(length(gi),length(gj));cc=bb;
cwd=pwd;cd('input');load extcoef.mat;cd(cwd);
ee1=e1{n};ee2=e2{n};ee3=e3{n};ee4=e4{n};ee5=e5{n};
a=9/180*pi; %increase the range of angle in degrees 
%determine the angle of outer bspline closest to the nearest boundary
for k=1:length(gj),
    q=D(gj(k),1);r=D(gj(k),2);px=D(gj(k),3)+(n+1)*h/2;py=D(gj(k),4)+(n+1)*h/2;
    vc=[];
    for kk=1:NC,
        ff=bound{kk};
        if ff.cont==1,
            x1=fminsearch(@(x) abs(((x(1)-ff.sx)/ff.rx)^2+((x(2)-ff.sy)/ff.ry)^2-1)+abs((x(2)-ff.sy)*(x(1)-px)*ff.rx^2-(x(2)-py)*(x(1)-ff.sx)*ff.ry^2),[px;py]);
            if x1(2)==py, aci=atan2((ff.sy-x1(2))*ff.rx^2,(ff.sx-x1(1))*ff.ry^2);  else aci=atan2(x1(2)-py,x1(1)-px);end
            vc=[vc;(x1(1)-px)^2+(x1(2)-py)^2 aci];   
            %plot(x1(1),x1(2),'r*');            
        else
            cx=C(2*kk-1,:)'-px;cy=C(2*kk,:)'-py;
            vec=[cx.^2+cy.^2 atan2(cy,cx)];
            v=find(vec(:,1)==min(vec(:,1)));
            if vec(v(1),1)==0,vec(v(1),2)=(2*pi*(v(1)-1)/(size(C,2)-1)+3*pi/2);
            display('Burada1');end,
            vc=[vc;vec(v(1),:)];
        end
    end,
    v=find(vc(:,1)==min(vc(:,1)));
    ang=vc(v(1),2);

     %determine extended bsplines
    NN=1:n+1;SZ=size(bb,1);
    
    if abs(ang)<=pi/4-a,%[-43,43]
        if bb(r,q+NN)==2,cc(r,q+NN)=3;%EAST
            for i=NN,F(find(gi==((q-1+i)*SZ+r)),k)=ee1(i);end
            %F(find(gi==(q*size(bb,1)+r))+[0:n]*(size(bb,1)),k)=ee1(1:n+1);F  
        elseif ang>0,%NORTHEAST
            if bb(r+1,q+NN)==2,cc(r+NN,q+NN)=3;
                for i=NN,
                    for j=NN,
                        F(find(gi==((q-1+i)*SZ+r+j)),k)=ee2(i,j);
                    end
                end
            end
        elseif ang<0,%SOUTHEAST
            if bb(r-1,q+NN)==2,cc(r-NN,q+NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-1+i)*SZ+r+j-n-2)),k)=ee3(i,j);  
                    end
                end
            end
        else display('Hata 1');
        end
    

    elseif abs(ang-pi/2)<=pi/4-a,%NORTH
        if bb(r+NN,q)==2,cc(r+NN,q)=3;
            for i=NN,F(find(gi==((q-1)*SZ+r+i)),k)=ee1(i);end
        elseif ang<pi/2,%NORTHEAST
            if bb(r+NN,q+1)==2,cc(r+NN,q+NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-1+i)*SZ+r+j)),k)=ee2(i,j);
                    end
                end
            end
        elseif ang>pi/2,%NORTHWEST
            if bb(r+NN,q-1)==2,cc(r+NN,q-NN)=3;
                for i=NN
                    for j=NN
                       F(find(gi==((q-n-3+i)*SZ+r+j)),k)=ee4(i,j);
                    end
                end
            end
        else display('Hata 2');
        end
    
    
    elseif abs(ang+pi/2)<=pi/4-a,%SOUTH
        if bb(r-NN,q)==2,cc(r-NN,q)=3;
            for i=NN,F(find(gi==((q-1)*SZ+r-i)),k)=ee1(i);end
        elseif ang>-pi/2,%SOUTHEAST
            if bb(r-NN,q+1)==2,cc(r-NN,q+NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-1+i)*SZ+r+j-n-2)),k)=ee3(i,j);
                    end
                end
            end
        elseif ang<-pi/2,%SOUTHWEST
            if bb(r-NN,q-1)==2,cc(r-NN,q-NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-n-3+i)*SZ+r+j-n-2)),k)=ee5(i,j);
                    end
                end
            end
        else display('Hata 3');
        end
    
    elseif ang>=3*pi/4+a | ang<=-3*pi/4-a,%WEST
        if bb(r,q-NN)==2,cc(r,q-NN)=3;
            for i=NN,F(find(gi==((q-1-i)*SZ+r)),k)=ee1(i);end
        elseif  ang>3*pi/4,%NORTHWEST
            if bb(r+1,q-NN)==2,cc(r+NN,q-NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-n-3+i)*SZ+r+j)),k)=ee4(i,j);
                    end
                end
            end
        elseif ang<-3*pi/4,%SOUTHWEST
            if bb(r-1,q-NN)==2,cc(r-NN,q-NN)=3;
                for i=NN
                    for j=NN
                        F(find(gi==((q-n-3+i)*SZ+r+j-n-2)),k)=ee5(i,j);
                    end
                end
            end
        else display('Hata 4');
        end
        
    elseif abs(ang-pi/4)<a,%NORTHEAST
        if bb(r+NN,q+NN)==2,cc(r+NN,q+NN)=3;
            for i=NN
                for j=NN
                    F(find(gi==((q-1+i)*SZ+r+j)),k)=ee2(i,j);
                end
            end
        else display('Hata 5');
        end

    elseif abs(ang-3*pi/4)<a,%NORTHWEST
        if bb(r+NN,q-NN)==2,cc(r+NN,q-NN)=3;
            for i=NN
                for j=NN
                    F(find(gi==((q-n-3+i)*SZ+r+j)),k)=ee4(i,j);
                end
            end
        else display('Hata 6');
        end
        
    elseif abs(ang+pi/4)<a,%SOUTHEAST
        if bb(r-NN,q+NN)==2,cc(r-NN,q+NN)=3;
            for i=NN
                for j=NN
                    F(find(gi==((q-1+i)*SZ+r+j-n-2)),k)=ee3(i,j);
                end
            end
        else display('Hata 7');
        end
    
    elseif abs(ang+3*pi/4)<a,%SOUTHWEST
        if bb(r-NN,q-NN)==2,cc(r-NN,q-NN)=3;
            for i=NN
                for j=NN
                    F(find(gi==((q-n-3+i)*SZ+r+j-n-2)),k)=ee5(i,j);
                end
            end
        else display('Hata 8');
        end
    end
end

E=[speye(length(gi)) F];bb=cc;