#include #include #include //for sin, cos, atan2 using namespace std; struct color { unsigned char red; //can hold integers in range 0 to 255 inclusive unsigned char green; unsigned char blue; }; int main() { const size_t width = 750; const size_t height = width; color a[width][height]; 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 color white = {255, 255, 255}; const color blue = { 0, 0, 255}; const color black = { 0, 0, 0}; for (int x = xmin; x < xmax; ++x) { for (int y = ymin; y < ymax; ++y) { if (x == 0 || y == 0) { a[x - xmin][y - ymin] = blue; //axes } else { a[x - xmin][y - ymin] = white; //background } } } const int n = 3 * width; //number of points to plot const double pi = 4.0 * atan2(1.0, 1.0); for (int i = 0; i < n; ++i) { //The fraction i/n goes from 0 to almost 1, //so theta goes from 0 to almost 2 * pi radians. const double theta = 2 * pi * i / n; const double r = sin(3 * theta); //flower with 3 petals const int x = static_cast(r * cos(theta) * xmax); const int y = static_cast(r * sin(theta) * ymax); if (xmin <= x && x < xmax && ymin <= y && y < ymax) { a[x - xmin][y - ymin] = black; } } 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) { const color c = a[x - xmin][y - ymin]; //Cast makes the unsigned chars print in decimal. cout << static_cast(c.red) << "\t" << static_cast(c.green) << "\t" << static_cast(c.blue) << "\n"; } } return EXIT_SUCCESS; }