function Envelop
% example on an envelope from union of two polyhedra
%
% M. Herceg 2014, ETH Zurich,
% requires MPT3

close all
%% printing parameters
label_font_size = 14;
tick_font_size = 10;
line_width = 0.8;
axeswidth=0.2;
% create new folder for storing figures
mkdir('figures');
figure_name_1 = ['.',filesep,'figures',filesep,'fig_4_4_a'];
figure_name_2 = ['.',filesep,'figures',filesep,'fig_4_4_b'];

%% core code
% vertices
V1 = [-6 0; -3 6; -3 -6; 0 0];
V2 = [0 0; 3 6; 3 -6; 6 0];
% construct two polyhedra
P1 = Polyhedron(V1);
P2 = Polyhedron(V2);
% construct union of two polyhedra
U = PolyUnion([P1, P2]);
% take an envelope from the union
Penv = U.envelope();

%plot figures
nfig_1 = figure;
h1 = U.plot('linewidth', line_width,'Color',[0.9 0.9 0.9]);
grid on
hold on
axis([-6 6 -15 15]);
ht1=text(-4, 0, '$\mathcal{R}_1$');
ht2=text(2, 0, '$\mathcal{R}_2$');
set(ht1, 'FontSize', label_font_size);
set(ht2, 'FontSize', label_font_size);


nfig_2 = figure;
h2 = Penv.plot('Color',[0.9 0.9 0.9],'LineWidth',line_width);
grid on
hold on
axis([-6 6 -15 15]);
ht = text(-2, 0, '$\text{env}(\mathcal{R})$');
set(ht, 'FontSize', label_font_size);


%% save book figures
figure(nfig_1)
% custom axis labels & font size
hx = xlabel('$x_1$');
set(hx, 'FontSize', label_font_size);
hy = ylabel('$x_2$');
set(hy, 'FontSize', label_font_size);

% font size of tick labels
set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);
set(gca,'YTickLabel',{''})
set(gca,'XTickLabel',{''})

% generate FIG file
disp('Hit any key to print the figure and continue.');
pause
saveas(gcf, [figure_name_1 '_matlab'], 'fig');

% % print using laprint
laprint(gcf, [figure_name_1 '_tex'],'scalefonts','off');

fprintf('Figure "%s" generated...\n', figure_name_1);

figure(nfig_2)
% custom axis labels & font size
hx = xlabel('$x_1$');
set(hx, 'FontSize', label_font_size);
hy = ylabel('$x_2$');
set(hy, 'FontSize', label_font_size);

% font size of tick labels
set(gca,'LineWidth',axeswidth)
set(gca,'FontSize', tick_font_size);
set(gca,'YTickLabel',{''})
set(gca,'XTickLabel',{''})

% generate FIG file
disp('Hit any key to print the figure and continue.');
pause
saveas(gcf, [figure_name_2 '_matlab'], 'fig');

% print using laprint
laprint(gcf, [figure_name_2 '_tex'],'scalefonts','off');

fprintf('Figure "%s" generated...\n', figure_name_2);

end