I need to set boundaries for my C++ program but I am having troubles doing so. P
ID: 654576 • Letter: I
Question
I need to set boundaries for my C++ program but I am having troubles doing so. Please help if possible. Here is my code so far.
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// So far, I am able to read in an image, copy the image and use the copy and draw dot function to change the position
// on each image in a loop I have set up. I haven't set up the boundaries though.
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix);
void writePGM(int nc, int nr, int mv, int *pix, int i);
void copy_img(string fname, int nc, int nr, int mv, int *&pix);
void draw_dot(int *pix, int i);
//const double gravity=-9.8;
int main()
{
int ncols = 0, nrows = 0, maxval = 255, frames;
int *pix = NULL;
double initpos, initveloc;
string fname, object, filewrite;
cout << "What is the name of your file? (.pgm) " << endl;
cin >> fname;
cout << "What is the object's initial position? (in pixels according to the size of your picture) " << endl;
cin >> initpos;
cout << "What is the object's initial velocity? (pixels/second) " << endl;
cin >> initveloc;
cout << "How many frames would you like? " << endl;
cin >> frames;
readPGM(fname, ncols, nrows, maxval, pix);
?
?
?
?
?
for (int l = initpos; l
{
copy_img(fname, ncols, nrows, maxval, pix);
draw_dot(pix, l);
writePGM(ncols, nrows, maxval, pix, l);
if (l%ncols == 0 && l>ncols)
for (int l = initpos; l
{
?
copy_img(fname, ncols, nrows, maxval, pix);
draw_dot(pix, l);
?
writePGM(ncols, nrows, maxval, pix, l);
}
else
continue;
?
?
?
?
?
return 0;
}
}
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
ifstream ifs;
ifs.open(fname);
ifs.ignore(2, ' ');
ifs >> nc >> nr >> mv;
pix = new int[nc*nr];
int tempVal;
for (int i = 0; i < (nc*nr); i++)
{
ifs >> tempVal;
pix[i] = tempVal;
}
ifs.close();
}
void writePGM(int nc, int nr, int mv, int *pix, int i)
{
char fname[100];
sprintf(fname, "output%d.pgm", i);
ofstream ofs(fname);
//
if (!ofs)
{
cerr << "Error while attempting to create the file, " << fname << "." << endl;
return;
}
ofs << "P2" << endl << nc << " " << nr << endl << mv << endl;
int size = (nc*nr), count = 0;
while (count < size)
{
for (int i = 0; i < size; i++)
{
ofs << pix[count] << " ";
count++;
}
ofs << endl;
ofs.close();
}
}
void draw_dot(int *pix, int i)
{
pix[i] = 255;
}
void copy_img(string fname, int nc, int nr, int mv, int *&pix)
{
ifstream ifs;
ifs.open(fname);
ifs.ignore(2, ' ');
ifs >> nc >> nr >> mv;
pix = new int[nc*nr];
int tempVal;
for (int i = 0; i < (nc*nr); i++)
{
ifs >> tempVal;
pix[i] = tempVal;
}
ifs.close();
}
Explanation / Answer
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix);
void writePGM(int nc, int nr, int mv, int *pix, int i);
void copy_img(string fname, int nc, int nr, int mv, int *&pix);
void draw_dot(int *pix, int i);
int main( )
{
int ncols=0, nrows=0, maxval=255;
int *pix = NULL; // instead of static int pix[1024*1024];
string filename = get_filename( );
readPGM(filename, ncols, nrows, maxval, pix);
negatePGM(ncols, nrows, maxval, pix);
writePGM("neg_"+filename, ncols, nrows, maxval, pix);
}
cout << "What is the name of your file? (.pgm) " << endl;
cin >> fname;
cout << "What is the object's initial position? (in pixels according to the size of your picture) " << endl;
cin >> initpos;
cout << "What is the object's initial velocity? (pixels/second) " << endl;
cin >> initveloc;
cout << "How many frames would you like? " << endl;
cin >> frames;
readPGM(fname, ncols, nrows, maxval, pix);
?
?
?
?
?
for (int l = initpos; l<maxval;I++)
{
copy_img(fname, ncols, nrows, maxval, pix);
draw_dot(pix, l);
writePGM(ncols, nrows, maxval, pix, l);
if (l%ncols == 0 && l>ncols)
for (int l = initpos; l
{
?
copy_img(fname, ncols, nrows, maxval, pix);
draw_dot(pix, l);
?
writePGM(ncols, nrows, maxval, pix, l);
}
else
continue;
?
?
?
?
?
return 0;
}
}
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
ifstream ifs;
ifs.open(fname);
ifs.ignore(2, ' ');
ifs >> nc >> nr >> mv;
pix = new int[nc*nr];
int tempVal;
for (int i = 0; i < (nc*nr); i++)
{
ifs >> tempVal;
pix[i] = tempVal;
}
ifs.close();
}
void writePGM(int nc, int nr, int mv, int *pix, int i)
{
char fname[100];
sprintf(fname, "output%d.pgm", i);
ofstream ofs(fname);
//
if (!ofs)
{
cerr << "Error while attempting to create the file, " << fname << "." << endl;
return;
}
ofs << "P2" << endl << nc << " " << nr << endl << mv << endl;
int size = (nc*nr), count = 0;
while (count < size)
{
for (int i = 0; i < size; i++)
{
ofs << pix[count] << " ";
count++;
}
ofs << endl;
ofs.close();
}
}
void draw_dot(int *pix, int i)
{
pix[i] = 255;
}
void copy_img(string fname, int nc, int nr, int mv, int *&pix)
{
ifstream ifs;
ifs.open(fname);
ifs.ignore(2, ' ');
ifs >> nc >> nr >> mv;
pix = new int[nc*nr];
int tempVal;
for (int i = 0; i < (nc*nr); i++)
{
ifs >> tempVal;
pix[i] = tempVal;
}
ifs.close();
}