boolean findContour ( boolean mustHaveInner ) { // find the node with the largest angleBetween NodeInfo seed = listInfo . get ( 0 ) ; for ( int i = 1 ; i < listInfo . size ( ) ; i ++ ) { NodeInfo info = listInfo . get ( i ) ; if ( info . angleBetween > seed . angleBetween ) { seed = info ; } } // trace around the contour contour . reset ( ) ; contour . add ( seed ) ; seed . contour = true ; NodeInfo prev = seed ; NodeInfo current = seed . right ; while ( current != null && current != seed && contour . size ( ) < listInfo . size ( ) ) { if ( prev != current . left ) return false ; contour . add ( current ) ; current . contour = true ; prev = current ; current = current . right ; } // fail if it is too small or was cycling return ! ( contour . size < 4 || ( mustHaveInner && contour . size >= listInfo . size ( ) ) ) ; }
Finds nodes in the outside of the grid . First the node in the grid with the largest angleBetween is selected as a seed . It is assumed at this node must be on the contour . Then the graph is traversed in CCW direction until a loop is formed .