/* * Generate an image of random circles of random colors in random * locations. Inputs the file name, dimensions, and number of * circles to generate. */ #include #include #include #include #include #include "img.h" using namespace std; // Prompt and read a parameter. template Dtype prompt_read(string prompt) { cout << prompt << ": "; Dtype ret; cin >> ret; return ret; } /* * Constraints on the random selection. */ // How far from the edges. static const int MARGIN = 20; // Center at least margin from edge static const int MINRAD = 25; // Minimum radius static const int CLEAR = 5; // Extra space in closest dim. static const int MINDIM = 2*MARGIN+MINRAD+3*CLEAR; // Minimum image dimension. // Generate a circle of random color and size at a random location. void gen_circle(image& img) { int mindir = min(img.rows(),img.cols()); // Choose the center. int crow = rand() % (img.rows() - 2*MARGIN) + MARGIN; int ccol = rand() % (img.cols() - 2*MARGIN) + MARGIN; // Choose the diameter and radius. int dia = rand() % (mindir - CLEAR); int rad = dia/2; int radsq = rad*rad; // Compute the visible row range. int minrow = max(crow - rad, 0); int maxrow = min(crow + rad, img.rows()-1); // Draw the circle pixel c; c.random(); for(int row = minrow; row <= maxrow; ++row) { int h = crow - row; int cdist = round(sqrt(radsq - h*h)); int start = max(0, ccol - cdist); int end = min(ccol + cdist, img.cols()-1); for(int col = start; col <= end; ++col) img.set(row, col, c); } } int main(int argc, char **argv) { // Get file name. string fn; if(argc >= 2) fn = argv[1]; else fn = prompt_read("File name to write"); // Get the number of rows. int nrow; if(argc >= 3) nrow = atoi(argv[2]); else nrow = prompt_read("Number of rows"); // Get the number of cols. int ncol; if(argc >= 4) ncol = atoi(argv[3]); else ncol = prompt_read("Number of columns"); if(nrow <= MINDIM || ncol <= MINDIM) { cerr << "Image size is too small" << std::endl; exit(2); } // Get the number of circles to generate. int ncircle; if(argc >= 5) ncircle = atoi(argv[4]); else ncircle = prompt_read("Number of circles"); // Make random stuff really random. srand(time(0)); // Choose a background color and create the image filled with it. pixel bg; bg.random(); image im(nrow, ncol, bg); // Add the circles. while(ncircle-- > 0) gen_circle(im); // Generate the image file. im.save(fn); }