T
T
tj572019-03-11 01:49:02
Algorithms
tj57, 2019-03-11 01:49:02

How to rotate the matrix by 90 degrees?

I have a gradient plotted as a matrix of bits:
5c85934e60ee3522902507.png

vector<vector<int>> make_gradient(int height, int width)
{
  assert(height > 0 && width > 0);

  int cf = height / 255;
  int color = 0;
  vector<vector<int>> result(height, vector<int>(width));
  for (int i = 0; i < height; i += cf)
  {
    for (int j = 0; j < cf; ++j)
    {
      fill(result[i + j].begin(), result[i + j].end(), color % 255);
    }
    color++;
  }
  stable_sort(result.begin(), result.end());
  return result;
}

int main(int argc, char *argv[])
{
ofstream file;

  file.open(argv[1]);

  if (!file)
  {
    cout << "can't open file" << endl;
    return 0;
  }

  file << "P5" << "\n";

  file << numrows << " " << numcols << "\n";

  file << 255 << "\n";

  vector<vector<int>> pixmap;

      for_each(pixmap.begin(), pixmap.end(), [&](const auto& v) {
    copy(v.begin(), v.end(), ostream_iterator<char>{file, ""});
  });

  file.close();
}

The gradient goes from top to bottom, i.e. the image is vertical. How to change the algorithm so that it builds a horizontal image (gradient from left to right)? I swapped height and width in cycles, but it didn't help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YURIY KOZLOV, 2019-03-11
@WizardNG

If I understand correctly, the construct in the nested loop
fill(......
fills the entire line with the given value. So it should be replaced with filling the column. Probably, one more nested loop will be needed to go through all the lines and write to given cell value.You
should get something like this:
vector> make_gradient(int height, int width)
{
assert(height > 0 && width > 0);
int cf = width / 255;
int color = 0;
vector> result( height, vector(width));
for (int i = 0; i < height ; i += cf)
{
for (int j = 0; j < cf; ++j)
{
for (Int z =0, z < width, z++)
{
result[z][i+j] = color % 255:
}
}
color++;
}
stable_sort(result.begin(), result.end());
return result;
}
int main(int argc, char *argv[])
{
ofstream file;
file.open(argv[1]);
if (!file)
{
cout << "can't open file" << endl;
return 0;
}
file << "P5" << "\n";
file << numrows << " " << numcols << "\n";
file << 255 << "\n";
vector> pixmap;
for_each(pixmap.begin(), pixmap.end(), [&](const auto&
v) { copy(v.begin(), v.end(), ostream_iterator{file, ""});
});
file.close();
}
I hope I didn't make a mistake with the coordinates anywhere :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question