Paradox
Paradox

Reputation: 141

Rotating a rectangular array in JavaScript

I am trying to make a Tetris game. I am trying to work on a function that rotates a 2D variable array 90 degrees (or -90).

For example, given an array like:

"-T-",
"TTT"

It would output:

"T-",
"TT",
"T-"

I have tried this function:

function rotateN90(a){
    var temp = [];
    for(var x = 0; x<a[0].length; x++){
        temp.push("");
        for(var y = 0; y<a.length; y++){
            temp[x] += a[y][x];
        }
    }
    
    return temp;
}

But it does not give the desired result. While it does rotate the first T-Block example given -90 degrees once, afterwards it reverts to it's original state.

Please help!

(PS: I am using KA's processing environment, so I can't use libraries or ES6)

Upvotes: 0

Views: 745

Answers (3)

The Bomb Squad
The Bomb Squad

Reputation: 4337

This answer would work for flipping 90 AND flipping -90
a truthy value in the left parameter would flip it -90
a falsey value in the left parameter would flip it +90

//1 to rotate left, 0 to rotate right
function rotate(arr,left){
  var newArr=[]
  arr.forEach(function(a){newArr.push(a.toString())})
  arr=newArr //we gonna do some wild stuff so this is to not mess with the original array given to function
  arr=arr.map(function(a){return a.split``})
  var newArr=new Array(arr[0].length)
  for(var i=0;i<newArr.length;i++){newArr[i]=[]}
  arr.forEach(function(a,i){
    a.forEach(function(b,j){
      newArr[j][i]=b
    })
  })
  if(left){
    newArr=newArr.map(function(a){return a.join``})
    return(newArr)
  }
  //else(right)
  newArr.map(function(a){a.reverse()})
  newArr=newArr.map(function(a){a.join``})
  return(newArr)
}

//example 1 (-90 degrees)
console.log("example 1(-90 degrees)",rotate(["-T-","TTT"],1))
//same example but you can use truthy or falsy values not JUST 1 or 0
console.log("example 1(-90 degrees) with another truthy value",rotate(["-T-","TTT"],{a:true}))

//example 2(+90 degrees)
console.log("example 2(+90 degrees)",rotate(["-T-","TTT"],0))

Upvotes: 1

Other Me
Other Me

Reputation: 508

class Array2D extends Array {
  constructor(width, height, array) {
    super();
    this.width = width;
    this.height = height;
    for(let i = 0; i < width*height; i++) {
      this[i] = array ? array[i]:0;
    }
  }
  set(x, y, value) {
    this[x+y*this.width] = value;
  }
  get(x, y) {
    return this[x+y*this.width];
  }
  static swap(array2d) {
    const result = new Array2D(array2d.height, array2d.width);
    for(let x = 0; x < array2d.width; x++) {
      for(let y = 0; y < array2d.height; y++) {
        result.set(y, x, array2d.get(x, y));
      }
    }
    return result;
  }
  static flip(array2d) {
    const result = new Array2D(array2d.width, array2d.height);
    for(let x = 0; x < array2d.width; x++) {
      for(let y = 0; y < array2d.height; y++) {
        result.set(x, array2d.height-1-y, array2d.get(x, y));
      }
    }
    return result;
  }
  static spin(array2d) {
    const swapped = Array2D.swap(array2d);
    return Array2D.flip(swapped);
  }
}

const a2d = new Array2D(2, 2, [1, 1, 1, 0]);
console.log(Array2D.spin(Array2D.spin(a2d)));

This should do the job, changed format though a little. Because class notation isn't allowed in khan academy here is a modified solution

//technically this one is a little unnessecary, but I like the organization
function create2D(width, height, array) {
  var arr = [];
  arr.width = width;
  arr.height = height;
  for(var i = 0; i < width*height; i++) {
    arr[i] = array ? array[i]:0;
  }
  return arr;
}

function set(array, x, y, value) {
  array[x+y*array.width] = value;
}

function get(array, x, y) {
  return array[x+y*array.width];
}

function swap(array2d) {
  var result = create2D(array2d.height, array2d.width);
  for(var x = 0; x < array2d.width; x++) {
    for(var y = 0; y < array2d.height; y++) {
      set(result, y, x, get(array2d, x, y));
    }
  }
  return result;
}

function flip(array2d) {
  var result = create2D(array2d.width, array2d.height);
  for(var x = 0; x < array2d.width; x++) {
    for(var y = 0; y < array2d.height; y++) {
      set(result, x, array2d.height-1-y, get(array2d, x, y));
    }
  }
  return result;
}

function spin(array2d) {
  return flip(swap(array2d));
}

var a1 = create2D(2, 2, [1, 1, 1, 0]);
var a2 = spin(spin(a1));

console.log(a2);

Upvotes: 1

manaschopra98
manaschopra98

Reputation: 101

The following code is to rotate a mxn size array to -90 degree.

function rotateN90(a){

 var temp = new Array(a[0].length); // number of columns
 var i=0;

 for (i = 0; i < temp.length; i++) { 
     temp[i] = [];
 } 

 for(i=0;i<a.length;i++){
    
     for(let j = 0; j<a[0].length;j++){

         temp[j][i]= a[i][a[i].length-1-j];
     }
 }

 return temp;
}

If your array is : [[1, 2,3],[4, 5, 6]]

It will rotate -90 degree and returned array will be [[3, 6],[2, 5],[1, 4]]

Upvotes: 2

Related Questions