%This is to plot the decision boundary

%First, call the training program

gmm_all;


%Set the grid interval to check
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
interval=25;            %Change this to change the check interval




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%An empty matrix to store the prob of the point belongs to class 1 - 10
classify=zeros(1200./interval,4000./interval,10);
i=1;
j=0;
k=1;
ind=[];
ind_now=9;

%The range was selected to be 4000x1200 and the interval was set to be 10

%x=10;
%y=10;
classified_result=[];
%classified_result=zeros(4000./interval,1200./interval,10);

for x=1:interval:1200
    j=j+1;
    i=1;
    for y=1:interval:4000
        classify(i,j,1)=gmmprob(gmm1, [x, y]);
        classify(i,j,2)=gmmprob(gmm2, [x, y]);
        classify(i,j,3)=gmmprob(gmm3, [x, y]);
        classify(i,j,4)=gmmprob(gmm4, [x, y]);
        classify(i,j,5)=gmmprob(gmm5, [x, y]);
        classify(i,j,6)=gmmprob(gmm6, [x, y]);
        classify(i,j,7)=gmmprob(gmm7, [x, y]);
        classify(i,j,8)=gmmprob(gmm8, [x, y]);
        classify(i,j,9)=gmmprob(gmm9, [x, y]);
        classify(i,j,10)=gmmprob(gmm10, [x, y]);
        [val,ind(i,j)]=max(classify(i,j,:));
        classified_result(k,:,ind(i,j))=[x,y];
        k=k+1;
        i=i+1;
        
    end
end

k=1;
boundary_row=[];
boundary_col=[];
[row_no,col_no]=size(ind);
for i=1:row_no-1
    ind_now=ind(i,1);
    for j=1:col_no-1
        %Check if the 2 neighbour point belongs to the same clss
        ind_previous=ind_now;
        ind_now=ind(i,j);
        if ind_now~=ind_previous
            %record the area with class changing
            boundary_row(k,:)=[(i-1)*interval+1,((((j-1)*interval+1))+((((j-1)*interval+1))-interval))./2];
            k=k+1;
        end
    end
end

k=1;
for j=1:col_no-1
    ind_now=ind(1,j);
for i=1:row_no-1
    
        %Check if the 2 neighbour point belongs to the same clss
        ind_previous=ind_now;
        ind_now=ind(i,j);
        if ind_now~=ind_previous
            %record the area with class changing
            boundary_col(k,:)=[((((i-1)*interval+1))+((((i-1)*interval+1))-interval))./2,(j-1)*interval+1];
            k=k+1;
        end
    end
end



%plot the classification
figure, plot(classified_result(:,1,1), classified_result(:,2,1), 'ro' ,classified_result(:,1,2), classified_result(:,2,2), 'go', classified_result(:,1,3), classified_result(:,2,3), 'bo' , classified_result(:,1,4), classified_result(:,2,4), 'mo' , classified_result(:,1,5), classified_result(:,2,5), 'co' , classified_result(:,1,6), classified_result(:,2,6), 'ys' , classified_result(:,1,7), classified_result(:,2,7), 'ks' , classified_result(:,1,8), classified_result(:,2,8), 'rs' ,  classified_result(:,1,9), classified_result(:,2,9), 'ms' , classified_result(:,1,10), classified_result(:,2,10), 'gs' );                                        
legend('Class 1', 'Class 2' , 'Calss 3' , 'Class 4' , 'Class 5' , 'Class 6' , 'Class 7' , ' Class 8' , 'Class 9' , 'Class 10' , -1)
title('The classification in the xy plan')


%plot the boundary

%figure, plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', import_class3(:, 1), import_class3(:, 2), 'bo' , import_class4(:, 1), import_class4(:, 2), 'mo' , import_class5(:, 1), import_class5(:, 2), 'co' , import_class6(:, 1), import_class6(:, 2), 'bs' , import_class7(:, 1), import_class7(:, 2), 'cs' , import_class8(:, 1), import_class8(:, 2), 'rs' ,  import_class9(:, 1), import_class9(:, 2), 'ms' , import_class10(:, 1), import_class10(:, 2), 'gs',boundary_row(:,2),boundary_row(:,1),'.k',abs(boundary_col(:,2)-1),boundary_col(:,1),'.b','MarkerSize',5,gmm1.centres(:,1),gmm1.centres(:,2),'kx',gmm1.centres(:,1),gmm2.centres(:,2),'kx',gmm3.centres(:,1),gmm3.centres(:,2),'kx',gmm4.centres(:,1),gmm4.centres(:,2),'kx',gmm5.centres(:,1),gmm5.centres(:,2),'kx',gmm6.centres(:,1),gmm6.centres(:,2),'kx',gmm7.centres(:,1),gmm7.centres(:,2),'kx',gmm8.centres(:,1),gmm8.centres(:,2),'kx',gmm9.centres(:,1),gmm9.centres(:,2),'kx',gmm10.centres(:,1),gmm10.centres(:,2),'kx','MarkerSize',20);
figure,plot(import_class1(:, 1), import_class1(:, 2), 'ro' ,import_class2(:, 1), import_class2(:, 2), 'go', import_class3(:, 1), import_class3(:, 2), 'bo' , import_class4(:, 1), import_class4(:, 2), 'mo' , import_class5(:, 1), import_class5(:, 2), 'co' , import_class6(:, 1), import_class6(:, 2), 'bs' , import_class7(:, 1), import_class7(:, 2), 'cs' , import_class8(:, 1), import_class8(:, 2), 'rs' ,  import_class9(:, 1), import_class9(:, 2), 'ms' , import_class10(:, 1), import_class10(:, 2), 'gs','MarkerSize',5)
hold on
plot(abs(boundary_col(:,2)-1),boundary_col(:,1),'.b',boundary_row(:,2),boundary_row(:,1),'.k','MarkerSize',10)
plot(gmm1.centres(:,1),gmm1.centres(:,2),'kx',gmm2.centres(:,1),gmm2.centres(:,2),'kx',gmm3.centres(:,1),gmm3.centres(:,2),'kx',gmm4.centres(:,1),gmm4.centres(:,2),'kx',gmm5.centres(:,1),gmm5.centres(:,2),'kx',gmm6.centres(:,1),gmm6.centres(:,2),'kx',gmm7.centres(:,1),gmm7.centres(:,2),'kx',gmm8.centres(:,1),gmm8.centres(:,2),'kx',gmm9.centres(:,1),gmm9.centres(:,2),'kx',gmm10.centres(:,1),gmm10.centres(:,2),'kx','MarkerSize',10)
legend('Class 1', 'Class 2'  ,'Class 3','Class 4','Class 5','Class 6','Class 7','Class 8','Class 9','Class 10','The Boundary','','Centres', -1)
title('The Decision Boundary');
hold off
