Find any possible two coordinates of Rectangle whose two coordinates are given | Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Given Input ; Function Call
function Create_Rectangle ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = arr [ i ] . split ( " " ) } let p1 = [ - 1 , - 1 ] ; let p2 = [ - 1 , - 1 ] ; let p3 = [ ] ; let p4 = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( arr [ i ] [ j ] == ' ' ) if ( p1 [ 0 ] == - 1 ) p1 = [ i , j ] ; else p2 = [ i , j ] ; } } p3 = p1 ; p4 = p2 ; if ( p1 [ 0 ] == p2 [ 0 ] ) { p3 [ 0 ] = ( p1 [ 0 ] + 1 ) % n ; p4 [ 0 ] = ( p2 [ 0 ] + 1 ) % n ; } else if ( p1 [ 1 ] == p2 [ 1 ] ) { p3 [ 1 ] = ( p1 [ 1 ] + 1 ) % n ; p4 [ 1 ] = ( p2 [ 1 ] + 1 ) % n ; } else { let temp = p3 [ 0 ] ; p3 [ 0 ] = p4 [ 0 ] ; p4 [ 0 ] = temp ; } arr [ p3 [ 0 ] ] [ p3 [ 1 ] ] = ' ' ; arr [ p4 [ 0 ] ] [ p4 [ 1 ] ] = ' ' ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] . join ( " " ) + " " ) ; } } let n = 4 ; let arr = [ " " , " " , " " , " " ] ; Create_Rectangle ( arr , n ) ;