private void connectToNeighbors ( int x , int y ) { List < LineSegment2D_F32 > lines = grid . get ( x , y ) ; Iterator < LineSegment2D_F32 > iter = lines . iterator ( ) ; while ( iter . hasNext ( ) ) { LineSegment2D_F32 l = iter . next ( ) ; boolean connected = false ; if ( connectTry ( l , x + 1 , y ) ) connected = true ; if ( ! connected && connectTry ( l , x + 1 , y + 1 ) ) connected = true ; if ( ! connected && connectTry ( l , x , y + 1 ) ) connected = true ; if ( ! connected && connectTry ( l , x - 1 , y + 1 ) ) connected = true ; // the line was added to the connecting grid // remove it to avoid double counting the line if ( connected ) iter . remove ( ) ; } }
Connect lines in the target region to lines in neighboring regions . Regions are selected such that no two regions are compared against each other more than once .