% Chapter 13, Fig.13.6
% Eric Dubois, updated 2019-01-15
% Required functions:  Lattice_Points_2d, ds2nfu
%example to show the intersection and sum of lattices

clear all
close all

%sampling matrices and portion of R^2 to plot
V1 = [2 0; 0 2];
V2 = [2 1; 2 -1];
V3 = [4 2 ; 0 2];
V4 = [2 1; 0 1];
UL = [-6 -4]';
LR = [6 4]';

%get the points of the lattices in the desired region 
xout1 = Lattice_Points_2d(V1,UL,LR);
xout2 = Lattice_Points_2d(V2,UL,LR);
xout3 = Lattice_Points_2d(V3,UL,LR);
xout4 = Lattice_Points_2d(V4,UL,LR);
%create the plot
figure

%plot the points
plot(xout1(:,1),xout1(:,2),'ko',xout3(:,1),xout3(:,2),'b+',xout4(:,1),xout4(:,2),'rx' )
hold on
plot(xout2(:,1),xout2(:,2),'gv','MarkerSize',9)
axis equal
axis off
set(gca,'ydir','reverse'); 
axis([UL(1) LR(1) UL(2) LR(2)])

%create the axes
%x axis from -6. to +6.1
[xaxx xaxy] = ds2nfu([-6. 6.1], [0 0]);
annotation('arrow',xaxx,xaxy,'headlength',5,'headwidth',5);
%y axis from -4 to +4.1
[yaxx yaxy] = ds2nfu([0 0],[4.1 -4]);
annotation('arrow',yaxx,yaxy,'headlength',5,'headwidth',5);
text(6.2,-.3,'\itx')
text(0.2, 4.1, '\ity')

%plot x-axis tick and label
plot([1 1],[-.05 .05],'k')
text(1,-.17,'1')
%plot y-axis tick and label
plot([-.05 .05],[1 1],'k')
text(.15,1,'1')

set(gcf,'Color',[1 1 1]);