#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; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //Draw flag of England. for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (x < -10 && y > 10) { //upper left quadrant is white cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else if (x > 10 && y > 10) { //upper right quadrant is white cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else if (x > 10 && y < -10) { //bottom right quadrant is white cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else if (x < -10 && y < -10) { //bottom left quadrant is white cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } else { //red cross (internal border) with 20 separating the quadriants cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } } } return EXIT_SUCCESS; }
Simpler to draw the foreground first.
#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; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw flag of England. for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (x >= -10 && x <= 10) { //vertical red stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else (y >= -10 && y <= 10) { //horizontal red stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else { //white background cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } } } return EXIT_SUCCESS; }
Consolidate the repetition with
||
(“or”).
#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; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw flag of England. for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (x >= -10 && x <= 10 //vertical red stripe || y >= -10 && y <= 10) { //horizontal red stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else { //white background cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } } } return EXIT_SUCCESS; }
Consolidate the repetition with
abs
(absolute value).
#include <iostream> #include <cstdlib> //for abs 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; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum //Draw flag of England. for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (abs(x) <= 10 || abs(y) <= 10) { //vertical or horizontal red stripe cout << 255 << "\t" << 0 << "\t" << 0 << "\n"; } else { //white background cout << 255 << "\t" << 255 << "\t" << 255 << "\n"; } } } return EXIT_SUCCESS; }
Even better: change 10 to
width / 30
.
The stripes cross at the point that is 40 pixels to the right of the center of the flag.
//This program produces a Swedish flag in RGB values #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; int stripes_width = width/20; cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if ((x >= -stripes_width - 40 && x <= stripes_width - 40) || (y >= -stripes_width && y <= stripes_width) ) { //yellow cross cout << 255 << "\t" << 204 << "\t" << 0 << "\n"; } else { //blue background cout << 0 << "\t" << 127 << "\t" << 229 << "\n"; } } } return EXIT_SUCCESS; }
Let’t put the origin (0, 0) at the point where the stripes cross.
x
will go from –100 to 189.
This will eliminate 60,000 pairs of subtractions.
stripes_width
should be
const
.
//This program produces a Swedish flag in RGB values #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 * 19 / 30; //place origin where stripes cross const int ymax = height / 2; const int xmin = xmax - width; const int ymin = ymax - height; const int stripes_width = width / 20; //half of width of stripe cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (x >= -stripes_width && x <= stripes_width || y >= -stripes_width && y <= stripes_width) { //yellow cross cout << 255 << "\t" << 204 << "\t" << 0 << "\n"; } else { //blue background cout << 0 << "\t" << 127 << "\t" << 229 << "\n"; } } } return EXIT_SUCCESS; }
Consolidate the repetition with
abs
(absolute value).
//This program produces a Swedish flag in RGB values #include <iostream> #include <cstdlib> //for abs 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 * 19 / 30; //place origin where stripes cross const int ymax = height / 2; const int xmin = xmax - width; const int ymin = ymax - height; const int stripes_width = width / 20; //half of width of stripe cout << "P3\n" //magic number of Netpbm .ppm file << width << " " << height << "\n" << 255 << "\n"; //maximum color value; 0 is minimum for (int y = ymax - 1; y >= ymin; --y) { for (int x = xmin; x < xmax; ++x) { if (abs(x) <= stripes_width || abs(y) <= stripes_width) { //yellow cross cout << 255 << "\t" << 204 << "\t" << 0 << "\n"; } else { //blue background cout << 0 << "\t" << 127 << "\t" << 229 << "\n"; } } } return EXIT_SUCCESS; }
Even shorter:
if (min(abs(x), abs(y)) <= stripes_width) {
quadrant
is an
int
whose value is 0, 1, 2, or 3.
// set the quadrant controls int qc_w, qc_h; if ( quadrant == 0 || quadrant == 1 ) { qc_w = 1; } else { qc_w = -1; } if ( quadrant == 0 || quadrant == 3 ) { qc_h = 1; } else { qc_h = -1; }
qc_w
and
qc_h
can now be
const
.
struct quad { int width; int height; }; static const quad[] { { 1, 1}, //upper right { 1, -1}, //lower right {-1, -1}, //lower left {-1, 1} //upper left }; const int qc_w = quad[quadrant].width; const int qc_h = quad[quadrant].height;