function [matchedFP] = LACC_distance_matching (img1, img2, FP1, FP2, Nhood, max_distance)
%% LACC FP matching with distance threshold
    % Funkcia na párovanie FP za pomoci LACC pričom sú brané do úvahy
    % maximálne vzdialenosti dvoch párov bodov

    %%
        if (size(FP1,1)) < (size(FP2,1))
            
            LACC = cell(1,size(FP1,1));
            matchedFP = zeros(size(FP1,1) , 4);
           
            for i = 1:(size (FP1,1))
                
                if (FP1(i,1)<= Nhood) || ((FP1(i,1)+Nhood)>=size(img1,1)) || (FP1(i,2)<=Nhood) || ((FP1(i,2)+Nhood)>=size(img1,2))
                else
                    imgFP1 = img1( (FP1(i,1)-Nhood):(FP1(i,1)+Nhood) , (FP1(i,2)-Nhood):(FP1(i,2)+Nhood) ) ;

                    for j = 1:(size(FP2,1))
                        if (FP2(j,1)<= Nhood) || ((FP2(j,1)+Nhood)>=size(img2,1)) || (FP2(j,2)<=Nhood) || ((FP2(j,2)+Nhood)>=size(img2,2))
                        else

                            imgFP2 = img2( (FP2(j,1)-Nhood):(FP2(j,1)+Nhood) , (FP2(j,2)-Nhood):(FP2(j,2)+Nhood) ) ;
                            LACCtemp = corr2(imgFP1,imgFP2);
                            LACC{i}(j,3) = LACCtemp;
                            LACC{i}(j,4:5) = FP2(j,1:2);
                            LACC{i}(j,1:2) = FP1(i,1:2);
  
                        end

                    end
   
                end

            end

            
            for i = 1:size(LACC,2)
                LACC_max = 0;    
                for j = 1:size(LACC{i},1)

                    distance = sqrt( (((LACC{i}(j,1)) - (LACC{i}(j,4)))^2) + (((LACC{i}(j,2)) - (LACC{i}(j,5)))^2) );
                    
                    if distance < max_distance
                        
                        if (LACC{i}(j,3)) > LACC_max
                            matchedFP(i,1:2) = LACC{i}(j,1:2);
                            matchedFP(i,3:4) = LACC{i}(j,4:5);
                            LACC_max = LACC{i}(j,3);
                        end
                    end

                end
            end
%%
        else
%%
            LACC = cell(1,size(FP2,1));
            matchedFP = zeros(size(FP2,1) , 4);

            for i = 1:(size (FP2,1))
    
                if (FP2(i,1)<= Nhood) || ((FP2(i,1)+Nhood)>=size(img2,1)) || (FP2(i,2)<=Nhood) || ((FP2(i,2)+Nhood)>=size(img2,2))
                else
                    imgFP2 = img2( (FP2(i,1)-Nhood):(FP2(i,1)+Nhood) , (FP2(i,2)-Nhood):(FP2(i,2)+Nhood) ) ;

                    for j = 1:(size(FP1,1))
                        if (FP1(j,1)<= Nhood) || ((FP1(j,1)+Nhood)>=size(img1,1)) || (FP1(j,2)<=Nhood) || ((FP1(j,2)+Nhood)>=size(img1,2))
                        else

                            imgFP1 = img1( (FP1(j,1)-Nhood):(FP1(j,1)+Nhood) , (FP1(j,2)-Nhood):(FP1(j,2)+Nhood) );
                            LACCtemp = corr2(imgFP2,imgFP1);
                            LACC{i}(j,3) = LACCtemp;
                            LACC{i}(j,4:5) = FP2(i,1:2);
                            LACC{i}(j,1:2) = FP1(j,1:2);

                        end

                    end

                end

            end
            
            for i = 1:size(LACC,2)
                LACC_max = 0;    
                for j = 1:size(LACC{i},1)

                    distance = sqrt( (((LACC{i}(j,1)) - (LACC{i}(j,4)))^2) + (((LACC{i}(j,2)) - (LACC{i}(j,5)))^2) );
                    
                    if distance < max_distance
                        
                        if (LACC{i}(j,3)) > LACC_max
                            matchedFP(i,1:2) = LACC{i}(j,1:2);
                            matchedFP(i,3:4) = LACC{i}(j,4:5);
                            LACC_max = LACC{i}(j,3);
                        end
                        
                    end

                end
                
            end
            
        end
        

end