close all
clear all

im_org = imread('lot3_ang2.bmp');
% i = rgb2gray(i); 
% i = double(i);
% getInterestPoints(i);     % This will show the image with the interest points overlaid.  

im_bw = im2bw(im_org); 
[height,width] = size(im_bw);
imshow(im_org);

hold on
% Initially, the list of points is empty.
% Loop, picking up the points.
disp('Left mouse button picks points.')
disp('Right mouse button picks last point.')
but = 1;
color = -1;

while (but==1)    
    xy = [];
    for (n=1:2)
        [xn,yn,but] = ginput(1);
        plot(xn,yn,'ro')
        xy(:,n) = [xn;yn];
    end

    plot(xy(1,:),xy(2,:),'b-'); 
    
    slope = ( xy(2,1) - xy(2,2) ) / (xy(1,1) - xy(1,2) );
    b = xy(2,1) - slope*xy(1,1);
    
    if (abs(slope) < 1)
        disp('horizontal');
          
        if (xy(1,1) > xy(1,2))
            temp = xy(1,1);
            xy(1,1) = xy(1,2);
            xy(1,2) = temp;
            temp = xy(2,1);
            xy(2,1) = xy(2,2);
            xy(2,2) = temp;
        end
        clear temp;
        
        xx = 1:width;  
        yy = slope*xx + b;
        yy = round(yy);
        
        first=1;
        count=1;
        arry_pt_start = [];
        clear pt;
        for(pt = xy(1,1) : xy(1,2) )
            if ( im_bw(yy(pt),xx(pt)) == 0)
                if ( first == 1)
                    arry_pt_start(count) = pt;
                    count = count +1;
                    first = 0;
                end
            else
                % reach white line
                first = 1;
            end
            
        end
        
        arry_pt_start(length(arry_pt_start)) = xy(1,2);
        
        
        
        for (idx_pt_start = 1:length(arry_pt_start) )
            
            color = color + 1;
            
            switch mod(color,4)
            case 0
                Rvalue = 255;
                Gvalue = 0;
                Bvalue = 0;
            case 1
                Rvalue = 0;
                Gvalue = 255;
                Bvalue = 0;
            case 2
                Rvalue = 0;
                Gvalue = 0;
                Bvalue = 255;
            case 3
                Rvalue = 255;
                Gvalue = 255;
                Bvalue = 0;                
            otherwise
                disp('Unknown method.')
            end
            
            if (( idx_pt_start ~= 1) & (idx_pt_start ~= length(arry_pt_start)))
                arry_pt_start(idx_pt_start) = arry_pt_start(idx_pt_start) + 15;
            end
            
            % start filling from pt_start down to max of height/4 (y positive)
            fillup_from = 0;
            fillup_to   =  height/4;
            fill = fillup_from;
            
            %%% pt_start = xy(1,1);
            pt_start = arry_pt_start(idx_pt_start);
            pt_end = xy(1,2);
            
            pv_idx_left = -1; % fix# -1 = first time
            pv_idx_right = -1;
            
            while (fill < fillup_to)  
                % starting pt is xy(1,1) 
                % black = 0, white = 1
                % first is going left til find a line or 1
                % then going right til find a line            
                % same pt_start (x-axis) and pt_end
                stop_left = 0;
                idx_left = pt_start;
                
                if ( im_bw(yy(pt_start)+fill,xx(pt_start)) == 0 )
                    
                    while (stop_left == 0)
                        
                        if ( im_bw(yy(idx_left)+fill,xx(idx_left)) == 1)
                            stop_left = 1;
                            idx_left = idx_left + 1;
                        else
                            idx_left = idx_left - 1;
                            if (idx_left == 1) % reach image edge, quit
                                stop_left = 1;
                            end
                        end
                        
                        if ( (pv_idx_left ~= -1) & ( idx_left < pv_idx_left -5 ) ) % fix threshold at 5 max diff
                            idx_left = idx_left + 5;
                            stop_left = 1;
                            %% break the while loop
                            fill = fillup_to + 1;
                        end
                        
                    end
                    pv_idx_left = idx_left;
                    
                    stop_right = 0;
                    idx_right = pt_start;
                    while (stop_right == 0)
                        if ( im_bw(yy(idx_right)+fill,xx(idx_right)) == 1)
                            stop_right = 1;
                            idx_right = idx_right -1;
                        else
                            idx_right = idx_right + 1;
                            if (idx_right >= width) % reach image edge, quit
                                stop_right = 1;
                            end
                        end
                        
                        if ( (pv_idx_right ~= -1) & ( idx_right > pv_idx_right +5 ) ) % fix threshold at 5 max diff
                            idx_right = idx_right - 5;
                            stop_right = 1;
                            %% break the while loop
                            fill = fillup_to + 1;                    
                        end                
                        
                    end  
                    pv_idx_right = idx_right; 
                    
                    % fill up space
                    if (fill < fillup_to)
                        for (idx = idx_left:idx_right)
                            im_org(yy(idx)+fill,xx(idx),1) = Rvalue;
                            im_org(yy(idx)+fill,xx(idx),2) = Gvalue;
                            im_org(yy(idx)+fill,xx(idx),3) = Bvalue;
                        end
                    end
                    fill = fill+1; 
                else
                    fill = fillup_to+1; % force quit   
                end
                
            end
            
            % start filling from pt_start up to max of -height/4 (y negative)
            fillup_from = -1;
            fillup_to   =  -height/4;
            fill = fillup_from;
            
            %% pt_start = xy(1,1);
            pt_start = arry_pt_start(idx_pt_start);    
            pt_end = xy(1,2);
            
            pv_idx_left = -1; % fix# -1 = first time
            pv_idx_right = -1;    
            
            
            while (fill > fillup_to)  
                % starting pt is xy(1,1) 
                % black = 0, white = 1
                % first is going left til find a line or 1
                % then going right til find a line            
                % same pt_start (x-axis) and pt_end
                stop_left = 0;
                idx_left = pt_start;
                
                if ( im_bw(yy(pt_start)+fill,xx(pt_start)) == 0 )
                    
                    while (stop_left == 0)
                        
                        if ( im_bw(yy(idx_left)+fill,xx(idx_left)) == 1)
                            stop_left = 1;
                            idx_left = idx_left + 1;
                        else
                            idx_left = idx_left - 1;
                            if (idx_left == 1) % reach image edge, quit
                                stop_left = 1;
                            end
                        end
                        
                        if ( (pv_idx_left ~= -1) & ( idx_left < pv_idx_left -5 ) ) % fix threshold at 5 max diff
                            idx_left = idx_left + 5;
                            stop_left = 1;
                            %% break the while loop
                            fill = fillup_to - 1;
                        end
                        
                    end
                    pv_idx_left = idx_left;
                    
                    if (fill <= fillup_to)
                        break;
                    end
                    
                    stop_right = 0;
                    idx_right = pt_start;
                    while (stop_right == 0)
                        if ( im_bw(yy(idx_right)+fill,xx(idx_right)) == 1)
                            stop_right = 1;
                            idx_right = idx_right -1;
                        else
                            idx_right = idx_right + 1;
                            if (idx_right >= width) % reach image edge, quit
                                stop_right = 1;
                            end
                        end
                        
                        if ( (pv_idx_right ~= -1) & ( idx_right > pv_idx_right +5 ) ) % fix threshold at 5 max diff
                            idx_right = idx_right - 5;
                            stop_right = 1;
                            %% break the while loop
                            fill = fillup_to - 1;                    
                        end                
                        
                    end  
                    pv_idx_right = idx_right; 
                    
                    % fill up space
                    if (fill > fillup_to)
                        for (idx = idx_left:idx_right)
                            im_org(yy(idx)+fill,xx(idx),1) = Rvalue;
                            im_org(yy(idx)+fill,xx(idx),2) = Gvalue;
                            im_org(yy(idx)+fill,xx(idx),3) = Bvalue;
                        end
                    end
                    fill = fill-1; 
                else
                    fill = fillup_to-1; % force quit   
                end
                
            end
            
        end
        
    
    else
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% vertical
        
        
        disp('vertical');
        
        if (xy(2,1) > xy(2,2))
            temp = xy(1,1);
            xy(1,1) = xy(1,2);
            xy(1,2) = temp;
            temp = xy(2,1);
            xy(2,1) = xy(2,2);
            xy(2,2) = temp;
        end
        clear temp;
        
        yy = 1:height;  
        if (slope > 50)
            slope = 50;
        end
        xx = (yy - b)/slope;
        xx = round(xx);
        
        first=1;
        count=1;
        arry_pt_start = [];
        clear pt;
        for(pt = xy(2,1) : xy(2,2) )
            if ( im_bw(yy(pt),xx(pt)) == 0)
                if ( first == 1)
                    arry_pt_start(count) = pt;
                    count = count +1;
                    first = 0;
                end
            else
                % reach white line
                first = 1;
            end
            
        end
        
        % last point is the usr input point
        arry_pt_start(length(arry_pt_start)) = xy(2,2);
        
        
        
        for (idx_pt_start = 1:length(arry_pt_start) )
            
            color = color + 1;
            
            switch mod(color,4)
            case 0
                Rvalue = 255;
                Gvalue = 0;
                Bvalue = 0;
            case 1
                Rvalue = 0;
                Gvalue = 255;
                Bvalue = 0;
            case 2
                Rvalue = 0;
                Gvalue = 0;
                Bvalue = 255;
            case 3
                Rvalue = 255;
                Gvalue = 255;
                Bvalue = 0;                
            otherwise
                disp('Unknown method.')
            end
            
            if (( idx_pt_start ~= 1) & (idx_pt_start ~= length(arry_pt_start)))
                arry_pt_start(idx_pt_start) = arry_pt_start(idx_pt_start) + 15;
            end
            
            % start filling from pt_start right to max of width/4 (x positive)
            fillup_from = 0;
            fillup_to   =  width/4;
            fill = fillup_from;
            
            %%% pt_start = xy(1,1);
            pt_start = arry_pt_start(idx_pt_start);
            pt_end = xy(2,2);
            
            pv_idx_left = -1; % fix# -1 = first time
            pv_idx_right = -1;
            
            while (fill < fillup_to)  
                % starting pt is xy(2,1) 
                % black = 0, white = 1
                % first is going down til find a line or 1
                % then going left til find a line            
                % same pt_start (y-axis) and pt_end
                stop_left = 0;
                idx_left = pt_start;
                
                if ( im_bw(yy(pt_start),xx(pt_start)+fill) == 0 )
                    
                    while (stop_left == 0)
                        
                        if ( im_bw(yy(idx_left),xx(idx_left)+fill) == 1)
                            stop_left = 1;
                            idx_left = idx_left + 1;
                        else
                            idx_left = idx_left - 1;
                            if (idx_left == 1) % reach image edge, quit
                                stop_left = 1;
                            end
                        end
                        
                        if ( (pv_idx_left ~= -1) & ( idx_left < pv_idx_left -5 ) ) % fix threshold at 5 max diff
                            idx_left = idx_left + 5;
                            stop_left = 1;
                            %% break the while loop
                            fill = fillup_to + 1;
                        end
                        
                    end
                    pv_idx_left = idx_left;
                    
                    stop_right = 0;
                    idx_right = pt_start;
                    while (stop_right == 0)
                        if ( im_bw(yy(idx_right),xx(idx_right)+fill) == 1)
                            stop_right = 1;
                            idx_right = idx_right -1;
                        else
                            idx_right = idx_right + 1;
                            if (idx_right >= height) % reach image edge, quit
                                stop_right = 1;
                            end
                        end
                        
                        if ( (pv_idx_right ~= -1) & ( idx_right > pv_idx_right +5 ) ) % fix threshold at 5 max diff
                            idx_right = idx_right - 5;
                            stop_right = 1;
                            %% break the while loop
                            fill = fillup_to + 1;                    
                        end                
                        
                    end  
                    pv_idx_right = idx_right; 
                    
                    % fill up space
                    if (fill < fillup_to)
                        for (idx = idx_left:idx_right)
                            im_org(yy(idx),xx(idx)+fill,1) = Rvalue;
                            im_org(yy(idx),xx(idx)+fill,2) = Gvalue;
                            im_org(yy(idx),xx(idx)+fill,3) = Bvalue;
                        end
                    end
                    fill = fill+1; 
                else
                    fill = fillup_to+1; % force quit   
                end
                
            end
            
            % start filling from pt_start up to max of -height/4 (y negative)
            fillup_from = -1;
            fillup_to   =  -width/4;
            fill = fillup_from;
            
            %% pt_start = xy(1,1);
            pt_start = arry_pt_start(idx_pt_start);    
            pt_end = xy(2,2);
            
            pv_idx_left = -1; % fix# -1 = first time
            pv_idx_right = -1;    
            
            
            while (fill > fillup_to)  
                % starting pt is xy(1,1) 
                % black = 0, white = 1
                % first is going left til find a line or 1
                % then going right til find a line            
                % same pt_start (x-axis) and pt_end
                stop_left = 0;
                idx_left = pt_start;
                
                if ( im_bw(yy(pt_start),xx(pt_start)+fill) == 0 )
                    
                    while (stop_left == 0)
                        
                        if ( im_bw(yy(idx_left),xx(idx_left)+fill) == 1)
                            stop_left = 1;
                            idx_left = idx_left + 1;
                        else
                            idx_left = idx_left - 1;
                            if (idx_left == 1) % reach image edge, quit
                                stop_left = 1;
                            end
                        end
                        
                        if ( (pv_idx_left ~= -1) & ( idx_left < pv_idx_left -5 ) ) % fix threshold at 5 max diff
                            idx_left = idx_left + 5;
                            stop_left = 1;
                            %% break the while loop
                            fill = fillup_to - 1;
                        end
                        
                    end
                    pv_idx_left = idx_left;
                    
                    if (fill <= fillup_to)
                        break;
                    end
                    
                    stop_right = 0;
                    idx_right = pt_start;
                    while (stop_right == 0)
                        if ( im_bw(yy(idx_right),xx(idx_right)+fill) == 1)
                            stop_right = 1;
                            idx_right = idx_right -1;
                        else
                            idx_right = idx_right + 1;
                            if (idx_right >= height) % reach image edge, quit
                                stop_right = 1;
                            end
                        end
                        
                        if ( (pv_idx_right ~= -1) & ( idx_right > pv_idx_right +5 ) ) % fix threshold at 5 max diff
                            idx_right = idx_right - 5;
                            stop_right = 1;
                            %% break the while loop
                            fill = fillup_to - 1;                    
                        end                
                        
                    end  
                    pv_idx_right = idx_right; 
                    
                    % fill up space
                    if (fill > fillup_to)
                        for (idx = idx_left:idx_right)
                            im_org(yy(idx),xx(idx)+fill,1) = Rvalue;
                            im_org(yy(idx),xx(idx)+fill,2) = Gvalue;
                            im_org(yy(idx),xx(idx)+fill,3) = Bvalue;
                        end
                    end
                    fill = fill-1; 
                else
                    fill = fillup_to-1; % force quit   
                end
                
            end
            
        end
        
            
        
        
    end
    
    
end

hold off

figure; imshow(im_org);
