%% Figure S2 generator

clear all
close all


%% example for searching linear piecewise function
% colormap
cjet=jet;
cmap1=cjet([1 50 80 95],:);%線
cmap2=cjet([180 215 256],:);%交点
clear cjet
greycol=[1 1 1]*0.6;

%外周用設定
a0=[9 7  3 1];
b0=[20 18 12 5];

%外周内判定線分
p=(20-12)/(9-3);%a-b平面内の傾き
q=8;%a-b平面内の切片


%外周内
rng(1);
a = rand(1,20)*10+3;
b = 5+rand(1,20)*10;

%線分交点
x_int = (b0(2:end)-b0(1:end-1))./(a0(2:end)-a0(1:end-1));
y_int = -a0(1:end-1).*(b0(2:end)-b0(1:end-1))./(a0(2:end)-a0(1:end-1))+b0(1:end-1);

x_start = 0;
x_end = 10;

%% plot
figure(1)

%x-y平面
subplot(1,2,1)
hold on
for ii=1:length(a0)%境界線   
    switch ii
        case 1
            x_1=[x_start x_start];
            x=[x_start x_int(ii)];
            x1=[x_int(ii) x_end];
        case {2,3}    
            x_1=[x_start x_int(ii-1)];
            x=x_int(ii-1:ii);
            x1=[x_int(ii) x_end];
        case 4
            x_1=[x_start x_int(ii-1)];            
            x=[x_int(ii-1) x_end];
            x1=[x_end x_end];
    end
    
    plot(x_1,-a0(ii)*x_1+b0(ii),'--','Color',greycol);
    plot(x_1,-a0(ii)*x_1+b0(ii),'--','Color',cmap1(ii,:),'LineWidth',1.5);
    plot(x,-a0(ii)*x+b0(ii),'-','Color',cmap1(ii,:),'LineWidth',2);
    plot(x1,-a0(ii)*x1+b0(ii),'--','Color',greycol);
    plot(x1,-a0(ii)*x1+b0(ii),'--','Color',cmap1(ii,:),'LineWidth',1.5);
end 

for ii=1:length(a0)%交点
    if ii>1
        plot(x_int(ii-1), y_int(ii-1),'s','MarkerFaceColor',cmap2(ii-1,:),'MarkerEdgeColor',cmap2(ii-1,:),'MarkerSize',14);
    end
end

x=[x_start x_end];
for ii=1:length(a)%内部線
    if a(ii)*p+q > b(ii)                
        plot(x,-a(ii)*x+b(ii),'-','Color',greycol);        
    end
end


%a-b平面
subplot(1,2,2)
hold on
for ii=1:length(a0)%線分
     if ii<length(a0)
        plot(a0(ii:ii+1),b0(ii:ii+1),'-','Color',cmap2(ii,:),'LineWidth',2);
     end
end

for ii=1:length(a0)%端点
    plot(a0(ii),b0(ii),'o','Color',cmap1(ii,:),'MarkerFaceColor',cmap1(ii,:),'MarkerSize',10);      
end

x=[x_start x_end];
for ii=1:length(a)%内部点
    if a(ii)*p+q > b(ii)                
        plot(a(ii),b(ii),'o','Color',greycol,'MarkerFaceColor',greycol);          
    end
end 

%グラフラベル等
subplot(1,2,1)
box on
grid on
set(gca,'FontSize',16)
xlabel('$$ x $$','fontsize',24,'Interpreter','latex');
ylabel('$$ y $$','fontsize',24,'Interpreter','latex');
xlim([0 5]);
ylim([0 22]);


subplot(1,2,2)
yticks=0:2:20;
ylim([4 22]);
box on
grid on
set(gca,'FontSize',16)
xlabel('$$ a $$','fontsize',24,'Interpreter','latex');
ylabel('$$ b $$','fontsize',24,'Interpreter','latex');
set(gcf,'Color',[1 1 1]);
