我目前已经旋转了90度,但我不知道如何输入任何旋转数。
例如:
1.格= n * n
2. K =转数
我只能使其旋转一次,现在必须这样做,以便K可以是一个动态数。
这是我的代码!
function rotate() {
var matrix = [[0,16,255,1],[8,128,32,1],[0,0,0,1],[1,1,1,1]];
const N = matrix.length - 1; // use a constant
// use arrow functions and nested map;
const result = matrix.map((row, i) =>
row.map((val, j) => matrix[N - j][i])
);
matrix.length = 0; // hold original array reference
matrix.push(...result); // Spread operator
console.log(matrix);
return matrix;
}
答案 0 :(得分:0)
我的建议...
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++){
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that aregreater than key, to one position
aheadof their current position */
while (j >= 0 && arr[j] > key){
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int arr[2000][2000];
int arr1[2000][2000];
int main()
{
int window[9], row = 0, col = 0, numrows = 0, numcols = 0, MAX = 0;
ifstream infile("phone.pmg");
stringstream ss;
string inputLine = "";
// First line : version
getline(infile, inputLine);
if (inputLine.compare("P2") != 0) cerr << "Version error" << endl;
else cout << "Version : " << inputLine << endl;
// Continue with a stringstream
ss << infile.rdbuf();
// Secondline : size of image
ss >> numcols >> numrows >> MAX;
//print total number of rows, columns and maximum intensity of image
cout << numcols << " columns and " << numrows << " rows" << endl<< "Maximium
Intesity "<< MAX <<endl;
//Initialize a new array of same size of image with 0
for (row = 0; row <= numrows; ++row)
{
arr1[row][0] = 0;
}
for (col = 0; col <= numcols; ++col) {
arr1[0][col] = 0;
}
// Following lines : data
for (row = 1; row <= numrows; ++row)
{
for (col = 1; col <= numcols; ++col)
{
//original data store in new array
ss >> arr1[row][col];
}
}
// Now print the array to see the result
for (row = 1; row <= numrows; ++row)
{
for (col = 1; col <= numcols; ++col)
{
//neighbor pixel values are stored in window including this pixel
window[0] = arr1[row - 1][col - 1];
window[1] = arr1[row - 1][col];
window[2] = arr1[row - 1][col + 1];
window[3] = arr1[row][col - 1];
window[4] = arr1[row][col];
window[5] = arr1[row][col + 1];
window[6] = arr1[row + 1][col - 1];
window[7] = arr1[row + 1][col];
window[8] = arr1[row + 1][col + 1];
//sort window array
insertionSort(window, 9);
//put the median to the new array
arr[row][col] = window[4];
}
}
ofstream outfile;
//new file open to stroe the output image
outfile.open("Medianfilter.pmn ");
outfile << "P2" << endl;
outfile << numcols << " " << numrows << endl;
outfile << "255" << endl;
for (row = 1; row <= numrows; ++row)
{
for (col = 1; col <= numcols; ++col)
{
//store resultant pixel values to the output file
outfile << arr[row][col] << " ";
}
}
outfile.close();
infile.close();
return 0;
}
Array.prototype.Rotate = function(RotateNumber)
{
let len = this.length
for (let l of this)
{
if ( !Array.isArray(l)) throw "this is not a matrix !"
if (l.length!==len) throw "this is not a square matrix !"
}
let r = RotateNumber %4 // max 3 rotates
if (r<0) r +=4 // for negative rotates
for(n=r;n--;)
{
let temp = this.map((r,i,A)=>
{
let t=[]
for(let j=A.length;j--;) t.push(A[j][i])
return t
})
this.forEach((r,i)=>r.forEach((c,j)=>this[i][j]=temp[i][j])) // copy values
}
return this
}
///////////////////////////////////// test part ///////////////////
function matrixDisplay(comment,matrix)
{
let prev = '[ '
console.log(comment, ' =')
for(let line of matrix)
{
console.log ( prev + '[ ' + line.map(col=>`'${col}'`).join(', ') + ' ]' )
prev = ', '
}
console.log(']')
}
var matrix =
[ [ 'a', 'b', 'c', 'd' ]
, [ '1', '2', '3', '4' ]
, [ 'X', 'Y', 'Z', 'W' ]
, [ '8', '7', '6', '5' ]
]
matrixDisplay('matrix start', matrix ,3)
matrixDisplay('matrix R2', matrix.Rotate(2),3)
matrixDisplay('matrix R-3', matrix.Rotate(-3),3)
matrixDisplay('matrix R1', matrix.Rotate(1),3)