Mirror of matrix across diagonal | Simple Javascript program to find mirror of matrix across diagonal . ; for diagonal which start from at first row of matrix ; traverse all top right diagonal ; here we use stack for reversing the element of diagonal ; push all element back to matrix in reverse order ; do the same process for all the diagonal which start from last column ; here we use stack for reversing the elements of diagonal ; push all element back to matrix in reverse order ; Utility function to print a matrix ; Driver code
let MAX = 100 ; function imageSwap ( mat , n ) { let row = 0 ; for ( let j = 0 ; j < n ; j ++ ) { let s = [ ] ; let i = row , k = j ; while ( i < n && k >= 0 ) { s . push ( mat [ i ++ ] [ k -- ] ) ; } i = row ; k = j ; while ( i < n && k >= 0 ) { mat [ i ++ ] [ k -- ] = s [ s . length - 1 ] ; s . pop ( ) ; } } let column = n - 1 ; for ( let j = 1 ; j < n ; j ++ ) { let s = [ ] ; let i = j , k = column ; while ( i < n && k >= 0 ) { s . push ( mat [ i ++ ] [ k -- ] ) ; } i = j ; k = column ; while ( i < n && k >= 0 ) { mat [ i ++ ] [ k -- ] = s [ s . length - 1 ] ; s . pop ( ) ; } } } function printMatrix ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; let n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ;