% Simulated dataset of triangles randomly
% sampled around a baseline triangle
%
% Samuli Siltanen Nov 2015

% Parameters for controlling the plots
lwidth = .5;
msize  = 10;
fsize  = 16;

% Load the dataset from file
load triangledataset trdata
n = size(trdata,1);

% Shift the sample mean of each column to zero
cvec = mean(trdata);
X    = trdata-repmat(cvec,n,1);

% Compute the principal components
XTX = X.'*X;
[P,D] = eig(XTX);

% Pick out the direction of most variation
mostv = P(:,end);

% Pick out the direction of second-most variation
mostv2 = P(:,end-1);

% Create a baseline triangle
blt = [0 sqrt(3) 1 0 -1 0];

% Plot the baseline triangle with red vertices
figure(1)
clf
plot(...
    [blt(1) blt(3) blt(5) blt(1)],...
    [blt(2) blt(4) blt(6) blt(2) ],'k');
hold on
plot(...
    [blt(1) blt(3) blt(5) blt(1)],...
    [blt(2) blt(4) blt(6) blt(2) ],'r.');

% Plot the two most important variations
Nt = 10;
t = linspace(-1,1,Nt);
for iii = 1:Nt
    tmp  = blt(:) + t(iii)*mostv(:);
    tmp2 = blt(:) + t(iii)*mostv2(:);
    plot(...
        [tmp(1) tmp(3) tmp(5)],...
        [tmp(2) tmp(4) tmp(6)],'b.');
    plot(...
        [tmp2(1) tmp2(3) tmp2(5)],...
        [tmp2(2) tmp2(4) tmp2(6)],'m.');
end

% Axis settings
axis equal
axis([-1.5 1.5 -.2 2.5])