document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //AUTHOR: Loukas Xanthos (thetrojan01)
  2. //T-square fractal drawer;
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. #define WHITE false
  10. #define BLACK true
  11.  
  12. #define FG BLACK
  13. #define BG WHITE
  14.  
  15. // "draws" a square in bmp with center (x,y) and sides of length = len.
  16. void mksq(vector< vector<bool> > &bmp, unsigned int centerx, unsigned int centery, unsigned int len)
  17. {
  18.     if(len == 1) {
  19.         bmp[centerx][centery] = FG;
  20.         return;
  21.     }
  22.     // move from the center to the corner at down-left and draw the square in the bitset;
  23.     centerx -= len/2;
  24.     centery -= len/2;
  25.  
  26.     for(unsigned int x = centerx; x < centerx + len; x++)
  27.         for(unsigned int y = centery; y < centery + len; y++)
  28.             bmp[x][y] = FG;
  29. }
  30.  
  31. // recursively starts drawing a fractal havig as center (centerx, centery);
  32. void fractal(vector< vector<bool> > &bmp, unsigned int centerx, unsigned int centery, unsigned int len)
  33. {
  34.     if(len < 1) return;
  35.     mksq(bmp, centerx, centery, len);
  36.  
  37.     fractal(bmp, centerx - len/2, centery - len/2, len/2); //the same at down-left;
  38.     fractal(bmp, centerx + len/2, centery - len/2, len/2); //the same at down-right;
  39.     fractal(bmp, centerx - len/2, centery + len/2, len/2); //the same at up-left;
  40.     fractal(bmp, centerx + len/2, centery + len/2, len/2); //the same at up-right;
  41. }
  42.  
  43. // saves a 2D bit map in a BW pbm file;
  44. int createBmp(vector< vector<bool> > &bmp, string strFName)
  45. {
  46.     int szDim = bmp.size();
  47.     ofstream fout(strFName.c_str());
  48.     if(!fout.is_open()) {
  49.         cerr << "Cannot open the output file." << endl;
  50.         return 1;
  51.     }
  52.  
  53.     // header
  54.     fout << "P1\\n";
  55.     fout << szDim << \' \' << szDim << \'\\n\';
  56.     // the actual bitmap
  57.     for(int i=0; i < szDim; i++) {
  58.         for(int j=0; j < szDim - 1; j++) {
  59.             fout << bmp[i][j] << \' \';
  60.         }
  61.         fout << bmp[i][szDim-1];
  62.         fout << "\\n";
  63.     }
  64.     return 0;
  65. }
  66.  
  67. int main()
  68. {
  69.     unsigned int szDim;
  70.     string fname;
  71.     vector< vector<bool> > bmp;
  72.     //gets from standard input:
  73.     //  dimension of the surrounding square;
  74.     //  filename of the output image;
  75.     cin >> szDim;
  76.     cin >> fname;
  77.     if(szDim < 4) {
  78.         return 0;
  79.     }
  80.  
  81.     bmp.resize(szDim);
  82.     for(unsigned int i=0; i < szDim; i++)
  83.         bmp[i].resize(szDim, BG);
  84.    
  85.     fractal(bmp, szDim/2, szDim/2, szDim/2);
  86.    
  87.     return createBmp(bmp, fname);
  88. }
');