clear all
close all
clc

cd('E:\NTNU\CRC\latex\matlab\ch8\MCD')

load('IceImage_290915_2_jpg.0000179.mat')
N = size(IceImage.Floe,1);
%---build the color vector between Red and Blue---
%---r : [1 0 0]; ---big
%---b : [0 0 1]; ---small

length_over_Pixel = 1.1794;

%---plot the polygon---
for i = 1:1:N
    Poly_Area(i) = IceImage.Floe(i).Polygon.Area*length_over_Pixel^2;
    Poly_MCD(i)=sqrt(Poly_Area(i)*4/pi);
end

color_limit_N = 30; % the first 30 bars are colored
Y_limi = IceImage.Param.NumPix_y;

Poly_struc = [IceImage.Floe.Polygon];
[Poly_counts,Poly_centers]=plot_color_bar_and_floe(color_limit_N,Poly_MCD,Y_limi,N,Poly_struc,length_over_Pixel)


%---plot the original ice floes-------
for i = 1:1:N
    Raw_Area(i) = IceImage.Floe(i).Area*length_over_Pixel^2;
    Raw_MCD(i)=sqrt(Raw_Area(i)*4/pi);
end

Raw_floe_struc = [IceImage.Floe];
[Raw_counts,Raw_centers]=plot_color_bar_and_floe(color_limit_N,Raw_MCD,Y_limi,N,Raw_floe_struc,length_over_Pixel)


%%
%---calculate the relative errors---

count_error=Poly_counts-[Raw_counts(2:end) 0];

color_M = jet(color_limit_N);
figure,
hold on
for i = 1:1:size(Raw_centers,2)
    %color_vec(i,:) = [min((centers(i)-C_min)/C_dist,1) 0 max(-(centers(i)-C_max)/C_dist,0)];
    
    h(i) = bar(Poly_centers(i),count_error(i),'BarWidth', 1);
    if i<=color_limit_N
        set(h(i),'FaceColor',color_M(i,:));
    else
        set(h(i),'FaceColor',color_M(color_limit_N,:));
    end
end
hold off
colormap(color_M)
caxis([Raw_centers(1) Raw_centers(color_limit_N)])
colorbar

set(gcf, 'Color','w')
set(gca,'Box','off','Fontsize',18)
xlabel(gca,'MCD $L_{i}$ [m]','Fontsize',25,'interpreter','Latex');
ylabel(gca,'Change in ice floes Numbers $\Delta N$','Fontsize',25,'interpreter','Latex');
grid on
axis tight

