%funkce na hledani koliku
function centerOfStick = hledaniStreduTycky(actPoint, radii,im)
    [r c]  = size(im);
    centerOfStick = [];
    endPoint = actPoint;
    Lft = 0;
    Rgh = 0;

%          while 1 %hledani vlevo
%             endPoint(2) = endPoint(2)-1;
%             if (im(endPoint(1), endPoint(2)) == 0)
%                 break;
%             
%             else
%                 Lft = Lft + 1;
%             end
% 
%         end    
%         endPoint = actPoint;
%         while 1 %hledani vpravo
%             endPoint(2) = endPoint(2)+1;
%             if (im(endPoint(1), endPoint(2)) == 0)
%                 break;
%            
%             else
%                 Rgh = Rgh + 1;
%             end
% 
%         end

% 
        while 1 %hledani vlevo
            endPoint(2) = endPoint(2)-1;
            if endPoint(2) <2
                break;
            end
            if (im(endPoint(1), endPoint(2)) == 1)
                Lft = Lft + 1;
            elseif endPoint(2)>3
                if im(endPoint(1), endPoint(2)) == 0 && im(endPoint(1), endPoint(2)-1) == 0 && im(endPoint(1), endPoint(2)-2) == 0 && im(endPoint(1), endPoint(2)-3) == 0
                    break;
                end
            elseif im(endPoint(1), endPoint(2)) == 0
                break;
            end

        end    
        endPoint = actPoint;
        while 1 %hledani vpravo
            endPoint(2) = endPoint(2)+1;
            if endPoint(2)>= c
                break;
            end

            if (im(endPoint(1), endPoint(2)) == 1)
                Rgh = Rgh + 1;
           elseif (endPoint(2)+3) < c 
                if im(endPoint(1), endPoint(2)) == 0 && im(endPoint(1), endPoint(2)+1) == 0 && im(endPoint(1), endPoint(2)+2) == 0 && im(endPoint(1), endPoint(2)+3) == 0
                    break;
                end
            elseif im(endPoint(1), endPoint(2)) == 0
                break;
            end 

        end
        
        if(Lft>=2*radii || Rgh >= 2*radii)
            centerOfStick = actPoint;
        elseif Rgh ==0 && Lft == 0
            centerOfStick = 0;       
     
        else
            centerOfStick(1) = endPoint(1);
            centerOfStick(2) = actPoint(2) + round((Rgh - Lft + 1)/2);
            
        end
end