Skip to main content
added 51 characters in body
Source Link
Kerdo
  • 65
  • 1
  • 12

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

Got some kind of atable here which shows where certain elements move when the matrix is rotated, but cannot get it working in code (those numbers around a&b are the x and y indexes):

  0                               0
0 a              0 1            0 b
1 b     ->     0 b a     ->     1 a
   (0,0)->(1,0)     (0,0)->(0,0)
   (1,0)->(0,0)     (1,0)->(0,1)

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

Got some kind of atable here which shows where certain elements move when the matrix is rotated, but cannot get it working in code:

  0                               0
0 a              0 1            0 b
1 b     ->     0 b a     ->     1 a
   (0,0)->(1,0)     (0,0)->(0,0)
   (1,0)->(0,0)     (1,0)->(0,1)

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

Got some kind of atable here which shows where certain elements move when the matrix is rotated, but cannot get it working in code (those numbers around a&b are the x and y indexes):

  0                               0
0 a              0 1            0 b
1 b     ->     0 b a     ->     1 a
   (0,0)->(1,0)     (0,0)->(0,0)
   (1,0)->(0,0)     (1,0)->(0,1)

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

added 336 characters in body
Source Link
Kerdo
  • 65
  • 1
  • 12

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

Got some kind of atable here which shows where certain elements move when the matrix is rotated, but cannot get it working in code:

  0                               0
0 a              0 1            0 b
1 b     ->     0 b a     ->     1 a
   (0,0)->(1,0)     (0,0)->(0,0)
   (1,0)->(0,0)     (1,0)->(0,1)

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

Got some kind of atable here which shows where certain elements move when the matrix is rotated, but cannot get it working in code:

  0                               0
0 a              0 1            0 b
1 b     ->     0 b a     ->     1 a
   (0,0)->(1,0)     (0,0)->(0,0)
   (1,0)->(0,0)     (1,0)->(0,1)

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

Added deeper explanation
Source Link
Kerdo
  • 65
  • 1
  • 12

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2?

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

I have this simple matrix class which has a char** array for storing the data. The data array gets initialized like this:

data = new char*[cols];
  for (int x = 0; x < cols; x++)
    data[x] = new char[rows];

Is it possible to rotate this kind of matrix when its let's say 3x2? So that the rotation will result in this (or counter clockwise works as well)

a b
c d    ->    e c a
e f          f d b

I'm currently rotating with these methods (they rotate clockwise):

void Matrix::reverseCols() {
  for (int x = 0; x < cols; x++)
    for (int y = 0, i = cols - 1; y < i; y++, i--)
      std::swap(data[y][x], data[i][x]);
}

void Matrix::transpose() {
  for (int y = 0; y < rows; y++)
    for (int x = y; x < cols; x++)
      std::swap(data[y][x], data[x][y]);
}

Should I keep it like this or should I go with a one-dimensional array and calculate the index when accessing the data by a method? And should I initialize the data array by columns (like I'm doing right now) or rows?

Wouldn't really bother doing this, but while learning C++ I want to build an console window engine and test it with a Tetris clone.

Thanks!

Source Link
Kerdo
  • 65
  • 1
  • 12
Loading