clear all
close all

disp('RanDemo')

x=RanLine(20,20);
nPoints=length(x);
x=[x;ones(1,nPoints)];


plot(x(1,:),x(2,:),'.')
axis tight
pause


%Plot LS fit.

bestIn=0;

for itt=1:10
    plot(x(1,:),x(2,:),'.')
    axis tight
    
    %Get Random set and estimate line
    s=randperm(nPoints);    s=s(1:2);
    Ps=[ x(:,s(1))  x(:,s(2))];
    l=cross(Ps(:,1),Ps(:,2));   l=l/l(3);
    hold on
    plot(Ps(1,:),Ps(2,:),'r*')
    hold off
    DrawLine(l)
    
    %Find number of inliers
    Dist=abs(l'*x/norm(l(1:2)));
    idx=find(Dist<1);
    hold on
    plot(x(1,idx),x(2,idx),'go')
    hold off
    nIn=length(idx);
    if(nIn>bestIn)
        bestIn=nIn;
        bestLine=l;
    end
    
    title(sprintf('Iteration %d, Inliers %d, Best %d',itt, nIn,bestIn))
    
    
    
    pause
end

plot(x(1,:),x(2,:),'.')
axis tight
DrawLine(bestLine)
title(sprintf('Best Line with %d inliers',bestIn))

