% Demo code for correspondence (part 2)
% Alex Berg aberg@cs.stonybrook.edu


% simple example with least squares
%
% this example maps 1d points (the x values) to 1d points (the y
% values)
%
% for your assignment you will need to find affine mappings between
% 2d points.
%
%


clear all

figure(1);
clf
axis([0 10 0 10]);
hold on


fprintf('left click on points, right click to stop.\n');
x=[];
y=[];
while (1)
  [px,py,pk]=ginput(1); % get click
  if (pk==1),
      x(end+1)=px;
      y(end+1)=py;
      plot(x(end),y(end),'k.');
  else
      break
  end
end

x=x';
y=y';

plot(x,y,'k.');  % plot click coordinates


x_hom = [x, ones(size(x,1),1)];

m = x_hom\y; % matrix left divide, try help mldivide


plot(x,x_hom*m,'r.-');


for i=1:length(x),
    plot([x(i) x(i)],[x_hom(i,:)*m y(i)],'b-');
end
