static int closestCorner4 ( Grid g ) { double bestDistance = g . get ( 0 , 0 ) . center . normSq ( ) ; int bestIdx = 0 ; double d = g . get ( 0 , g . columns - 1 ) . center . normSq ( ) ; if ( d < bestDistance ) { bestDistance = d ; bestIdx = 3 ; } d = g . get ( g . rows - 1 , g . columns - 1 ) . center . normSq ( ) ; if ( d < bestDistance ) { bestDistance = d ; bestIdx = 2 ; } d = g . get ( g . rows - 1 , 0 ) . center . normSq ( ) ; if ( d < bestDistance ) { bestIdx = 1 ; } return bestIdx ; }
Number of CCW rotations to put selected corner into the canonical location . Only works when there are 4 possible solutions