
function [corners_x,corners_y,bestangle]=adjust_corners(corners_x,corners_y,plate_image,format)
%Fred Cross June 29 2016  The Rockefeller University
global ninespots;
bestangle=0;

plate_factor=sqrt(format/6)/2;

while 1
    figure(1);clf;hold on;
    imagesc(plate_image);
    title('Click near anchor point to move; click outside image or right-click to accept');
    for k=1:size(corners_x,1)
        line([corners_x(k,1), corners_x(k,3*plate_factor+1), corners_x(k,end)],...
            [corners_y(k,1), corners_y(k,3*plate_factor+1), corners_y(k,end)]);
    end
    for k=1:size(corners_x,2)
        line([corners_x(1,k), corners_x(2*plate_factor+1,k), corners_x(end,k)],...
            [corners_y(1,k), corners_y(2*plate_factor+1,k), corners_y(end,k)]);
    end
    
    for spotrow=1:3
        for spotcol=1:3
            plot(ninespots{spotrow,spotcol}(1),ninespots{spotrow,spotcol}(2),'ro');
            
        end
    end
    shg;
    [x,y,b]=ginput(1);
    
    if b==3
        break
    end
    if x<1 | y<1 | x>size(plate_image,2) | y>size(plate_image,1)
        break
    end
    
    mindist=10^16;
    for spotrow=1:3
        for spotcol=1:3
            xs=ninespots{spotrow,spotcol}(1);
            ys=ninespots{spotrow,spotcol}(2);
            dist=sqrt((x-xs)^2+(y-ys)^2);
            if dist<mindist
                mindist=dist;
                targrow=spotrow;
                targcol=spotcol;
                newx=round(x);
                newy=round(y);
            end
        end
    end
    ninespots{targrow,targcol}=[newx,newy];
    [corners_x,corners_y]=combine_four_grids(format);

end

