I don't know how to save in lines 0 and 1 to create a figure. This is my code, which generates lines with zeros. I need to create a square or trianlge, or rectangle (doesn't matter). Just need to know how to do that properly and save to pbm (portable bitmap) as monochromatic image.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream file;
int width=5;
int height=5;
int tab [10][10];
file.open("graphics.pbm");
if(file.good() == true)
{
file << "P1" << endl;
file << "# comment" << endl;
file << "10 10" << endl;
for (int i=0; i<width; i++)
{
for (int j=0; j<height; j++)
{
tab[i][j]=0;
}
}
for (int i=0; i<width; i++)
{
for (int j=0; j<height; j++)
{
file << tab[i][j] << " ";
}
}
file.close();
}
return 0;
}
Well you have some work to do... You need to specify a function for each geometry form which would fill your image.
Here an example which is more C++ like where a class
Imagehandle the methods to compute the geometric forms such asmakeFillCircle().Imageclass also handles the PBM output. Remark how easy it is now to save it usingstd::ostream operator<<!Live Code
Result is :
And voilà... I let you the fun to create your own functions
makeRectangle(),makeTriangle()for your needs ! That requires some little maths !Edit BONUS
As asked in the comment, here a little member function which would save the image in a file. Add it in the Image class (you can also if you prefer have a free function outside the class). Since we have a ostream operator<< this is straightforward :
An other way without modifying the code would be to catch the output of the program in a file with something like this on your terminal :
./main >> file.pgm