Answer the question
In order to leave comments, you need to log in
How to rotate the matrix by 90 degrees?
I have a gradient plotted as a matrix of bits:
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();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question