Draw a flag in C++

The following program outputs a flag in Netpbm ppm format.

The trios of numbers are the amounts of red, green, and blue in each pixel. They must be in the range 0 to 255 inclusive. For example, the trio 127, 127, 127 is gray, and the trio 0, 255, 255 is blue-green. There are many more colors. There’s even a flag database.


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	const int width = 300;            //number of columns of pixels
	const int height = width * 2 / 3; //number of rows of pixels

	const int xmax = width / 2;       //place origin at center
	const int ymax = height / 2;
	const int xmin = xmax - width;
	const int ymin = ymax - height;

	const int nstripes = 20;		//number of stripes
	const int npairs = nstripes / 2;	//number of pairs of stripes
	const int pairheight = height / npairs;	//height of each pair of stripes

	cout << "P3\n"                    //magic number of Netpbm .ppm file
		<< width << " " << height << "\n"
		<< 255 << "\n";           //maximum color value; 0 is minimum

	//Draw a simplified American flag.

	for (int y = ymax - 1; y >= ymin; --y) {		//top to bottom
		for (int x = xmin; x < xmax; ++x) {		//left to right
			if (x <= 0 && y >= 0) {
				//upper left quadrant is blue union jack
				cout << 0 << "\t" << 0 << "\t" << 255 << "\n";
			} else if ((y - ymin) % pairheight < pairheight / 2) {
				//white stripe
				cout << 255 << "\t" << 255 << "\t" << 255 << "\n";
			} else {
				//red stripe
				cout << 255 << "\t" << 0 << "\t" << 0 << "\n";
			}
		}
	}

	return EXIT_SUCCESS;
}

Do not add the

	system("PAUSE");
statement. Compile (or “build”) the program, creating an executable file named flag.exe on Windows, or flag or a.out on Unix. But do not run the executable within Bloodshed or Xcode. Run it with the following command at the Windows Command Prompt or Unix command line, storing the program’s output into a new file named flag.txt.

Windows:

flag.exe > flag.txt
dir flag.txt

Unix:

./a.out > flag.txt
ls -l flag.txt

Examine the contents of the flag.txt file with more.

more flag.txt
(To interrupt more, Control-break in Windows, lowercase q in Unix.) flag.txt should consist of a three-line header, followed by three parallel columns of numbers. The first 10 lines of the file should be
P3
300 200
255
0	0	255
0	0	255
0	0	255
0	0	255
0	0	255
0	0	255
0	0	255

Then upload the flag.txt file to i5.nyu.edu with the following form, which runs this gateway. The gateway should output this picture, which you will see in your browser.


GIF (Graphics Interchange Format) looks better than JPG for this kind of graphics.
JPG (Joint Photographic Experts Group)
PNG (Portable Network Graphics)
TIFF (Tagged Image File Format)



Upload the .txt file to i5.nyu.edu and display it visually.

To verify that the above form is working, here is a complete flag.txt file that you can open in your browser and save to your hard disk. Then open the saved copy with the above form.

Aux armes, citoyens! Formez vos bataillons! But let’s not all do France or the other horizontal or vertical tricolors.